feat 初始化

This commit is contained in:
2026-10-29 10:12:22 +08:00
parent 332d812347
commit 09c1df8a22
81 changed files with 2211 additions and 7821 deletions

View File

@@ -4,13 +4,30 @@ import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
"os"
"strings"
"sync"
)
const (
envConfigPrefix = "XH_G"
)
var (
currentEnv EnvType // 当前环境变量
currentEnvOnce sync.Once
currentCommit string // 当前提交的CommitID
currentCommitOnce sync.Once
)
type EnvType string
const (
EnvDev EnvType = "dev"
EnvTest EnvType = "test"
EnvProd EnvType = "prod"
)
// LoadConfig 加载并返回应用配置
func LoadConfig[T any](configDir string, configPtr *T) (*T, error) {
v := viper.New()
@@ -40,3 +57,27 @@ func LoadConfig[T any](configDir string, configPtr *T) (*T, error) {
fmt.Printf("Configuration loading completed: %v\n", string(marshal))
return configPtr, nil
}
// Env 获取环境
func Env() EnvType {
currentEnvOnce.Do(func() {
if v, ok := os.LookupEnv(envConfigPrefix + "_ENV"); ok {
currentEnv = EnvType(v)
} else {
currentEnv = EnvDev
}
})
return currentEnv
}
// Commit 获取提交ID
func Commit() string {
currentCommitOnce.Do(func() {
if v, ok := os.LookupEnv(envConfigPrefix + "_COMMIT"); ok {
currentCommit = v
} else {
currentCommit = "unknown"
}
})
return currentCommit
}