conf.go 3.4 KB

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