优化配置类

pull/30/head
陶士涵 4 years ago
parent d2ae5c2476
commit 0e4feb18b1

@ -7,10 +7,40 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
) )
const Dir = "config/" const Dir = "config/"
const AccountConf = Dir + "account.json" const AccountConf = Dir + "account.json"
const MysqlConf = Dir + "mysql.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 { func GetMysql() map[string]string {
var mysql map[string]string var mysql map[string]string
isExist, _ := tools.IsFileExist(MysqlConf) isExist, _ := tools.IsFileExist(MysqlConf)

@ -11,8 +11,8 @@ type Mysql struct{
Dsn string Dsn string
} }
func NewMysql()*Mysql{ func NewMysql()*Mysql{
mysqlInfo:=config.GetMysql() mysql:=config.CreateMysql()
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", mysqlInfo["Username"], mysqlInfo["Password"], mysqlInfo["Server"], mysqlInfo["Port"], mysqlInfo["Database"]) 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{ return &Mysql{
Dsn:dsn, Dsn:dsn,
} }

@ -14,8 +14,8 @@ type Model struct {
DeletedAt *time.Time `sql:"index" json:"deleted_at"` DeletedAt *time.Time `sql:"index" json:"deleted_at"`
} }
func init(){ func init(){
mysqlInfo:=config.GetMysql() mysql:=config.CreateMysql()
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"]) 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,_=gorm.Open("mysql",dsn)
DB.SingularTable(true) DB.SingularTable(true)
DB.LogMode(true) DB.LogMode(true)

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/controller" "github.com/taoshihan1991/imaptool/controller"
"github.com/taoshihan1991/imaptool/middleware" "github.com/taoshihan1991/imaptool/middleware"
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
@ -10,6 +11,7 @@ import (
) )
var ( var (
port string port string
GoflyConfig config.Config
) )
func main() { func main() {
//获取参数中的数据 //获取参数中的数据
@ -57,5 +59,6 @@ func main() {
//前台接口 //前台接口
engine.GET("/notice", controller.GetNotice) engine.GET("/notice", controller.GetNotice)
//配置文件
engine.Run(baseServer) engine.Run(baseServer)
} }

Loading…
Cancel
Save