From 47fafa0f2ffed8c54490a3ba88028c000cb24761 Mon Sep 17 00:00:00 2001 From: 陆恒 Date: Thu, 30 Apr 2020 10:14:45 +0800 Subject: [PATCH] 提交测试 --- src/HttpServer/conf/conf.go | 13 +++++++++++++ src/HttpServer/conf/world.xml | 3 ++- src/HttpServer/main/main.go | 10 +++++++++- src/mysql/dbmysql.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/mysql/dbmysql.go diff --git a/src/HttpServer/conf/conf.go b/src/HttpServer/conf/conf.go index 7aff05a..14bff6f 100644 --- a/src/HttpServer/conf/conf.go +++ b/src/HttpServer/conf/conf.go @@ -15,6 +15,7 @@ type RedisConf struct { } type TexasConf struct { + GameDB MysqlConf `xml:"GameDB"` ServerHttpAddr ServerHttpAddrConf `xml:"ServerHttpAddr"` Redis RedisConf `xml:"Redis"` } @@ -26,6 +27,18 @@ var ( func GetRedisConf() RedisConf { return config.Redis } +type MysqlConf struct { + Ip string `xml:",attr"` + Port int `xml:",attr"` + User string `xml:",attr"` + Pwd string `xml:",attr"` + Database string `xml:",attr"` + LoadInterval int `xml:",attr"` +} + +func GetGameDBConf() MysqlConf { + return config.GameDB +} func LoadConf(filename string) error { content, err := ioutil.ReadFile(filename) diff --git a/src/HttpServer/conf/world.xml b/src/HttpServer/conf/world.xml index 075ffb3..78db86e 100644 --- a/src/HttpServer/conf/world.xml +++ b/src/HttpServer/conf/world.xml @@ -1,5 +1,6 @@ + - + \ No newline at end of file diff --git a/src/HttpServer/main/main.go b/src/HttpServer/main/main.go index 443049c..5210ff9 100644 --- a/src/HttpServer/main/main.go +++ b/src/HttpServer/main/main.go @@ -2,6 +2,7 @@ package main import ( "HttpServer/conf" + "mysql" "HttpServer/logic" "HttpServer/redishandler" @@ -49,9 +50,16 @@ func main() { err = redishandler.Init() if err != nil { - logger.Info("err init redis err=%v", err) + logger.Error("err init redis err=%v", err) return } + + err = mysql.InitMysql() + if err != nil { + logger.Error("err init mysql err=%v", err) + return + } + //測試 logic.Test() go logic.StartHttpServe() diff --git a/src/mysql/dbmysql.go b/src/mysql/dbmysql.go new file mode 100644 index 0000000..1ef78b2 --- /dev/null +++ b/src/mysql/dbmysql.go @@ -0,0 +1,31 @@ +package mysql + +import ( + "HttpServer/conf" + "common/logger" + "database/sql" + "strconv" +) + +var ( + m_game_db *sql.DB +) + +func InitMysql() error { + db, err := InitMysqlByConf(conf.GetGameDBConf()) + if err != nil { + return err + } + m_game_db = db + return nil +} + +func InitMysqlByConf(cfg conf.MysqlConf) (*sql.DB, error) { + url := cfg.User + ":" + cfg.Pwd + "@tcp(" + cfg.Ip + ":" + strconv.Itoa(cfg.Port) + ")/" + cfg.Database + "?charset=utf8" + db, err := sql.Open("mysql", url) + if err != nil { + logger.Notic("Open mysql failed:%v url:%v", err, url) + return nil, err + } + return db, nil +} \ No newline at end of file -- libgit2 0.21.0