diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8d50337..6b759f4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,12 @@ - + + + + + + + + + + @@ -34,6 +47,22 @@ + + + + + + + + + + + + + + + + @@ -45,7 +74,7 @@ diff --git a/dri/db/auto.go b/dri/db/auto.go new file mode 100644 index 0000000..4137107 --- /dev/null +++ b/dri/db/auto.go @@ -0,0 +1,16 @@ +package db + +import ( + "code.hoteas.com/hoteas/hotime" + "code.hoteas.com/hoteas/hotime/dri/mysql" + "code.hoteas.com/hoteas/hotime/dri/sqlite" + "strings" +) + +func SetDB(appIns *hotime.Application) { + if appIns.Config.GetString("dbType") == "sqlite" || strings.Contains(appIns.Config.GetString("dbName"), ".db") { + sqlite.SetDB(appIns) + } else { + mysql.SetDB(appIns) + } +} diff --git a/dri/mysql/mysql.go b/dri/mysql/mysql.go new file mode 100644 index 0000000..4d290be --- /dev/null +++ b/dri/mysql/mysql.go @@ -0,0 +1,19 @@ +package mysql + +import ( + "code.hoteas.com/hoteas/hotime" + "database/sql" + _ "github.com/go-sql-driver/mysql" +) + +func SetDB(appIns *hotime.Application) { + appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB { + query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + + "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" + DB, e := sql.Open("mysql", query) + if e != nil && len(err) != 0 { + err[0].SetError(e) + } + return DB + }) +} diff --git a/dri/sqlite/sqlite.go b/dri/sqlite/sqlite.go new file mode 100644 index 0000000..bea0e04 --- /dev/null +++ b/dri/sqlite/sqlite.go @@ -0,0 +1,17 @@ +package sqlite + +import ( + "code.hoteas.com/hoteas/hotime" + "database/sql" + _ "github.com/mattn/go-sqlite3" +) + +func SetDB(appIns *hotime.Application) { + appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB { + db, e := sql.Open("sqlite3", appIns.Config.GetString("dbName")) + if e != nil && len(err) != 0 { + err[0].SetError(e) + } + return db + }) +} diff --git a/example/config/config.json b/example/config/config.json index abbe2c8..492d7da 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -1,21 +1,27 @@ { "cacheLongTime": 2592000, "cacheShortTime": 7200, + "dbCached": 0, "dbHost": "127.0.0.1", "dbName": "test", "dbPort": "3306", "dbPwd": "root", "dbUser": "root", + "debug": 1, "defFile": [ "index.html", "index.htm" ], "error": {}, "logLevel": 0, + "modeRouterStrict": false, "port": "8080", "redisHost": "192.168.6.254:6379", "redisPort": "6379", "redisPwd": "9rusdfjk482fjdfo2e023", "sessionName": "HOTIME", + "tlsCert": "", + "tlsKey": "", + "tlsPort": "0", "tpt": "example/tpt" } \ No newline at end of file diff --git a/example/main.go b/example/main.go index e80b247..3ab4be8 100644 --- a/example/main.go +++ b/example/main.go @@ -1,10 +1,11 @@ package main import ( - "database/sql" + "code.hoteas.com/hoteas/hotime" + "code.hoteas.com/hoteas/hotime/dri/mysql" "fmt" - _ "github.com/go-sql-driver/mysql" - "go.hoteas.com/hotime" + "strings" + //"go.hoteas.com/hotime/cache" "golang.org/x/net/websocket" "time" @@ -13,12 +14,10 @@ import ( func main() { - appIns := hotime.Application{} - appIns.SetConnectListener(func(context *hotime.Context) bool { - fmt.Println(context.HandlerStr+time.Now().Format(" 2006-01-02 15:04 ")+hotime.Substr(context.Req.RemoteAddr,0,strings.Index(context.Req.RemoteAddr,":"))) + fmt.Println(context.HandlerStr + time.Now().Format(" 2006-01-02 15:04 ") + hotime.Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))) //this.HandlerStr = "/test.html" return true @@ -26,7 +25,6 @@ func main() { //手动模式, appIns.SetConfig("example/config/config.json") - //redis缓存接入 //ca:=hotime.CacheIns(&cache.CacheRedis{Host:appIns.Config.GetString("redisHost"),Pwd:appIns.Config.GetString("redisPwd"),Time:appIns.Config.GetCeilInt64("cacheLongTime")}) //ca.Cache("xyzm","dasdas") @@ -35,18 +33,19 @@ func main() { //ca.Cache("xyz*",nil) //fmt.Println(ca.Cache("xyzm").Data) + mysql.SetDB(&appIns) - appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB { - query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + - "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" - DB, e := sql.Open("mysql", query) - if e != nil && len(err) != 0 { - err[0].SetError(e) - } - return DB - }) + //appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB { + // query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + + // "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" + // DB, e := sql.Open("mysql", query) + // if e != nil && len(err) != 0 { + // err[0].SetError(e) + // } + // return DB + //}) //内存缓存数据库数据,错误则删除 -// appIns.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{}) + // appIns.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{}) appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")})) appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{})) @@ -81,7 +80,7 @@ func main() { msg := make([]byte, 5120) n, err := ws.Read(msg) go func() { - time.Sleep(time.Second*5) + time.Sleep(time.Second * 5) ws.Write([]byte("dsadasdasgregergrerge")) }()