From 52ba0c45d0dee7a6f45cdc0a9fc3cce77af6721d Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Fri, 14 Jun 2024 17:11:27 +0800 Subject: [PATCH] template: add conf module --- template/server/conf/config.go | 53 ++++++++++++++++++++++++++++++++++ template/server/conf/meta.go | 12 ++++++++ template/server/template.yml | 13 +++++++++ 3 files changed, 78 insertions(+) create mode 100644 template/server/conf/config.go create mode 100644 template/server/conf/meta.go diff --git a/template/server/conf/config.go b/template/server/conf/config.go new file mode 100644 index 00000000..34551251 --- /dev/null +++ b/template/server/conf/config.go @@ -0,0 +1,53 @@ +package conf + +import ( + "flag" + "fmt" + "os" + "path" + + "gitee.com/openeuler/PilotGo-plugin-template/global" + "gitee.com/openeuler/PilotGo/sdk/logger" + "gopkg.in/yaml.v2" +) + +var Global_Config *ServerConfig + +const config_type = "template.yaml" + +var config_dir string + +type ServerConfig struct { + Template *TemplateConf + PilotGo *PilotGoConf + Logopts *logger.LogOpts `yaml:"log"` +} + +func ConfigFile() string { + configfilepath := path.Join(config_dir, config_type) + + return configfilepath +} + +func InitConfig() { + flag.StringVar(&config_dir, "conf", "/opt/PilotGo/plugin/template", "template configuration directory") + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s -conf /path/to/template.yaml(default:/opt/PilotGo/plugin/template) \n", os.Args[0]) + } + flag.Parse() + + bytes, err := global.FileReadBytes(ConfigFile()) + if err != nil { + flag.Usage() + fmt.Printf("open file failed: %s, %s", ConfigFile(), err.Error()) + os.Exit(1) + } + + Global_Config = &ServerConfig{} + + err = yaml.Unmarshal(bytes, Global_Config) + if err != nil { + fmt.Printf("yaml unmarshal failed: %s", err.Error()) + os.Exit(1) + } +} diff --git a/template/server/conf/meta.go b/template/server/conf/meta.go new file mode 100644 index 00000000..0c4dc303 --- /dev/null +++ b/template/server/conf/meta.go @@ -0,0 +1,12 @@ +package conf + +type TemplateConf struct { + Https_enabled bool `yaml:"https_enabled"` + CertFile string `yaml:"cert_file"` + KeyFile string `yaml:"key_file"` + Addr string `yaml:"addr"` +} + +type PilotGoConf struct { + Addr string `yaml:"addr"` +} diff --git a/template/server/template.yml b/template/server/template.yml index e69de29b..681de3e6 100644 --- a/template/server/template.yml +++ b/template/server/template.yml @@ -0,0 +1,13 @@ +template: + https_enabled: false + cert_file: "" + key_file: "" + addr: "0.0.0.0:49151" +PilotGo: + addr: "localhost:8888" +log: + level: debug + driver: file # 可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 + path: /opt/PilotGo/plugin/elk/log/elk.log + max_file: 1 + max_size: 10485760 \ No newline at end of file -- Gitee