配置说明
环境变量
| 变量 | 默认值 | 说明 |
|---|---|---|
| DB_HOST | localhost | PostgreSQL 主机 |
| DB_PORT | 5432 | PostgreSQL 端口 |
| DB_USER | postgres | 数据库用户名 |
| DB_PASSWORD | postgres | 数据库密码 |
| DB_NAME | hr | 数据库名 |
| DB_SSLMODE | disable | SSL 模式 |
| SERVER_PORT | 8080 | 服务监听端口 |
| REDIS_HOST | localhost | Redis 主机 |
| REDIS_PORT | 6379 | Redis 端口 |
| REDIS_PASSWORD | Redis 密码 | |
| JWT_SECRET | change-me-in-production | JWT 签名密钥 |
Config 结构体
定义在 config/config.go:
type Config struct {
DBHost string
DBPort string
DBUser string
DBPassword string
DBName string
DBSSLMode string
ServerPort string
RedisHost string
RedisPort string
RedisPassword string
JWTSecret string
}
通过 config.Load() 读取,优先使用环境变量,未设置则用默认值。
cfg.DSN() 方法生成 PostgreSQL 连接字符串。
cfg.RedisAddr() 方法生成 Redis 地址(host:port)。
数据库初始化
config.NewDB(cfg) 会:
- 用 DSN 连接 PostgreSQL(重试 30 次,2 秒间隔)
- 执行 AutoMigrate(自动建表/加列)
Redis 初始化
config.InitRedis(cfg) 会:
- 用 RedisAddr 连接 Redis(重试 30 次,2 秒间隔)
- 用于员工列表缓存和 JWT Token 存储