Skip to content

Commit

Permalink
add: GetRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-home committed Apr 19, 2024
1 parent 0ca7609 commit ad6840a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ func StringToHump(s string) string {
}
return string(data[:])
}

func GetRoot() string {
return app.GetBean("config").(app.GetRoot).GetRoot()
}
29 changes: 29 additions & 0 deletions bootstrap/providers/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,32 @@ func (c *ConfigProvider) GetBean(alias string) interface{} {

return &aliasDef
}

var ROOT string = ""

func (c *ConfigProvider) GetRoot() string {
if ROOT != "" {
return ROOT
}

pwd, _ := os.Getwd()
parDir := ""

if defaultConfigDir == nil {
// 单元测试中, 可能未初始化框架, 从本目录开始往上查找go.mod文件确定跟目录
for i := 0; i <= 100; i++ {
checkDir := pwd + parDir
_, err1 := os.Stat(checkDir + "/go.mod")
_, err2 := os.Stat(checkDir + "/.env")
if err1 == nil || err2 == nil {
parDir = checkDir
break
}
parDir += "/.."
}
} else {
parDir = pwd
}
ROOT = parDir
return parDir
}
4 changes: 4 additions & 0 deletions bootstrap/services/app/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type AppendRun interface {
AppendRun(fun func())
}

type GetRoot interface {
GetRoot() string
}

// GetBean 只能返回指针的值
func GetBean(alias string) interface{} {
arr := strings.Split(alias, ", ")
Expand Down

0 comments on commit ad6840a

Please sign in to comment.