æ·feat: commplete init process
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-ini/ini"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
JwtSecret string
|
||||
PageSize int
|
||||
PrefixUrl string
|
||||
|
||||
RuntimeRootPath string
|
||||
|
||||
ImageSavePath string
|
||||
ImageMaxSize int
|
||||
ImageAllowExts []string
|
||||
|
||||
ExportSavePath string
|
||||
QrCodeSavePath string
|
||||
FontSavePath string
|
||||
|
||||
LogSavePath string
|
||||
LogSaveName string
|
||||
LogFileExt string
|
||||
TimeFormat string
|
||||
|
||||
LogKeepNum int
|
||||
CleanTaskLogID string
|
||||
}
|
||||
|
||||
var AppSetting = &App{}
|
||||
|
||||
type Server struct {
|
||||
RunMode string
|
||||
HttpPort int
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
}
|
||||
|
||||
var ServerSetting = &Server{}
|
||||
|
||||
type Database struct {
|
||||
Type string
|
||||
User string
|
||||
Password string
|
||||
Host string
|
||||
Name string
|
||||
TablePrefix string
|
||||
}
|
||||
|
||||
var DatabaseSetting = &Database{}
|
||||
|
||||
type Redis struct {
|
||||
Host string
|
||||
Password string
|
||||
MaxIdle int
|
||||
MaxActive int
|
||||
IdleTimeout time.Duration
|
||||
}
|
||||
|
||||
var RedisSetting = &Redis{}
|
||||
|
||||
var cfg *ini.File
|
||||
|
||||
// Setup initialize the configuration instance
|
||||
func Setup() {
|
||||
var err error
|
||||
cfg, err = ini.Load("conf/app.ini")
|
||||
if err != nil {
|
||||
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
|
||||
}
|
||||
|
||||
mapTo("app", AppSetting)
|
||||
mapTo("server", ServerSetting)
|
||||
mapTo("database", DatabaseSetting)
|
||||
mapTo("redis", RedisSetting)
|
||||
|
||||
AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
|
||||
ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
|
||||
ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
|
||||
RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second
|
||||
|
||||
// 默认值
|
||||
AppSetting.LogKeepNum = 1000
|
||||
AppSetting.CleanTaskLogID = "00000000-0000-0000-0000-000000000001"
|
||||
}
|
||||
|
||||
// mapTo map section
|
||||
func mapTo(section string, v interface{}) {
|
||||
err := cfg.Section(section).MapTo(v)
|
||||
if err != nil {
|
||||
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user