feat 初次提交
This commit is contained in:
68
config/config.go
Normal file
68
config/config.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
type AppConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Debug bool `yaml:"debug"`
|
||||
MaxSize int32 `yaml:"maxSize"`
|
||||
MaxBackups int32 `yaml:"maxBackups"`
|
||||
MaxAge int32 `yaml:"maxAge"`
|
||||
Level string `yaml:"level"`
|
||||
}
|
||||
|
||||
type MetricConfig struct {
|
||||
Prometheus *struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
} `yaml:"prometheus"`
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
Etcd *EtcdConfig `yaml:"etcd"`
|
||||
MySQL map[string]*MySQLConfig `yaml:"mysql"`
|
||||
Mongo map[string]*MongoConfig `yaml:"mongo"`
|
||||
Redis *RedisConfig `yaml:"redis"`
|
||||
}
|
||||
|
||||
type EtcdConfig struct {
|
||||
Endpoints []string `yaml:"endpoints"`
|
||||
}
|
||||
|
||||
type MySQLConfig struct {
|
||||
Dsn string `yaml:"dsn"`
|
||||
MaxOpenConn int32 `yaml:"maxOpenConn"`
|
||||
MaxIdleConn int32 `yaml:"maxIdleConn"`
|
||||
ConnMaxLifetimeSec int32 `yaml:"connMaxLifetimeSec"`
|
||||
ConnMaxIdleTimeSec int32 `yaml:"connMaxIdleTimeSec"`
|
||||
LogLevel string `yaml:"logLevel"`
|
||||
}
|
||||
|
||||
type MongoConfig struct {
|
||||
URI string `yaml:"uri"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Addr string `yaml:"addr"`
|
||||
Password string `yaml:"password"`
|
||||
DB int `yaml:"db"`
|
||||
}
|
||||
|
||||
type ServeConfig struct {
|
||||
Grpc *struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
TTL int64 `yaml:"ttl"`
|
||||
} `yaml:"grpc"`
|
||||
Socket *struct {
|
||||
Web *AddressConfig `yaml:"web"`
|
||||
Raw *AddressConfig `yaml:"raw"`
|
||||
} `yaml:"socket"`
|
||||
Http *AddressConfig `yaml:"http"`
|
||||
}
|
||||
|
||||
type AddressConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
}
|
||||
42
config/loader.go
Normal file
42
config/loader.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
envConfigPrefix = "XH_G"
|
||||
)
|
||||
|
||||
// LoadConfig 加载并返回应用配置
|
||||
func LoadConfig[T any](configDir string, configPtr *T) (*T, error) {
|
||||
v := viper.New()
|
||||
|
||||
v.SetEnvPrefix(envConfigPrefix)
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
||||
env := v.GetString("env")
|
||||
if env == "" {
|
||||
env = "dev"
|
||||
}
|
||||
|
||||
v.SetConfigName(fmt.Sprintf("config.%s", strings.ToLower(env)))
|
||||
v.AddConfigPath(configDir)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read config: %w", err)
|
||||
}
|
||||
|
||||
if err := v.Unmarshal(&configPtr); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
|
||||
}
|
||||
|
||||
marshal, _ := json.Marshal(configPtr)
|
||||
fmt.Printf("Configuration loading completed: %v\n", string(marshal))
|
||||
return configPtr, nil
|
||||
}
|
||||
Reference in New Issue
Block a user