conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "io"
  21. "os"
  22. "path"
  23. "time"
  24. )
  25. const ConfFileName = "kuiper.yaml"
  26. var (
  27. Config *KuiperConf
  28. IsTesting bool
  29. )
  30. type tlsConf struct {
  31. Certfile string `yaml:"certfile"`
  32. Keyfile string `yaml:"keyfile"`
  33. }
  34. type KuiperConf struct {
  35. Basic struct {
  36. Debug bool `yaml:"debug"`
  37. ConsoleLog bool `yaml:"consoleLog"`
  38. FileLog bool `yaml:"fileLog"`
  39. RotateTime int `yaml:"rotateTime"`
  40. MaxAge int `yaml:"maxAge"`
  41. Ip string `yaml:"ip"`
  42. Port int `yaml:"port"`
  43. RestIp string `yaml:"restIp"`
  44. RestPort int `yaml:"restPort"`
  45. RestTls *tlsConf `yaml:"restTls"`
  46. Prometheus bool `yaml:"prometheus"`
  47. PrometheusPort int `yaml:"prometheusPort"`
  48. PluginHosts string `yaml:"pluginHosts"`
  49. Authentication bool `yaml:"authentication"`
  50. IgnoreCase bool `yaml:"ignoreCase"`
  51. }
  52. Rule api.RuleOption
  53. Sink struct {
  54. CacheThreshold int `yaml:"cacheThreshold"`
  55. CacheTriggerCount int `yaml:"cacheTriggerCount"`
  56. DisableCache bool `yaml:"disableCache"`
  57. }
  58. Store struct {
  59. Type string `yaml:"type"`
  60. Redis struct {
  61. Host string `yaml:"host"`
  62. Port int `yaml:"port"`
  63. Password string `yaml:"password"`
  64. Timeout int `yaml:"timeout"`
  65. ConnectionSelector string `yaml:"connectionSelector"`
  66. }
  67. Sqlite struct {
  68. Name string `yaml:"name"`
  69. }
  70. }
  71. Portable struct {
  72. PythonBin string `yaml:"pythonBin"`
  73. }
  74. }
  75. func InitConf() {
  76. cpath, err := GetConfLoc()
  77. if err != nil {
  78. panic(err)
  79. }
  80. kc := KuiperConf{
  81. Rule: api.RuleOption{
  82. LateTol: 1000,
  83. Concurrency: 1,
  84. BufferLength: 1024,
  85. CheckpointInterval: 300000, //5 minutes
  86. SendError: true,
  87. },
  88. }
  89. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  90. if err != nil {
  91. Log.Fatal(err)
  92. panic(err)
  93. }
  94. Config = &kc
  95. if 0 == len(Config.Basic.Ip) {
  96. Config.Basic.Ip = "0.0.0.0"
  97. }
  98. if 0 == len(Config.Basic.RestIp) {
  99. Config.Basic.RestIp = "0.0.0.0"
  100. }
  101. if Config.Basic.Debug {
  102. Log.SetLevel(logrus.DebugLevel)
  103. }
  104. if Config.Basic.FileLog {
  105. logDir, err := GetLoc(logDir)
  106. if err != nil {
  107. Log.Fatal(err)
  108. }
  109. file := path.Join(logDir, logFileName)
  110. logWriter, err := rotatelogs.New(
  111. file+".%Y-%m-%d_%H-%M-%S",
  112. rotatelogs.WithLinkName(file),
  113. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  114. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  115. )
  116. if err != nil {
  117. fmt.Println("Failed to init log file settings..." + err.Error())
  118. Log.Infof("Failed to log to file, using default stderr.")
  119. } else if Config.Basic.ConsoleLog {
  120. mw := io.MultiWriter(os.Stdout, logWriter)
  121. Log.SetOutput(mw)
  122. } else if !Config.Basic.ConsoleLog {
  123. Log.SetOutput(logWriter)
  124. }
  125. } else if Config.Basic.ConsoleLog {
  126. Log.SetOutput(os.Stdout)
  127. }
  128. if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" {
  129. if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil {
  130. Log.Fatal(err)
  131. }
  132. }
  133. if Config.Portable.PythonBin == "" {
  134. Config.Portable.PythonBin = "python"
  135. }
  136. }
  137. func init() {
  138. InitLogger()
  139. InitClock()
  140. }