06488f0237
功能: - Go后端 (Gin + GORM + PostgreSQL) - UniApp用户端 (iOS/Android/小程序) - DaisyUI5后台管理 - JWT认证 + 微信登录 - 盲选加权算法 - 会员系统 + 优惠券 - 打分评价 + 偏好学习
85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
DB DBConfig `yaml:"database"`
|
|
Redis RedisConfig `yaml:"redis"`
|
|
JWT JWTConfig `yaml:"jwt"`
|
|
AI AIConfig `yaml:"ai"`
|
|
Wechat WechatConfig `yaml:"wechat"`
|
|
}
|
|
|
|
type WechatConfig struct {
|
|
AppID string `yaml:"app_id"`
|
|
AppSecret string `yaml:"app_secret"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string `yaml:"port"`
|
|
Mode string `yaml:"mode"`
|
|
}
|
|
|
|
type DBConfig struct {
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
User string `yaml:"user"`
|
|
Password string `yaml:"password"`
|
|
DBName string `yaml:"dbname"`
|
|
SSLMode string `yaml:"sslmode"`
|
|
MaxIdleConns int `yaml:"max_idle_conns"`
|
|
MaxOpenConns int `yaml:"max_open_conns"`
|
|
}
|
|
|
|
func (d *DBConfig) DSN() string {
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?sslmode=%s&charset=utf8mb4",
|
|
d.User, d.Password, d.Host, d.Port, d.DBName, d.SSLMode)
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
Password string `yaml:"password"`
|
|
DB int `yaml:"db"`
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string `yaml:"secret"`
|
|
Expire string `yaml:"expire"`
|
|
RefreshExpire string `yaml:"refresh_expire"`
|
|
}
|
|
|
|
type AIConfig struct {
|
|
Endpoint string `yaml:"endpoint"`
|
|
Key string `yaml:"key"`
|
|
}
|
|
|
|
var (
|
|
cfg *Config
|
|
cfgOnce sync.Once
|
|
)
|
|
|
|
func Load(path string) (*Config, error) {
|
|
var err error
|
|
cfgOnce.Do(func() {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
cfg = &Config{}
|
|
err = yaml.Unmarshal(data, cfg)
|
|
})
|
|
return cfg, err
|
|
}
|
|
|
|
func Get() *Config {
|
|
return cfg
|
|
}
|