yaml文件
yaml中一部分代码
# catering Global Configuration
# jwt configuration
jwt:
signing-key: 'catering'
expires-time: 604800
issuer: 'catering'
# zap logger configuration
zap:
level: 'info'
format: 'console'
prefix: '[catering]'
director: 'log'
show-line: true
encode-level: 'LowercaseColorLevelEncoder'
stacktrace-key: 'stacktrace'
log-in-console: true
# redis configuration
redis:
db: 0
addr: '192.168.138.128:6379'
password: ''
# casbin configuration
casbin:
model-path: './resource/rbac_model.conf'
# system configuration
system:
env: 'develop' # Change to "develop" to skip authentication for development mode
addr: 8888
db-type: 'mysql'
oss-type: 'local' # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置
use-multipoint: false
# IP限制次数 一个小时15000次
iplimit-count: 15000
# IP限制一个小时
iplimit-time: 3600
# captcha configuration
captcha:
key-long: 6
img-width: 240
img-height: 80
# mysql connect configuration
mysql:
host: 127.0.0.1
port: "3306"
config: charset=utf8mb4&parseTime=True&loc=Local
db-name: gva
username: root
password: root
max-idle-conns: 0
max-open-conns: 0
log-mode: ""
log-zap: false
配置文件结构体
结构体中一部分代码
package config
type JWT struct {
SigningKey string `json:"signingKey" yaml:"signing-key"` // jwt签名
ExpiresTime int64 `json:"expiresTime" yaml:"expires-time"` // 过期时间
Issuer string `json:"issuer" yaml:"issuer"` // 签发者
}
package config
type Config struct {
JWT JWT `json:"jwt" yaml:"jwt"`
Zap Zap `json:"zap" yaml:"zap"`
Redis Redis `json:"redis" yaml:"redis"`
Casbin Casbin `json:"casbin" yaml:"casbin"`
System System `json:"system" yaml:"system"`
Captcha Captcha `json:"captcha" yaml:"captcha"`
// gorm
Mysql Mysql `json:"mysql" yaml:"mysql"`
// oss
Local Local `json:"local" yaml:"local"`
AliyunOSS AliyunOSS `json:"aliyunOSS" yaml:"aliyun-oss"`
Excel Excel `json:"excel" yaml:"excel"`
// 跨域配置
Cors CORS `json:"cors" yaml:"cors"`
}
初始化文件
package initialize
import (
"catering/global"
"io/ioutil"
"gopkg.in/yaml.v2"
)
func InitConfig() error {
path := "./conf/config.yaml"
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
//yaml.Unmarshal会根据yaml标签的字段进行赋值
err = yaml.Unmarshal(data, &global.Config)
if err != nil {
return err
}
return nil
}