Cloudreve/pkg/conf/conf.go

62 lines
1.2 KiB
Go
Raw Normal View History

2019-11-08 18:29:12 +08:00
package conf
import (
"cloudreve/pkg/util"
2019-11-08 18:29:12 +08:00
"github.com/go-ini/ini"
)
// 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
}
2019-11-09 18:06:29 +08:00
var DatabaseConfig = &database{
2019-11-08 18:29:12 +08:00
Type: "UNSET",
}
// system 系统通用配置
type system struct {
Debug bool
}
var SystemConfig = &system{}
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 {
util.Log().Panic("无法解析配置文件 '%s': ", path, err)
}
sections := map[string]interface{}{
"Database": DatabaseConfig,
"System": SystemConfig,
}
for sectionName, sectionStruct := range sections {
err = mapSection(sectionName, sectionStruct)
if err != nil {
util.Log().Warning("配置文件 %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-09 18:06:29 +08:00
return nil
2019-11-08 18:29:12 +08:00
}