From 0e4feb18b19ea89e0b70f70f28930b7f406ba822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sat, 4 Jul 2020 21:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 32 +++++++++++++++++++++++++++++++- database/mysql.go | 4 ++-- models/models.go | 4 ++-- server.go | 3 +++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index a1807d4..aff4ef2 100644 --- a/config/config.go +++ b/config/config.go @@ -7,10 +7,40 @@ import ( "io/ioutil" "os" ) - const Dir = "config/" const AccountConf = Dir + "account.json" const MysqlConf = Dir + "mysql.json" +type Mysql struct{ + Server string + Port string + Database string + Username string + Password string +} +type Config struct { + Mysql *Mysql +} +func CreateConfig()*Config{ + mysql :=CreateMysql() + c:=&Config{ + Mysql: mysql, + } + return c +} +func CreateMysql() *Mysql { + var mysql Mysql + isExist, _ := tools.IsFileExist(MysqlConf) + if !isExist { + return &mysql + } + info, err := ioutil.ReadFile(MysqlConf) + if err != nil { + return &mysql + } + + err = json.Unmarshal(info, &mysql) + return &mysql +} func GetMysql() map[string]string { var mysql map[string]string isExist, _ := tools.IsFileExist(MysqlConf) diff --git a/database/mysql.go b/database/mysql.go index e793887..32ffd6d 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -11,8 +11,8 @@ type Mysql struct{ Dsn string } func NewMysql()*Mysql{ - mysqlInfo:=config.GetMysql() - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", mysqlInfo["Username"], mysqlInfo["Password"], mysqlInfo["Server"], mysqlInfo["Port"], mysqlInfo["Database"]) + mysql:=config.CreateMysql() + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database) return &Mysql{ Dsn:dsn, } diff --git a/models/models.go b/models/models.go index d510eca..4a842a0 100644 --- a/models/models.go +++ b/models/models.go @@ -14,8 +14,8 @@ type Model struct { DeletedAt *time.Time `sql:"index" json:"deleted_at"` } func init(){ - mysqlInfo:=config.GetMysql() - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", mysqlInfo["Username"], mysqlInfo["Password"], mysqlInfo["Server"], mysqlInfo["Port"], mysqlInfo["Database"]) + mysql:=config.CreateMysql() + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database) DB,_=gorm.Open("mysql",dsn) DB.SingularTable(true) DB.LogMode(true) diff --git a/server.go b/server.go index e2055d8..60f5c20 100644 --- a/server.go +++ b/server.go @@ -3,6 +3,7 @@ package main import ( "flag" "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/controller" "github.com/taoshihan1991/imaptool/middleware" "github.com/taoshihan1991/imaptool/tmpl" @@ -10,6 +11,7 @@ import ( ) var ( port string + GoflyConfig config.Config ) func main() { //获取参数中的数据 @@ -57,5 +59,6 @@ func main() { //前台接口 engine.GET("/notice", controller.GetNotice) + //配置文件 engine.Run(baseServer) }