1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package common
- import (
- "encoding/json"
- "fmt"
- "go.uber.org/zap/zapcore"
- "io"
- "net/http"
- "net/url"
- "sync"
- )
- type Config struct {
- Service `json:"service"`
- Mysql `json:"mysql"`
- Redis
- Log
- }
- type Log struct {
- Type string
- Level zapcore.Level
- }
- type Service struct {
- IsDebug bool
- Httpport string
- UseLogger bool
- }
- type Redis struct {
- Addr string
- Password string
- Database int
- }
- type Mysql struct {
- Host string `json:"host"`
- Database string `json:"database"`
- User string `json:"user"`
- Password string `json:"password"`
- Maxidleconns int `json:"maxidleconns"`
- Maxopenconns int `json:"max_open_conns"`
- }
- var c *Config
- var cOnce sync.Once
- func InitConfig() {
- type remoteConfig struct {
- Data struct {
- Name string `json:"name"`
- EndAt int `json:"end_at"`
- Config *Config `json:"config"`
- } `json:"data"`
- }
- rConfig := &remoteConfig{}
- resp, err := http.PostForm(fmt.Sprintf("http://kconf.kphcdr.com/api/config/all?app_key=%s&env=%s", APP_KEY, Env), url.Values{})
- if err != nil {
- panic(fmt.Sprintf("拉取配置失败:%s", err))
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- panic(fmt.Sprintf("读取配置失败:%s", err))
- }
- if err := json.Unmarshal(body, &rConfig); err != nil {
- panic(err)
- }
- c = rConfig.Data.Config
- }
- func GetConfig() *Config {
- cOnce.Do(func() {
- InitConfig()
- })
- return c
- }
|