配置文件
VEF Framework 使用 YAML 格式的配置文件,支持多环境配置。
配置文件位置
project/
├── config/
│ ├── config.yaml # 默认配置
│ ├── config.dev.yaml # 开发环境
│ ├── config.test.yaml # 测试环境
│ └── config.prod.yaml # 生产环境完整配置示例
yaml
# config.yaml
app:
name: "my-app"
version: "1.0.0"
env: "development"
debug: true
server:
host: "0.0.0.0"
port: 8080
readTimeout: 30s
writeTimeout: 30s
idleTimeout: 120s
database:
driver: "mysql"
host: "localhost"
port: 3306
database: "myapp"
username: "root"
password: "password"
charset: "utf8mb4"
maxIdleConns: 10
maxOpenConns: 100
connMaxLifetime: 1h
logLevel: "info"
redis:
host: "localhost"
port: 6379
password: ""
db: 0
poolSize: 10
auth:
jwt:
secret: "your-secret-key-change-in-production"
expiration: 24h
refreshExpiration: 168h
issuer: "my-app"
log:
level: "info"
format: "json"
output: "stdout"
file:
enabled: false
path: "logs/app.log"
maxSize: 100
maxBackups: 3
maxAge: 7配置项说明
应用配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
app.name | string | - | 应用名称 |
app.version | string | - | 应用版本 |
app.env | string | development | 运行环境 |
app.debug | bool | false | 调试模式 |
服务器配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
server.host | string | 0.0.0.0 | 监听地址 |
server.port | int | 8080 | 监听端口 |
server.readTimeout | duration | 30s | 读取超时 |
server.writeTimeout | duration | 30s | 写入超时 |
server.idleTimeout | duration | 120s | 空闲超时 |
数据库配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
database.driver | string | mysql | 数据库驱动 |
database.host | string | localhost | 数据库主机 |
database.port | int | 3306 | 数据库端口 |
database.database | string | - | 数据库名 |
database.username | string | - | 用户名 |
database.password | string | - | 密码 |
database.charset | string | utf8mb4 | 字符集 |
database.maxIdleConns | int | 10 | 最大空闲连接数 |
database.maxOpenConns | int | 100 | 最大打开连接数 |
database.connMaxLifetime | duration | 1h | 连接最大生命周期 |
database.logLevel | string | info | 日志级别 |
Redis 配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
redis.host | string | localhost | Redis 主机 |
redis.port | int | 6379 | Redis 端口 |
redis.password | string | - | Redis 密码 |
redis.db | int | 0 | 数据库索引 |
redis.poolSize | int | 10 | 连接池大小 |
认证配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
auth.jwt.secret | string | - | JWT 密钥 |
auth.jwt.expiration | duration | 24h | Token 过期时间 |
auth.jwt.refreshExpiration | duration | 168h | 刷新 Token 过期时间 |
auth.jwt.issuer | string | - | Token 签发者 |
日志配置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
log.level | string | info | 日志级别 |
log.format | string | json | 日志格式 |
log.output | string | stdout | 输出目标 |
log.file.enabled | bool | false | 启用文件日志 |
log.file.path | string | - | 日志文件路径 |
log.file.maxSize | int | 100 | 单文件最大大小(MB) |
log.file.maxBackups | int | 3 | 最大备份数 |
log.file.maxAge | int | 7 | 最大保留天数 |
多环境配置
环境特定配置
yaml
# config.prod.yaml
app:
env: "production"
debug: false
database:
host: "db.production.internal"
maxOpenConns: 200
log:
level: "warn"
file:
enabled: true
path: "/var/log/app/app.log"加载配置
go
import "github.com/ilxqx/vef-framework-go/config"
// Load configuration based on VEF_APP_ENV
cfg, err := config.Load()
if err != nil {
log.Fatal(err)
}
// Access configuration
dbHost := cfg.Database.Host
jwtSecret := cfg.Auth.JWT.Secret自定义配置
扩展配置结构
go
package config
import "github.com/ilxqx/vef-framework-go/config"
type AppConfig struct {
config.Config
// Custom configuration
Payment PaymentConfig `yaml:"payment"`
SMS SMSConfig `yaml:"sms"`
}
type PaymentConfig struct {
Provider string `yaml:"provider"`
AppId string `yaml:"appId"`
AppSecret string `yaml:"appSecret"`
}
type SMSConfig struct {
Provider string `yaml:"provider"`
ApiKey string `yaml:"apiKey"`
}使用自定义配置
yaml
# config.yaml
payment:
provider: "alipay"
appId: "your-app-id"
appSecret: "your-app-secret"
sms:
provider: "aliyun"
apiKey: "your-api-key"go
// Load custom configuration
var cfg AppConfig
if err := config.LoadInto(&cfg); err != nil {
log.Fatal(err)
}
// Use custom configuration
paymentProvider := cfg.Payment.Provider