2025-02-07 13:01:38 +08:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2025-02-07 20:33:27 +08:00
|
|
|
"path/filepath"
|
2025-02-07 13:35:31 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/types"
|
|
|
|
"git.apinb.com/bsm-sdk/core/utils"
|
2025-02-07 13:01:38 +08:00
|
|
|
)
|
|
|
|
|
2025-02-07 20:33:27 +08:00
|
|
|
var Runtime *types.RuntimeEnv = nil
|
2025-02-07 13:35:31 +08:00
|
|
|
|
|
|
|
// get system env.
|
2025-02-07 20:33:27 +08:00
|
|
|
func NewEnv() *types.RuntimeEnv {
|
|
|
|
if Runtime == nil {
|
|
|
|
Runtime = &types.RuntimeEnv{
|
|
|
|
Workspace: GetEnvDefault("BSM_Workspace", "def"),
|
|
|
|
JwtSecretKey: GetEnvDefault("BSM_JwtSecretKey", "Cblocksmesh2022C"),
|
|
|
|
Mode: strings.ToLower(GetEnvDefault("BSM_RuntimeMode", "dev")),
|
|
|
|
LicencePath: strings.ToLower(GetEnvDefault("BSM_Licence", "")),
|
2025-02-07 13:35:31 +08:00
|
|
|
}
|
|
|
|
|
2025-02-07 20:33:27 +08:00
|
|
|
if Runtime.Mode == "dev" {
|
|
|
|
Runtime.Prefix = GetEnvDefault("BSM_Prefix", utils.GetCurrentPath())
|
2025-02-07 13:35:31 +08:00
|
|
|
} else {
|
2025-02-07 20:33:27 +08:00
|
|
|
Runtime.Prefix = GetEnvDefault("BSM_Prefix", "/usr/local/bsm")
|
2025-02-07 13:35:31 +08:00
|
|
|
}
|
|
|
|
|
2025-02-07 20:33:27 +08:00
|
|
|
if Runtime.LicencePath == "" {
|
|
|
|
Runtime.LicencePath = filepath.Join(Runtime.Prefix, "etc")
|
2025-02-07 13:35:31 +08:00
|
|
|
}
|
2025-02-07 20:33:27 +08:00
|
|
|
}
|
|
|
|
return Runtime
|
|
|
|
}
|
2025-02-07 13:35:31 +08:00
|
|
|
|
2025-02-07 20:33:27 +08:00
|
|
|
func GetEnvDefault(key string, def string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return def
|
2025-02-07 13:35:31 +08:00
|
|
|
}
|
2025-02-07 20:33:27 +08:00
|
|
|
return value
|
2025-02-07 13:35:31 +08:00
|
|
|
}
|