conf.go 3.7 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. "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. Path string `yaml:"path"`
  80. Name string `yaml:"name"`
  81. }
  82. }
  83. }
  84. func InitConf() {
  85. b, err := LoadConf(StreamConf)
  86. if err != nil {
  87. Log.Fatal(err)
  88. }
  89. kc := KuiperConf{
  90. Rule: api.RuleOption{
  91. LateTol: 1000,
  92. Concurrency: 1,
  93. BufferLength: 1024,
  94. CheckpointInterval: 300000, //5 minutes
  95. SendError: true,
  96. },
  97. }
  98. if err := yaml.Unmarshal(b, &kc); err != nil {
  99. Log.Fatal(err)
  100. } else {
  101. Config = &kc
  102. }
  103. if 0 == len(Config.Basic.Ip) {
  104. Config.Basic.Ip = "0.0.0.0"
  105. }
  106. if 0 == len(Config.Basic.RestIp) {
  107. Config.Basic.RestIp = "0.0.0.0"
  108. }
  109. if Config.Basic.Debug {
  110. Log.SetLevel(logrus.DebugLevel)
  111. }
  112. if Config.Basic.FileLog {
  113. logDir, err := GetLoc(logDir)
  114. if err != nil {
  115. Log.Fatal(err)
  116. }
  117. file := path.Join(logDir, logFileName)
  118. logWriter, err := rotatelogs.New(
  119. file+".%Y-%m-%d_%H-%M-%S",
  120. rotatelogs.WithLinkName(file),
  121. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  122. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  123. )
  124. if err != nil {
  125. fmt.Println("Failed to init log file settings..." + err.Error())
  126. Log.Infof("Failed to log to file, using default stderr.")
  127. } else if Config.Basic.ConsoleLog {
  128. mw := io.MultiWriter(os.Stdout, logWriter)
  129. Log.SetOutput(mw)
  130. } else if !Config.Basic.ConsoleLog {
  131. Log.SetOutput(logWriter)
  132. }
  133. } else if Config.Basic.ConsoleLog {
  134. Log.SetOutput(os.Stdout)
  135. }
  136. }
  137. func init() {
  138. InitLogger()
  139. InitClock()
  140. }