diff --git a/tpl/hertz/server/standard/layout.yaml b/tpl/hertz/server/standard/layout.yaml index 96b07481..e21589cd 100644 --- a/tpl/hertz/server/standard/layout.yaml +++ b/tpl/hertz/server/standard/layout.yaml @@ -114,11 +114,14 @@ layouts: package conf import ( + "bytes" "io/ioutil" "os" + "os/exec" "path/filepath" "sync" + "github.com/bytedance/sonic" "github.com/cloudwego/hertz/pkg/common/hlog" "github.com/kr/pretty" "gopkg.in/validator.v2" @@ -142,7 +145,6 @@ layouts: DSN string `yaml:"dsn"` } - type Redis struct { Address string `yaml:"address"` Password string `yaml:"password"` @@ -161,6 +163,10 @@ layouts: LogMaxBackups int `yaml:"log_max_backups"` LogMaxAge int `yaml:"log_max_age"` } + + type BindMainDir struct { + Dir string `json:"Dir"` + } // GetConf gets configuration instance func GetConf() *Config { @@ -169,8 +175,7 @@ layouts: } func initConf() { - prefix := "conf" - confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) + confFileRelPath := getConfAbsPath() content, err := ioutil.ReadFile(confFileRelPath) if err != nil { panic(err) @@ -191,7 +196,26 @@ layouts: pretty.Printf("%+v\n", conf) } + + func getConfAbsPath() string { + cmd := exec.Command("go", "list", "-m", "-json") + + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + if err := cmd.Run(); err != nil { + panic(err) + } + + bindDir := &BindMainDir{} + if err := sonic.Unmarshal(out.Bytes(), bindDir); err != nil { + panic(err) + } + prefix := "conf" + return filepath.Join(bindDir.Dir, prefix, filepath.Join(GetEnv(), "conf.yaml")) + } + func GetEnv() string { e := os.Getenv("GO_ENV") if len(e) == 0 { @@ -222,7 +246,6 @@ layouts: } } - - path: conf/dev/conf.yaml delims: - "" @@ -298,7 +321,6 @@ layouts: password: "" db: 0 - - path: biz/dal/init.go delims: - "" diff --git a/tpl/kitex/server/standard/conf_tpl.yaml b/tpl/kitex/server/standard/conf_tpl.yaml index 9f06fd7f..464fbd38 100644 --- a/tpl/kitex/server/standard/conf_tpl.yaml +++ b/tpl/kitex/server/standard/conf_tpl.yaml @@ -5,14 +5,17 @@ body: |- package conf import ( + "bytes" "io/ioutil" "os" + "os/exec" "path/filepath" "sync" "gopkg.in/validator.v2" "gopkg.in/yaml.v2" + "github.com/bytedance/sonic" "github.com/cloudwego/kitex/pkg/klog" "github.com/kr/pretty" ) @@ -59,6 +62,10 @@ body: |- Username string `yaml:"username"` Password string `yaml:"password"` } + + type BindMainDir struct { + Dir string `json:"Dir"` + } // GetConf gets configuration instance func GetConf() *Config { @@ -67,8 +74,7 @@ body: |- } func initConf() { - prefix := "conf" - confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) + confFileRelPath := getConfAbsPath() content, err := ioutil.ReadFile(confFileRelPath) if err != nil { panic(err) @@ -86,6 +92,25 @@ body: |- conf.Env = GetEnv() pretty.Printf("%+v\n", conf) } + + func getConfAbsPath() string { + cmd := exec.Command("go", "list", "-m", "-json") + + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + if err := cmd.Run(); err != nil { + panic(err) + } + + bindDir := &BindMainDir{} + if err := sonic.Unmarshal(out.Bytes(), bindDir); err != nil { + panic(err) + } + + prefix := "conf" + return filepath.Join(bindDir.Dir, prefix, filepath.Join(GetEnv(), "conf.yaml")) + } func GetEnv() string { e := os.Getenv("GO_ENV")