自动设置数据库

This commit is contained in:
hoteas 2019-11-10 16:42:49 +08:00
parent 177eb86eae
commit 58b908f8ba
6 changed files with 114 additions and 21 deletions

42
.idea/workspace.xml generated
View File

@ -2,7 +2,12 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b2aca021-ff30-4cbf-8dc9-8cdd4f4c39dc" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/dri/db/auto.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/dri/mysql/mysql.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/dri/sqlite/sqlite.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/config/config.json" beforeDir="false" afterPath="$PROJECT_DIR$/example/config/config.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/example/main.go" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -10,12 +15,20 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Go File" />
</list>
</option>
</component>
<component name="GOROOT" path="D:/app/go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1TPrQqMMb4T7xS6v0afE7E8yl1y" />
<component name="PropertiesComponent">
<property name="DefaultGoTemplateProperty" value="Go File" />
<property name="GO_FMT" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
@ -34,6 +47,22 @@
</list>
</option>
</component>
<component name="RunManager">
<configuration name="go build code.hoteas.com/hoteas/hotime/example" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="hotime" />
<working_directory value="$PROJECT_DIR$/" />
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/example/main.go" />
<package value="code.hoteas.com/hoteas/hotime/example" />
<directory value="$PROJECT_DIR$/" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build code.hoteas.com/hoteas/hotime/example" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
@ -45,7 +74,7 @@
<option name="presentableId" value="Default" />
<updated>1573372557346</updated>
<workItem from="1573372558521" duration="18000" />
<workItem from="1573372583551" duration="294000" />
<workItem from="1573372583551" duration="2743000" />
</task>
<task id="LOCAL-00001" summary="清理">
<created>1573372845218</created>
@ -54,7 +83,14 @@
<option name="project" value="LOCAL" />
<updated>1573372845218</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="清理">
<created>1573372887175</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1573372887175</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">

16
dri/db/auto.go Normal file
View File

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

19
dri/mysql/mysql.go Normal file
View File

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

17
dri/sqlite/sqlite.go Normal file
View File

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

View File

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

View File

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