Ai
3 Star 0 Fork 0

neuro-netw0rk/server-golib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
env.go 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
LeoWang 提交于 2024-12-16 16:45 +08:00 . 整合golib和serverlib
package env
import (
"os"
"path/filepath"
"gitee.com/neuro-netw0rk/server-golib/utils"
"github.com/gin-gonic/gin"
)
const DefaultRootPath = "."
const LogLevel = "LOG_LEVEL"
const (
NNClusterType = "NN_CLUSTER_TYPE"
DockAppName = "APP_NAME"
DockerRunEnv = "RUN_ENV"
)
// RUN_ENV: (prod,tips,test)
const (
RunEnvDEV = 0
RunEnvQAT = 1
RunEnvPRE = 2
RunEnvPRD = 3
RunEnvDOC = 4
)
var (
LocalIP string
AppName string
RunMode string
ServerID string
runEnv int
rootPath string
dockerPlateForm bool
logLevel string
)
func init() {
LocalIP = utils.GetLocalIp()
dockerPlateForm = false
if r := os.Getenv(NNClusterType); r != "" {
dockerPlateForm = true
// 容器里,appName在编排的时候决定
if n := os.Getenv(DockAppName); n != "" {
AppName = n
println("docker env, APP_NAME=", n)
} else {
println("docker env, lack APP_NAME!!!")
}
}
// 运行环境
RunMode = gin.ReleaseMode
r := os.Getenv(DockerRunEnv)
switch r {
case "doc":
runEnv = RunEnvDOC
case "prd":
runEnv = RunEnvPRD
case "pre":
runEnv = RunEnvPRE
RunMode = gin.DebugMode
case "qat":
runEnv = RunEnvQAT
RunMode = gin.DebugMode
case "dev":
runEnv = RunEnvDEV
RunMode = gin.DebugMode
default:
runEnv = RunEnvDEV
RunMode = gin.DebugMode
}
gin.SetMode(RunMode)
initDBSecret()
logLevel = os.Getenv(LogLevel)
}
func IsDockerPlatform() bool {
return dockerPlateForm
}
func SetAppName(appName string) {
if !dockerPlateForm {
AppName = appName
}
}
func GetAppName() string {
return AppName
}
// SetRootPath 设置应用的根目录
func SetRootPath(r string) {
if !dockerPlateForm {
rootPath = r
}
}
func GetRootPath() string {
if rootPath != "" {
return rootPath
} else {
return DefaultRootPath
}
}
// GetConfDirPath 返回配置文件目录绝对地址
func GetConfDirPath() string {
return filepath.Join(GetRootPath(), "conf")
}
func GetLogDirPath() string {
return filepath.Join(GetRootPath(), "log")
}
func GetRunEnv() int {
return runEnv
}
func SetServerID(serverID string) {
ServerID = serverID
}
func GetServerID() string {
return ServerID
}
func GetLogLevel() string {
return logLevel
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/neuro-netw0rk/server-golib.git
git@gitee.com:neuro-netw0rk/server-golib.git
neuro-netw0rk
server-golib
server-golib
v0.0.5-beta2

搜索帮助