This commit is contained in:
oneao committed 2026-04-01 21:51:18 +08:00
1 parent 4ef00e1d5d
commit 9e00adf2fe
27 files changed
+1104

No files matched your search

@@ -0,0 +1,103 @@
package config
import (
"fmt"
"os"
"strings"
"github.com/spf13/viper"
)
func Load() (*Config, error) {
env := getEnv("APP_ENV", "dev")
v := viper.New()
v.SetConfigType("yaml")
// 配置路径
v.AddConfigPath("configs")
// ======================
// 1️⃣ 基础配置
// ======================
if err := loadBase(v); err != nil {
return nil, err
}
// ======================
// 2️⃣ 环境配置(支持多环境)
// ======================
loadByEnv(v, env)
// ======================
// 3️⃣ 环境变量覆盖
// ======================
bindEnv(v)
// ======================
// 4️⃣ 默认值
// ======================
setDefaults(v, env)
// ======================
// 5️⃣ 解析
// ======================
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("decode config failed: %w", err)
}
return &cfg, nil
}
func loadBase(v *viper.Viper) error {
v.SetConfigName("config")
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("read base config failed: %w", err)
}
return nil
}
func loadByEnv(v *viper.Viper, env string) {
envs := strings.Split(env, ",")
for _, e := range envs {
e = strings.TrimSpace(e)
if e == "" {
continue
}
v.SetConfigName("config." + e)
if err := v.MergeInConfig(); err != nil {
fmt.Printf("⚠️ config.%s.yaml not found\n", e)
}
}
}
func bindEnv(v *viper.Viper) {
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
func setDefaults(v *viper.Viper, env string) {
v.SetDefault("app.env", env)
v.SetDefault("app.name", "golang")
v.SetDefault("server.port", "8080")
v.SetDefault("unique_id.datacenter_id", 1)
v.SetDefault("unique_id.worker_id", 1)
v.SetDefault("jwt.secret", "abcdedhaldkasdlkasd")
v.SetDefault("jwt.access_expiry", -1)
}
func getEnv(key, def string) string {
if val := os.Getenv(key); val != "" {
return val
}
return def
}
@@ -0,0 +1,54 @@
package config
import "time"
type Config struct {
App AppConfig `mapstructure:"app"`
Server ServerConfig `mapstructure:"server"`
JWT JwtConfig `mapstructure:"jwt"`
UniqueID UniqueIDConfig `mapstructure:"unique_id"`
Postgres PostgresConfig `mapstructure:"postgres"`
Wechat WechatConfig `mapstructure:"wechat"`
Redis RedisConfig `mapstructure:"redis"`
}
type AppConfig struct {
Name string `mapstructure:"name"`
Env string `mapstructure:"env"`
}
type ServerConfig struct {
Port string `mapstructure:"port"`
}
type JwtConfig struct {
Secret string `mapstructure:"secret"`
AccessExpiry time.Duration `mapstructure:"access_expiry"`
}
type UniqueIDConfig struct {
DataCenterID uint16 `mapstructure:"datacenter_id"`
WorkerID uint16 `mapstructure:"worker_id"`
}
type WechatConfig struct {
AppId string `mapstructure:"app_id"`
AppSecret string `mapstructure:"app_secret"`
}
type PostgresConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Dbname string `mapstructure:"dbname"`
SslMode bool `mapstructure:"ssl_mode"`
TimeZone string `mapstructure:"timezone"`
}
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Db int `mapstructure:"db"`
Password string `mapstructure:"password"`
}