优化配置类

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

@ -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)

@ -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,
}

@ -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)

@ -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)
}

Loading…
Cancel
Save