Skip to content

Commit

Permalink
fix: add absolute path for config
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 committed Oct 6, 2023
1 parent c119149 commit b9bc136
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
32 changes: 27 additions & 5 deletions tpl/hertz/server/standard/layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -142,7 +145,6 @@ layouts:
DSN string `yaml:"dsn"`
}
type Redis struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -222,7 +246,6 @@ layouts:
}
}
- path: conf/dev/conf.yaml
delims:
- ""
Expand Down Expand Up @@ -298,7 +321,6 @@ layouts:
password: ""
db: 0
- path: biz/dal/init.go
delims:
- ""
Expand Down
29 changes: 27 additions & 2 deletions tpl/kitex/server/standard/conf_tpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit b9bc136

Please sign in to comment.