Skip to content

Commit

Permalink
Revert "fix: add absolute path for config (cloudwego#84)"
Browse files Browse the repository at this point in the history
This reverts commit 97b4535.
  • Loading branch information
rogerogers committed Nov 13, 2023
1 parent 44092d3 commit 6a5a542
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 56 deletions.
32 changes: 5 additions & 27 deletions tpl/hertz/server/standard/layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,11 @@ 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 @@ -146,6 +143,7 @@ layouts:
DSN string `yaml:"dsn"`
}
type Redis struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
Expand All @@ -164,10 +162,6 @@ 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 @@ -176,7 +170,8 @@ layouts:
}
func initConf() {
confFileRelPath := getConfAbsPath()
prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath)
if err != nil {
panic(err)
Expand All @@ -197,26 +192,7 @@ 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 @@ -247,6 +223,7 @@ layouts:
}
}
- path: conf/dev/conf.yaml
delims:
- ""
Expand Down Expand Up @@ -322,6 +299,7 @@ layouts:
password: ""
db: 0
- path: biz/dal/init.go
delims:
- ""
Expand Down
33 changes: 4 additions & 29 deletions tpl/kitex/server/standard/conf_tpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ body: |-
package conf
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"github.com/bytedance/sonic"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/kr/pretty"
"gopkg.in/validator.v2"
Expand All @@ -35,7 +32,7 @@ body: |-
type MySQL struct {
DSN string `yaml:"dsn"`
}
type Redis struct {
Address string `yaml:"address"`
Username string `yaml:"username"`
Expand All @@ -61,10 +58,6 @@ 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 @@ -73,7 +66,8 @@ body: |-
}
func initConf() {
confFileRelPath := getConfAbsPath()
prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath)
if err != nil {
panic(err)
Expand All @@ -91,25 +85,6 @@ 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 All @@ -118,7 +93,7 @@ body: |-
}
return e
}
func LogLevel() klog.Level {
level := GetConf().Kitex.LogLevel
switch level {
Expand Down

0 comments on commit 6a5a542

Please sign in to comment.