package conf import ( "fmt" "github.com/emqx/kuiper/pkg/api" "github.com/lestrrat-go/file-rotatelogs" "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "io" "io/ioutil" "os" "path" "time" ) const StreamConf = "kuiper.yaml" var ( Config *KuiperConf IsTesting bool ) func LoadConf(confName string) ([]byte, error) { confDir, err := GetConfLoc() if err != nil { return nil, err } file := path.Join(confDir, confName) b, err := ioutil.ReadFile(file) if err != nil { return nil, err } return b, nil } type tlsConf struct { Certfile string `yaml:"certfile"` Keyfile string `yaml:"keyfile"` } type KuiperConf struct { Basic struct { Debug bool `yaml:"debug"` ConsoleLog bool `yaml:"consoleLog"` FileLog bool `yaml:"fileLog"` RotateTime int `yaml:"rotateTime"` MaxAge int `yaml:"maxAge"` Ip string `yaml:"ip"` Port int `yaml:"port"` RestIp string `yaml:"restIp"` RestPort int `yaml:"restPort"` RestTls *tlsConf `yaml:"restTls"` Prometheus bool `yaml:"prometheus"` PrometheusPort int `yaml:"prometheusPort"` PluginHosts string `yaml:"pluginHosts"` } Rule api.RuleOption Sink struct { CacheThreshold int `yaml:"cacheThreshold"` CacheTriggerCount int `yaml:"cacheTriggerCount"` DisableCache bool `yaml:"disableCache""` } } func InitConf() { b, err := LoadConf(StreamConf) if err != nil { Log.Fatal(err) } kc := KuiperConf{ Rule: api.RuleOption{ LateTol: 1000, Concurrency: 1, BufferLength: 1024, CheckpointInterval: 300000, //5 minutes SendError: true, }, } if err := yaml.Unmarshal(b, &kc); err != nil { Log.Fatal(err) } else { Config = &kc } if 0 == len(Config.Basic.Ip) { Config.Basic.Ip = "0.0.0.0" } if 0 == len(Config.Basic.RestIp) { Config.Basic.RestIp = "0.0.0.0" } if Config.Basic.Debug { Log.SetLevel(logrus.DebugLevel) } if Config.Basic.FileLog { logDir, err := GetLoc(logDir) if err != nil { Log.Fatal(err) } file := path.Join(logDir, logFileName) logWriter, err := rotatelogs.New( file+".%Y-%m-%d_%H-%M-%S", rotatelogs.WithLinkName(file), rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)), rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)), ) if err != nil { fmt.Println("Failed to init log file settings..." + err.Error()) Log.Infof("Failed to log to file, using default stderr.") } else if Config.Basic.ConsoleLog { mw := io.MultiWriter(os.Stdout, logWriter) Log.SetOutput(mw) } else if !Config.Basic.ConsoleLog { Log.SetOutput(logWriter) } } else if Config.Basic.ConsoleLog { Log.SetOutput(os.Stdout) } } func init() { InitLogger() InitClock() }