From 279eb69ba8fde0e7b63e87afa20c5ab42cad4933 Mon Sep 17 00:00:00 2001 From: guozhengxin Date: Thu, 31 Aug 2023 18:25:21 +0800 Subject: [PATCH] add http server config --- .gitignore | 2 + conf/config.go | 125 ++++++++++++++-------------- config.yaml => config.yaml.templete | 20 +++-- main.go | 14 +++- 4 files changed, 87 insertions(+), 74 deletions(-) rename config.yaml => config.yaml.templete (43%) diff --git a/.gitignore b/.gitignore index b88feea..2ac1556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ # config file config.yml +config.yaml # bin files server *.exe +grafana .vscode diff --git a/conf/config.go b/conf/config.go index 53857a6..4539bfa 100644 --- a/conf/config.go +++ b/conf/config.go @@ -1,63 +1,62 @@ -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 -} +package conf + +import ( + "fmt" + "io/ioutil" + "os" + + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" + "gopkg.in/yaml.v3" +) + +type HttpConfig struct { + Addr string `json:"addr"` +} + +type GrafanaConfig struct { + Addr string `yaml:"addr"` +} + +type PilotGoConfig struct { + Addr string `yaml:"http_addr"` +} + +type ServerConfig struct { + Http *HttpConfig `yaml:"http_server"` + Grafana *GrafanaConfig `yaml:"grafana_server"` + PilotGo *PilotGoConfig `yaml:"pilotgo_server"` + Logopts *logger.LogOpts `yaml:"log"` +} + +func config_file() string { + return "./config.yaml" +} + +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.templete similarity index 43% rename from config.yaml rename to config.yaml.templete index f763436..0923cc1 100644 --- a/config.yaml +++ b/config.yaml.templete @@ -1,10 +1,12 @@ -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 +http_server: + addr: "192.168.48.169:9999" +grafana_server: + addr: "192.168.48.169:3000" +pilotgo_server: + addr: "192.168.48.169: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 3fb215d..d379b5d 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,8 @@ var PluginInfo = &client.PluginInfo{ Description: "grafana可视化工具支持", Author: "guozhengxin", Email: "guozhengxin@kylinos.cn", - Url: "http://192.168.75.100:9999/plugin/grafana", - ReverseDest: "http://192.168.75.100:3000/", + // Url: "http://192.168.48.169:9999/plugin/grafana", + // ReverseDest: "http://192.168.48.169:3000/", } func main() { @@ -30,6 +30,9 @@ func main() { InitLogger() + PluginInfo.Url = "http://" + conf.Config().Http.Addr + "/plugin/grafana" + PluginInfo.ReverseDest = "http://" + conf.Config().Grafana.Addr + "/plugin/grafana" + server := gin.Default() client := client.DefaultClient(PluginInfo) @@ -74,5 +77,12 @@ func ReverseProxyHandler(c *gin.Context) { proxy := httputil.NewSingleHostReverseProxy(target) c.Request.URL.Path = strings.Replace(c.Request.URL.Path, "/plugin/grafana", "", 1) //请求API + // proxy.ModifyResponse = func(r *http.Response) error { + // if setCookie := r.Header.Get("Set-Cookie"); setCookie != "" { + // r.Header.Set("Set-Cookie", setCookie+" ; Secure ") + // } + // return nil + // } + proxy.ServeHTTP(c.Writer, c.Request) } -- Gitee