draft
This commit is contained in:
107
backend/pkg/config/config.go
Normal file
107
backend/pkg/config/config.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Config 全局配置
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Database DatabaseConfig `mapstructure:"database"`
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
Security SecurityConfig `mapstructure:"security"`
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
Notify NotifyConfig `mapstructure:"notify"`
|
||||
Reconciliation ReconciliationConfig `mapstructure:"reconciliation"`
|
||||
Merchant MerchantConfig `mapstructure:"merchant"`
|
||||
Log LogConfig `mapstructure:"log"`
|
||||
Channels ChannelsConfig `mapstructure:"channels"`
|
||||
}
|
||||
|
||||
// ChannelsConfig 各渠道网关地址配置
|
||||
type ChannelsConfig struct {
|
||||
Heepay HeepayURLConfig `mapstructure:"heepay"`
|
||||
}
|
||||
|
||||
// HeepayURLConfig 汇元网关地址
|
||||
type HeepayURLConfig struct {
|
||||
PayURL string `mapstructure:"pay_url"` // 支付网关
|
||||
MerchantURL string `mapstructure:"merchant_url"` // 进件网关
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Port int `mapstructure:"port"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
||||
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
DSN string `mapstructure:"dsn"`
|
||||
MaxOpenConns int `mapstructure:"max_open_conns"`
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
||||
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
PoolSize int `mapstructure:"pool_size"`
|
||||
}
|
||||
|
||||
type SecurityConfig struct {
|
||||
FieldEncryptKey string `mapstructure:"field_encrypt_key"`
|
||||
AppSecretSalt string `mapstructure:"app_secret_salt"`
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
Secret string `mapstructure:"secret"`
|
||||
ExpireHours int `mapstructure:"expire_hours"`
|
||||
}
|
||||
|
||||
type NotifyConfig struct {
|
||||
PollerInterval time.Duration `mapstructure:"poller_interval"`
|
||||
PollerBatch int `mapstructure:"poller_batch"`
|
||||
HTTPTimeout time.Duration `mapstructure:"http_timeout"`
|
||||
}
|
||||
|
||||
type ReconciliationConfig struct {
|
||||
Cron string `mapstructure:"cron"`
|
||||
BillRetryTimes int `mapstructure:"bill_retry_times"`
|
||||
}
|
||||
|
||||
type MerchantConfig struct {
|
||||
StatusCheckCron string `mapstructure:"status_check_cron"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level string `mapstructure:"level"`
|
||||
Format string `mapstructure:"format"`
|
||||
}
|
||||
|
||||
// Load 加载配置文件
|
||||
func Load(cfgFile string) (*Config, error) {
|
||||
v := viper.New()
|
||||
if cfgFile != "" {
|
||||
v.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
v.SetConfigName("config.local")
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath("./configs")
|
||||
v.AddConfigPath(".")
|
||||
}
|
||||
|
||||
v.AutomaticEnv()
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
38
backend/pkg/config/database.go
Normal file
38
backend/pkg/config/database.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// NewDB 初始化 MySQL 连接
|
||||
func NewDB(cfg DatabaseConfig) (*gorm.DB, error) {
|
||||
gormCfg := &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Warn),
|
||||
}
|
||||
|
||||
db, err := gorm.Open(mysql.Open(cfg.DSN), gormCfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open mysql: %w", err)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
|
||||
|
||||
if err := sqlDB.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("ping mysql: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("mysql connected")
|
||||
return db, nil
|
||||
}
|
||||
26
backend/pkg/config/redis.go
Normal file
26
backend/pkg/config/redis.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
// NewRedis 初始化 Redis 客户端
|
||||
func NewRedis(cfg RedisConfig) (*redis.Client, error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
PoolSize: cfg.PoolSize,
|
||||
})
|
||||
|
||||
if err := rdb.Ping(context.Background()).Err(); err != nil {
|
||||
return nil, fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("redis connected")
|
||||
return rdb, nil
|
||||
}
|
||||
Reference in New Issue
Block a user