conf.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "fmt"
  17. "github.com/lestrrat-go/file-rotatelogs"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/sirupsen/logrus"
  20. "gopkg.in/yaml.v3"
  21. "io"
  22. "io/ioutil"
  23. "os"
  24. "path"
  25. "time"
  26. )
  27. const StreamConf = "kuiper.yaml"
  28. var (
  29. Config *KuiperConf
  30. IsTesting bool
  31. )
  32. func LoadConf(confName string) ([]byte, error) {
  33. confDir, err := GetConfLoc()
  34. if err != nil {
  35. return nil, err
  36. }
  37. file := path.Join(confDir, confName)
  38. b, err := ioutil.ReadFile(file)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return b, nil
  43. }
  44. type tlsConf struct {
  45. Certfile string `yaml:"certfile"`
  46. Keyfile string `yaml:"keyfile"`
  47. }
  48. type KuiperConf struct {
  49. Basic struct {
  50. Debug bool `yaml:"debug"`
  51. ConsoleLog bool `yaml:"consoleLog"`
  52. FileLog bool `yaml:"fileLog"`
  53. RotateTime int `yaml:"rotateTime"`
  54. MaxAge int `yaml:"maxAge"`
  55. Ip string `yaml:"ip"`
  56. Port int `yaml:"port"`
  57. RestIp string `yaml:"restIp"`
  58. RestPort int `yaml:"restPort"`
  59. RestTls *tlsConf `yaml:"restTls"`
  60. Prometheus bool `yaml:"prometheus"`
  61. PrometheusPort int `yaml:"prometheusPort"`
  62. PluginHosts string `yaml:"pluginHosts"`
  63. }
  64. Rule api.RuleOption
  65. Sink struct {
  66. CacheThreshold int `yaml:"cacheThreshold"`
  67. CacheTriggerCount int `yaml:"cacheTriggerCount"`
  68. DisableCache bool `yaml:"disableCache"`
  69. }
  70. Store struct {
  71. Type string `yaml:"type"`
  72. Redis struct {
  73. Host string `yaml:"host"`
  74. Port int `yaml:"port"`
  75. Password string `yaml:"password"`
  76. Timeout int `yaml:"timeout"`
  77. }
  78. Sqlite struct {
  79. Name string `yaml:"name"`
  80. }
  81. }
  82. }
  83. func InitConf() {
  84. b, err := LoadConf(StreamConf)
  85. if err != nil {
  86. Log.Fatal(err)
  87. }
  88. kc := KuiperConf{
  89. Rule: api.RuleOption{
  90. LateTol: 1000,
  91. Concurrency: 1,
  92. BufferLength: 1024,
  93. CheckpointInterval: 300000, //5 minutes
  94. SendError: true,
  95. },
  96. }
  97. if err := yaml.Unmarshal(b, &kc); err != nil {
  98. Log.Fatal(err)
  99. } else {
  100. Config = &kc
  101. }
  102. if 0 == len(Config.Basic.Ip) {
  103. Config.Basic.Ip = "0.0.0.0"
  104. }
  105. if 0 == len(Config.Basic.RestIp) {
  106. Config.Basic.RestIp = "0.0.0.0"
  107. }
  108. if Config.Basic.Debug {
  109. Log.SetLevel(logrus.DebugLevel)
  110. }
  111. if Config.Basic.FileLog {
  112. logDir, err := GetLoc(logDir)
  113. if err != nil {
  114. Log.Fatal(err)
  115. }
  116. file := path.Join(logDir, logFileName)
  117. logWriter, err := rotatelogs.New(
  118. file+".%Y-%m-%d_%H-%M-%S",
  119. rotatelogs.WithLinkName(file),
  120. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  121. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  122. )
  123. if err != nil {
  124. fmt.Println("Failed to init log file settings..." + err.Error())
  125. Log.Infof("Failed to log to file, using default stderr.")
  126. } else if Config.Basic.ConsoleLog {
  127. mw := io.MultiWriter(os.Stdout, logWriter)
  128. Log.SetOutput(mw)
  129. } else if !Config.Basic.ConsoleLog {
  130. Log.SetOutput(logWriter)
  131. }
  132. } else if Config.Basic.ConsoleLog {
  133. Log.SetOutput(os.Stdout)
  134. }
  135. }
  136. func init() {
  137. InitLogger()
  138. InitClock()
  139. }