Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add absolute path for config #84

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
"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