u
This commit is contained in:
1 parent
835958886e
commit
d04d22723b
155 files changed
+543
-9793
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
conf *Config
|
||||
)
|
||||
|
||||
func InitConf() {
|
||||
// 配置文件的目录
|
||||
prefix := "resources"
|
||||
env := GetEnv() // 获取当前环境
|
||||
|
||||
// 默认配置文件
|
||||
confFileRelPath := filepath.Join(prefix, "application.yaml")
|
||||
|
||||
// 创建 viper 实例
|
||||
v := viper.New()
|
||||
|
||||
// 首先加载默认的 application.yaml
|
||||
v.SetConfigFile(confFileRelPath)
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
// 读取默认配置文件
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 根据环境加载不同的配置文件
|
||||
switch env {
|
||||
case "dev":
|
||||
v.SetConfigFile(filepath.Join(prefix, "application-dev.yaml"))
|
||||
case "build":
|
||||
v.SetConfigFile(filepath.Join(prefix, "application-build.yaml"))
|
||||
}
|
||||
|
||||
// 如果需要,合并环境特定的配置文件
|
||||
if err := v.MergeInConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 将配置文件反序列化到 Config 结构体
|
||||
conf = new(Config)
|
||||
if err := v.Unmarshal(conf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("✅️ 加载配置成功")
|
||||
// 设置环境变量
|
||||
conf.Env = env
|
||||
}
|
||||
|
||||
// GetEnv 获取 GO_ENV 环境变量,默认返回 "dev"
|
||||
func GetEnv() string {
|
||||
e := os.Getenv("GO_ENV")
|
||||
if len(e) == 0 {
|
||||
// 如果没有设置 GO_ENV,使用默认的 "dev" 配置
|
||||
return "dev"
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func GetConf() *Config {
|
||||
return conf
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package config
|
||||
@@ -0,0 +1 @@
|
||||
package config
|
||||
@@ -0,0 +1 @@
|
||||
package config
|
||||
@@ -0,0 +1,57 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Config struct {
|
||||
Env string
|
||||
|
||||
Server ServerConfig `yaml:"server"`
|
||||
JWT JwtConfig `yaml:"jwt"`
|
||||
SnowID SnowIDConfig `yaml:"snow_id"`
|
||||
Postgres PostgresConfig `yaml:"postgres"`
|
||||
Log LogConfig `yaml:"log"`
|
||||
Wechat WechatConfig `yaml:"wechat"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port string `yaml:"port" mapstructure:"port"`
|
||||
BaseUrl string `mapstructure:"base_url"` // 添加mapstructure标签
|
||||
WsUrl string `yaml:"ws_url" mapstructure:"ws_url"`
|
||||
}
|
||||
|
||||
type JwtConfig struct {
|
||||
Secret string `yaml:"secret"`
|
||||
AccessExpiry time.Duration `yaml:"access_expiry"`
|
||||
}
|
||||
|
||||
type SnowIDConfig struct {
|
||||
DataCenterID uint16 `yaml:"datacenter_id"`
|
||||
WorkerID uint16 `yaml:"worker_id"`
|
||||
}
|
||||
|
||||
type PostgresConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Dbname string `yaml:"dbname"`
|
||||
SllMode bool `yaml:"sll_mode"`
|
||||
TimeZone string `yaml:"timezone"`
|
||||
MaxOpenConns int32 `yaml:"max_open_conns"`
|
||||
MaxIdleConns int32 `yaml:"max_idle_conns"`
|
||||
ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level string `yaml:"level"`
|
||||
Dir string `yaml:"dir"`
|
||||
MaxSize int `yaml:"max_size"`
|
||||
MaxBackups int `yaml:"max_backups"`
|
||||
MaxAge int `yaml:"max_age"`
|
||||
Compress bool `yaml:"compress"`
|
||||
}
|
||||
|
||||
type WechatConfig struct {
|
||||
AppId string `yaml:"app_id"`
|
||||
AppSecret string `yaml:"app_secret"`
|
||||
}
|
||||
Reference in new issue
Block a user