You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.2 KiB
146 lines
3.2 KiB
package conf
|
|
|
|
import (
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
"github.com/go-ini/ini"
|
|
"gopkg.in/go-playground/validator.v8"
|
|
)
|
|
|
|
// database 数据库
|
|
type database struct {
|
|
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"`
|
|
Debug bool
|
|
SessionSecret string
|
|
HashIDSalt string
|
|
}
|
|
|
|
// slave 作为slave存储端配置
|
|
type slave struct {
|
|
Secret string `validate:"omitempty,gte=64"`
|
|
CallbackTimeout int `validate:"omitempty,gte=1"`
|
|
SignatureTTL int `validate:"omitempty,gte=1"`
|
|
}
|
|
|
|
// 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
|
|
CaptchaLen int `validate:"gt=0"`
|
|
}
|
|
|
|
// redis 配置
|
|
type redis struct {
|
|
Server string
|
|
Password string
|
|
DB string
|
|
}
|
|
|
|
// 缩略图 配置
|
|
type thumb struct {
|
|
MaxWidth uint
|
|
MaxHeight uint
|
|
FileSuffix string `validate:"min=1"`
|
|
}
|
|
|
|
// 跨域配置
|
|
type cors struct {
|
|
AllowOrigins []string
|
|
AllowMethods []string
|
|
AllowHeaders []string
|
|
AllowCredentials bool
|
|
ExposeHeaders []string
|
|
}
|
|
|
|
var cfg *ini.File
|
|
|
|
const defaultConf = `[System]
|
|
Mode = master
|
|
Listen = :5212
|
|
SessionSecret = {SessionSecret}
|
|
HashIDSalt = {HashIDSalt}
|
|
`
|
|
|
|
// Init 初始化配置文件
|
|
func Init(path string) {
|
|
var err error
|
|
|
|
if path == "" || !util.Exists(path) {
|
|
// 创建初始配置文件
|
|
confContent := util.Replace(map[string]string{
|
|
"{SessionSecret}": util.RandStringRunes(64),
|
|
"{HashIDSalt}": util.RandStringRunes(64),
|
|
}, defaultConf)
|
|
f, err := util.CreatNestedFile("conf.ini")
|
|
if err != nil {
|
|
util.Log().Panic("无法创建配置文件, %s", err)
|
|
}
|
|
|
|
// 写入配置文件
|
|
_, err = f.WriteString(confContent)
|
|
if err != nil {
|
|
util.Log().Panic("无法写入配置文件, %s", err)
|
|
}
|
|
|
|
f.Close()
|
|
path = "conf.ini"
|
|
}
|
|
|
|
cfg, err = ini.Load(path)
|
|
if err != nil {
|
|
util.Log().Panic("无法解析配置文件 '%s': %s", path, err)
|
|
}
|
|
|
|
sections := map[string]interface{}{
|
|
"Database": DatabaseConfig,
|
|
"System": SystemConfig,
|
|
"Captcha": CaptchaConfig,
|
|
"Redis": RedisConfig,
|
|
"Thumbnail": ThumbConfig,
|
|
"CORS": CORSConfig,
|
|
"Slave": SlaveConfig,
|
|
}
|
|
for sectionName, sectionStruct := range sections {
|
|
err = mapSection(sectionName, sectionStruct)
|
|
if err != nil {
|
|
util.Log().Panic("配置文件 %s 分区解析失败: %s", sectionName, err)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// mapSection 将配置文件的 Section 映射到结构体上
|
|
func mapSection(section string, confStruct interface{}) error {
|
|
err := cfg.Section(section).MapTo(confStruct)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 验证合法性
|
|
validate := validator.New(&validator.Config{TagName: "validate"})
|
|
err = validate.Struct(confStruct)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|