This commit is contained in:
“xHuPo” 2025-06-17 14:46:09 +08:00
parent 01b8951dd5
commit 10ebc59ffb
17 changed files with 1087 additions and 238 deletions

View file

@ -9,54 +9,54 @@ import (
// Config holds all configuration for our application
type Config struct {
Server ServerConfig
Database DatabaseConfig
Security SecurityConfig
CORS CORSConfig
Wechat WechatConfig
Server ServerConfig `mapstructure:"server"`
Database DatabaseConfig `mapstructure:"database"`
Security SecurityConfig `mapstructure:"security"`
CORS CORSConfig `mapstructure:"cors"`
Wechat WechatConfig `mapstructure:"wechat"`
}
// ServerConfig holds all server related configuration
type ServerConfig struct {
Port int
Timeout time.Duration
Port int `mapstructure:"port"`
Timeout time.Duration `mapstructure:"timeout"`
}
// DatabaseConfig holds all database related configuration
type DatabaseConfig struct {
Driver string
SQLite SQLiteConfig
Postgres PostgresConfig
Driver string `mapstructure:"driver"`
SQLite SQLiteConfig `mapstructure:"sqlite"`
Postgres PostgresConfig `mapstructure:"postgres"`
}
// SQLiteConfig holds SQLite specific configuration
type SQLiteConfig struct {
Path string
Path string `mapstructure:"path"`
}
// PostgresConfig holds PostgreSQL specific configuration
type PostgresConfig struct {
Host string
Port int
User string
Password string
DBName string
SSLMode string
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
SSLMode string `mapstructure:"sslmode"`
}
// SecurityConfig holds all security related configuration
type SecurityConfig struct {
EncryptionKey string
JWTSigningKey string
TokenExpiry time.Duration
RefreshTokenExpiry time.Duration
EncryptionKey string `mapstructure:"encryption_key"`
JWTSigningKey string `mapstructure:"jwt_signing_key"`
TokenExpiry time.Duration `mapstructure:"token_expiry"`
RefreshTokenExpiry time.Duration `mapstructure:"refresh_token_expiry"`
}
// CORSConfig holds CORS related configuration
type CORSConfig struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
AllowedOrigins []string `mapstructure:"allowed_origins"`
AllowedMethods []string `mapstructure:"allowed_methods"`
AllowedHeaders []string `mapstructure:"allowed_headers"`
}
// WechatConfig holds WeChat related configuration
@ -69,10 +69,16 @@ type WechatConfig struct {
func LoadConfig(configPath string) (*Config, error) {
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(configPath)
v.AddConfigPath(".")
// 检查配置路径是否包含扩展名
if len(configPath) > 5 && (configPath[len(configPath)-5:] == ".yaml" || configPath[len(configPath)-4:] == ".yml") {
// 如果包含扩展名,直接使用完整路径
v.SetConfigFile(configPath)
} else {
// 否则按照传统方式处理
v.SetConfigName(configPath)
v.SetConfigType("yaml")
v.AddConfigPath(".")
}
// Read environment variables
v.AutomaticEnv()

44
config/config.yaml Normal file
View file

@ -0,0 +1,44 @@
# Server Configuration
server:
port: 8080
timeout: 30s
# Database Configuration
database:
driver: "sqlite3" # or "postgres"
sqlite:
path: "./data.db"
postgres:
host: "localhost"
port: 5432
user: "postgres"
password: "password"
dbname: "otpdb"
sslmode: "disable"
# Security Configuration
security:
encryption_key: "12345678901234567890123456789012"
jwt_signing_key: "jwt_secret_key_for_authentication_12345"
token_expiry: 24h
refresh_token_expiry: 168h # 7 days
# WeChat Configuration
wechat:
app_id: "wx57d1033974eb5250"
app_secret: "be494c2a81df685a40b9a74e1736b15d"
# CORS Configuration
cors:
allowed_origins:
- "http://localhost:8080"
- "https://yourdomain.com"
allowed_methods:
- "GET"
- "POST"
- "PUT"
- "DELETE"
- "OPTIONS"
allowed_headers:
- "Authorization"
- "Content-Type"