配置说明

环境变量

变量默认值说明
DB_HOSTlocalhostPostgreSQL 主机
DB_PORT5432PostgreSQL 端口
DB_USERpostgres数据库用户名
DB_PASSWORDpostgres数据库密码
DB_NAMEhr数据库名
DB_SSLMODEdisableSSL 模式
SERVER_PORT8080服务监听端口
REDIS_HOSTlocalhostRedis 主机
REDIS_PORT6379Redis 端口
REDIS_PASSWORDRedis 密码
JWT_SECRETchange-me-in-productionJWT 签名密钥

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) 会:

  1. 用 DSN 连接 PostgreSQL(重试 30 次,2 秒间隔)
  2. 执行 AutoMigrate(自动建表/加列)

Redis 初始化

config.InitRedis(cfg) 会:

  1. 用 RedisAddr 连接 Redis(重试 30 次,2 秒间隔)
  2. 用于员工列表缓存和 JWT Token 存储