diff --git a/conf/config.go b/conf/config.go new file mode 100644 index 0000000000000000000000000000000000000000..53857a63e689d57d7c93a2e53682009462d18521 --- /dev/null +++ b/conf/config.go @@ -0,0 +1,63 @@ +package conf + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + "runtime" + + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" + "gopkg.in/yaml.v3" +) + +type GrafanaConf struct { + Addr string `yaml:"http_addr"` +} + +type PilotGoConf struct { + Addr string `yaml:"http_addr"` +} + +type ServerConfig struct { + Grafana *GrafanaConf `yaml:"grafana"` + PilotGo *PilotGoConf `yaml:"pilotgo"` + Logopts *logger.LogOpts `yaml:"log"` +} + +func config_file() string { + _, thisfilepath, _, _ := runtime.Caller(0) + dirpath := filepath.Dir(thisfilepath) + configfilepath := path.Join(dirpath, "..", "config.yaml") + return configfilepath +} + +var global_config ServerConfig + +func init() { + err := readConfig(config_file(), &global_config) + if err != nil { + fmt.Printf("") + os.Exit(-1) + } +} + +func Config() *ServerConfig { + return &global_config +} + +func readConfig(file string, config interface{}) error { + bytes, err := ioutil.ReadFile(file) + if err != nil { + fmt.Printf("open %s failed! err = %s\n", file, err.Error()) + return err + } + + err = yaml.Unmarshal(bytes, config) + if err != nil { + fmt.Printf("yaml Unmarshal %s failed!\n", string(bytes)) + return err + } + return nil +} diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f7634360ae67b6de7aab24730d218822d41c81c6 --- /dev/null +++ b/config.yaml @@ -0,0 +1,10 @@ +grafana: + http_addr: "0.0.0.0:9999" +pilotgo: + http_addr: "192.168.75.100:8887" +log: + level: debug + driver: stdout + path: /var/log/grafana.log + max_file: 1 + max_size: 10485760 \ No newline at end of file diff --git a/main.go b/main.go index 156b77d35ede6d084ddd034e463998806421c48c..3fb215db913d825f88dc39822d9329724dcf7d24 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client" "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/grafana-plugin/conf" ) const Version = "0.0.1" @@ -35,22 +36,13 @@ func main() { client.RegisterHandlers(server) InitRouter(server) - if err := server.Run("0.0.0.0:9999"); err != nil { + if err := server.Run(conf.Config().Grafana.Addr); err != nil { logger.Fatal("failed to run server") } } func InitLogger() { - // TODO: use config in file - conf := &logger.LogOpts{ - Level: "debug", - Driver: "stdout", - Path: "./log", - MaxFile: 10, - MaxSize: 1024 * 1024 * 30, - } - - if err := logger.Init(conf); err != nil { + if err := logger.Init(conf.Config().Logopts); err != nil { fmt.Printf("logger init failed, please check the config file: %s", err) os.Exit(-1) }