mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Reading config file
This commit is contained in:
45
pkg/conf/conf.go
Normal file
45
pkg/conf/conf.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"Cloudreve/pkg/util"
|
||||
"fmt"
|
||||
"github.com/go-ini/ini"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
Database Database
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Type string
|
||||
User string
|
||||
Password string
|
||||
Host string
|
||||
Name string
|
||||
TablePrefix string
|
||||
}
|
||||
|
||||
var database = &Database{
|
||||
Type: "UNSET",
|
||||
}
|
||||
|
||||
var cfg *ini.File
|
||||
|
||||
func Init() {
|
||||
var err error
|
||||
//TODO 配置文件不存在时创建
|
||||
cfg, err = ini.Load("conf/conf.ini")
|
||||
if err != nil {
|
||||
util.Log().Panic("无法解析配置文件 'conf/conf.ini': ", err)
|
||||
}
|
||||
mapSection("Database", database)
|
||||
fmt.Println(database)
|
||||
|
||||
}
|
||||
|
||||
func mapSection(section string, confStruct interface{}) {
|
||||
err := cfg.Section("Database").MapTo(database)
|
||||
if err != nil {
|
||||
util.Log().Warning("配置文件 Database 分区解析失败")
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func (ll *Logger) Error(format string, v ...interface{}) {
|
||||
if LevelError > ll.level {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("[E] "+format, v...)
|
||||
msg := fmt.Sprintf("[Error] "+format, v...)
|
||||
ll.Println(msg)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func (ll *Logger) Warning(format string, v ...interface{}) {
|
||||
if LevelWarning > ll.level {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("[W] "+format, v...)
|
||||
msg := fmt.Sprintf("[Warning] "+format, v...)
|
||||
ll.Println(msg)
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ func (ll *Logger) Info(format string, v ...interface{}) {
|
||||
if LevelInformational > ll.level {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("[I] "+format, v...)
|
||||
msg := fmt.Sprintf("[Info] "+format, v...)
|
||||
ll.Println(msg)
|
||||
}
|
||||
|
||||
@@ -71,7 +71,16 @@ func (ll *Logger) Debug(format string, v ...interface{}) {
|
||||
if LevelDebug > ll.level {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("[D] "+format, v...)
|
||||
msg := fmt.Sprintf("[Debug] "+format, v...)
|
||||
ll.Println(msg)
|
||||
}
|
||||
|
||||
// GORM 的 Logger实现
|
||||
func (ll *Logger) Print(v ...interface{}) {
|
||||
if LevelDebug > ll.level {
|
||||
return
|
||||
}
|
||||
msg := fmt.Sprintf("[SQL] ", v...)
|
||||
ll.Println(msg)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user