Cloudreve/pkg/conf/conf.go

119 lines
2.6 KiB
Go
Raw Normal View History

2019-11-08 18:29:12 +08:00
package conf
import (
2019-11-16 16:11:37 +08:00
"github.com/HFO4/cloudreve/pkg/util"
2019-11-08 18:29:12 +08:00
"github.com/go-ini/ini"
2019-11-13 17:22:45 +08:00
"gopkg.in/go-playground/validator.v8"
2019-11-08 18:29:12 +08:00
)
// database 数据库
2019-11-09 18:06:29 +08:00
type database struct {
2019-11-08 18:29:12 +08:00
Type string
User string
Password string
Host string
Name string
TablePrefix string
}
// system 系统通用配置
type system struct {
Mode string `validate:"eq=master|eq=slave"`
Listen string `validate:"required"`
2019-11-11 19:13:17 +08:00
Debug bool
SessionSecret string
2020-01-26 13:07:05 +08:00
HashIDSalt string `validate:"required"`
2019-12-29 17:04:08 +08:00
}
// slave 作为slave存储端配置
type slave struct {
Secret string `validate:"omitempty,gte=64"`
CallbackTimeout int `validate:"omitempty,gte=1"`
SignatureTTL int `validate:"omitempty,gte=1"`
}
2019-11-13 17:22:45 +08:00
// captcha 验证码配置
type captcha struct {
Height int `validate:"gte=0"`
Width int `validate:"gte=0"`
Mode int `validate:"gte=0,lte=3"`
ComplexOfNoiseText int `validate:"gte=0,lte=2"`
ComplexOfNoiseDot int `validate:"gte=0,lte=2"`
IsShowHollowLine bool
IsShowNoiseDot bool
IsShowNoiseText bool
IsShowSlimeLine bool
IsShowSineLine bool
2019-11-13 18:34:29 +08:00
CaptchaLen int `validate:"gt=0"`
2019-11-13 17:22:45 +08:00
}
2019-11-27 19:53:39 +08:00
// redis 配置
type redis struct {
Server string
Password string
DB string
}
// 缩略图 配置
type thumb struct {
MaxWidth uint
MaxHeight uint
FileSuffix string `validate:"min=1"`
2019-11-13 17:22:45 +08:00
}
2019-12-13 19:00:58 +08:00
// 跨域配置
type cors struct {
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
AllowCredentials bool
ExposeHeaders []string
}
2019-11-08 18:29:12 +08:00
var cfg *ini.File
2019-11-09 18:06:29 +08:00
// Init 初始化配置文件
func Init(path string) {
2019-11-08 18:29:12 +08:00
var err error
//TODO 配置文件不存在时创建
2019-11-09 18:06:29 +08:00
//TODO 配置合法性验证
cfg, err = ini.Load(path)
if err != nil {
2019-11-13 18:34:29 +08:00
util.Log().Panic("无法解析配置文件 '%s': %s", path, err)
2019-11-09 18:06:29 +08:00
}
sections := map[string]interface{}{
"Database": DatabaseConfig,
"System": SystemConfig,
"Captcha": CaptchaConfig,
"Redis": RedisConfig,
"Thumbnail": ThumbConfig,
2019-12-13 19:00:58 +08:00
"CORS": CORSConfig,
2019-12-29 17:04:08 +08:00
"Slave": SlaveConfig,
}
for sectionName, sectionStruct := range sections {
err = mapSection(sectionName, sectionStruct)
if err != nil {
util.Log().Panic("配置文件 %s 分区解析失败: %s", sectionName, err)
}
2019-11-08 18:29:12 +08:00
}
}
2019-11-09 18:06:29 +08:00
// mapSection 将配置文件的 Section 映射到结构体上
func mapSection(section string, confStruct interface{}) error {
err := cfg.Section(section).MapTo(confStruct)
2019-11-08 18:29:12 +08:00
if err != nil {
2019-11-09 18:06:29 +08:00
return err
2019-11-08 18:29:12 +08:00
}
2019-11-13 17:22:45 +08:00
// 验证合法性
validate := validator.New(&validator.Config{TagName: "validate"})
err = validate.Struct(confStruct)
if err != nil {
return err
}
2019-11-09 18:06:29 +08:00
return nil
2019-11-08 18:29:12 +08:00
}