conf.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. }
  51. Rule api.RuleOption
  52. Sink struct {
  53. CacheThreshold int `yaml:"cacheThreshold"`
  54. CacheTriggerCount int `yaml:"cacheTriggerCount"`
  55. DisableCache bool `yaml:"disableCache"`
  56. }
  57. Store struct {
  58. Type string `yaml:"type"`
  59. Redis struct {
  60. Host string `yaml:"host"`
  61. Port int `yaml:"port"`
  62. Password string `yaml:"password"`
  63. Timeout int `yaml:"timeout"`
  64. }
  65. Sqlite struct {
  66. Name string `yaml:"name"`
  67. }
  68. }
  69. Portable struct {
  70. PythonBin string `yaml:"pythonBin"`
  71. }
  72. }
  73. func InitConf() {
  74. cpath, err := GetConfLoc()
  75. if err != nil {
  76. panic(err)
  77. }
  78. kc := KuiperConf{
  79. Rule: api.RuleOption{
  80. LateTol: 1000,
  81. Concurrency: 1,
  82. BufferLength: 1024,
  83. CheckpointInterval: 300000, //5 minutes
  84. SendError: true,
  85. },
  86. }
  87. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  88. if err != nil {
  89. Log.Fatal(err)
  90. panic(err)
  91. }
  92. Config = &kc
  93. if 0 == len(Config.Basic.Ip) {
  94. Config.Basic.Ip = "0.0.0.0"
  95. }
  96. if 0 == len(Config.Basic.RestIp) {
  97. Config.Basic.RestIp = "0.0.0.0"
  98. }
  99. if Config.Basic.Debug {
  100. Log.SetLevel(logrus.DebugLevel)
  101. }
  102. if Config.Basic.FileLog {
  103. logDir, err := GetLoc(logDir)
  104. if err != nil {
  105. Log.Fatal(err)
  106. }
  107. file := path.Join(logDir, logFileName)
  108. logWriter, err := rotatelogs.New(
  109. file+".%Y-%m-%d_%H-%M-%S",
  110. rotatelogs.WithLinkName(file),
  111. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  112. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  113. )
  114. if err != nil {
  115. fmt.Println("Failed to init log file settings..." + err.Error())
  116. Log.Infof("Failed to log to file, using default stderr.")
  117. } else if Config.Basic.ConsoleLog {
  118. mw := io.MultiWriter(os.Stdout, logWriter)
  119. Log.SetOutput(mw)
  120. } else if !Config.Basic.ConsoleLog {
  121. Log.SetOutput(logWriter)
  122. }
  123. } else if Config.Basic.ConsoleLog {
  124. Log.SetOutput(os.Stdout)
  125. }
  126. }
  127. func init() {
  128. InitLogger()
  129. InitClock()
  130. }