feat 初始化
This commit is contained in:
@@ -18,7 +18,8 @@ type MetricConfig struct {
|
||||
Port int32 `yaml:"port"`
|
||||
} `yaml:"prometheus"`
|
||||
Jaeger *struct {
|
||||
Endpoint string `yaml:"endpoint"`
|
||||
Endpoint string `yaml:"endpoint"`
|
||||
SamplingRate float64 `yaml:"samplingRate"`
|
||||
} `yaml:"jaeger"`
|
||||
}
|
||||
|
||||
@@ -76,3 +77,14 @@ type AddressConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
}
|
||||
|
||||
type CosConfig struct {
|
||||
SecretID string `yaml:"secretID"`
|
||||
SecretKey string `yaml:"secretKey"`
|
||||
BucketURL string `yaml:"bucketURL"`
|
||||
}
|
||||
|
||||
type SensitiveWordConfig struct {
|
||||
SourceType string `yaml:"sourceType"`
|
||||
Source string `yaml:"sourceURL"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user