From 53da4655ad71e246071b18a2ff8d1937335e8fa6 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 8 Nov 2019 18:29:12 +0800 Subject: [PATCH] Reading config file --- conf/conf.ini | 7 +++++++ go.mod | 1 + main.go | 2 ++ models/init.go | 2 +- pkg/conf/conf.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ pkg/util/logger.go | 17 +++++++++++++---- 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 conf/conf.ini create mode 100644 pkg/conf/conf.go diff --git a/conf/conf.ini b/conf/conf.ini new file mode 100644 index 0000000..0ce3ce3 --- /dev/null +++ b/conf/conf.ini @@ -0,0 +1,7 @@ +[Database] +Type = mysql +User = root +Password = root +Host = 127.0.0.1:3306 +Name = v3 +TablePrefix = v3_ \ No newline at end of file diff --git a/go.mod b/go.mod index 4a13281..d7a110b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/DATA-DOG/go-sqlmock v1.3.3 github.com/gin-gonic/gin v1.4.0 + github.com/go-ini/ini v1.50.0 github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/universal-translator v0.16.0 // indirect github.com/jinzhu/gorm v1.9.11 diff --git a/main.go b/main.go index 2c83d77..96f25c7 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "Cloudreve/models" + "Cloudreve/pkg/conf" "Cloudreve/routers" ) func init() { + conf.Init() model.Init() } diff --git a/models/init.go b/models/init.go index dccf0e5..25a5a6c 100644 --- a/models/init.go +++ b/models/init.go @@ -5,7 +5,6 @@ import ( "github.com/jinzhu/gorm" "time" - // _ "github.com/jinzhu/gorm/dialects/mysql" ) @@ -18,6 +17,7 @@ func Init() { util.Log().Info("初始化数据库连接\n") db, err := gorm.Open("mysql", "root:root@(localhost)/v3?charset=utf8&parseTime=True&loc=Local") db.LogMode(true) + //db.SetLogger(util.Log()) // Error if err != nil { util.Log().Panic("连接数据库不成功", err) diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go new file mode 100644 index 0000000..957af07 --- /dev/null +++ b/pkg/conf/conf.go @@ -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 分区解析失败") + } +} diff --git a/pkg/util/logger.go b/pkg/util/logger.go index b8f1222..522a056 100644 --- a/pkg/util/logger.go +++ b/pkg/util/logger.go @@ -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) }