diff --git a/application.go b/application.go index 01d0a03..bad8ab4 100644 --- a/application.go +++ b/application.go @@ -1,6 +1,7 @@ package hotime import ( + "./tools/db" "bytes" "database/sql" "encoding/json" @@ -158,21 +159,9 @@ func (this *Application) Run(router Router) { //启动实例 func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) { - //this.Db.DBCached=false - //if this.Config.GetCeilInt("dbCached")!=0{ - // this.Db.DBCached=true - //} - this.connectDbFunc = connect this.Db.SetConnect(this.connectDbFunc) - this.Db.DBCached = false - if this.Config.GetCeilInt("dbCached") != 0 { - this.Db.DBCached = true - } - - this.Db.Type = this.Config.GetString("dbType") - } //设置配置文件路径全路径或者相对路径 @@ -458,3 +447,11 @@ func (this *Application) crossDomain(context *Context) { header.Set("Access-Control-Allow-Origin", refer) } } +func Init(config string) Application { + appIns := Application{} + //手动模式, + appIns.SetConfig(config) + db.SetDB(&appIns) + //appIns.SetCache() + return appIns +} diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..6127dc7 --- /dev/null +++ b/cache.go @@ -0,0 +1,27 @@ +package hotime + +type HoTimeCache struct { + + //set(key string, value interface{}, time int64) + //get(key string) interface{} + //delete(key string) + dbCache CacheIns + redisCache CacheIns + memoryCache CacheIns +} + +func (this *HoTimeCache) Session(key string, data ...interface{}) *Obj { + return nil +} +func (this *HoTimeCache) Db(key string, data ...interface{}) *Obj { + return nil +} + +func (this *HoTimeCache) Cache(key string, data ...interface{}) *Obj { + return nil +} + +func (this *HoTimeCache) getIndex() []CacheIns { + + return nil +} diff --git a/map.go b/map.go index 1d9751d..a4ae5f6 100644 --- a/map.go +++ b/map.go @@ -90,6 +90,14 @@ func (this Map) GetSlice(key string, err ...*Error) Slice { return v +} +func (this Map) GetBool(key string, err ...*Error) bool { + + //var v Slice + v := ObjToBool((this)[key], err...) + + return v + } func (this Map) GetMap(key string, err ...*Error) Map { diff --git a/objtoobj.go b/objtoobj.go index d9f5419..3653677 100644 --- a/objtoobj.go +++ b/objtoobj.go @@ -212,6 +212,31 @@ func ObjToInt(obj interface{}, e ...*Error) int { return int(v) } +func ObjToBool(obj interface{}, e ...*Error) bool { + var err error + v := false + + if obj == nil { + + err = errors.New("没有合适的转换对象!") + } else { + switch obj.(type) { + case bool: + v = obj.(bool) + default: + toInt := ObjToInt(obj) + if toInt != 0 { + v = true + } + err = errors.New("没有合适的转换对象!") + } + } + if len(e) != 0 { + e[0].SetError(err) + } + return v +} + func ObjToStr(obj interface{}) string { // fmt.Println(reflect.ValueOf(obj).Type().String() ) str := "" diff --git a/slice.go b/slice.go index 7cf9932..034cca9 100644 --- a/slice.go +++ b/slice.go @@ -60,6 +60,15 @@ func (this Slice) GetSlice(key int, err ...*Error) Slice { return v } +func (this Slice) GetBool(key int, err ...*Error) bool { + + //var v Slice + v := ObjToBool((this)[key], err...) + + return v + +} + func (this Slice) GetMap(key int, err ...*Error) Map { //var v Map v := ObjToMap((this)[key], err...) diff --git a/tools/db/auto.go b/tools/db/auto.go index 42938ef..059bbcd 100644 --- a/tools/db/auto.go +++ b/tools/db/auto.go @@ -4,15 +4,16 @@ import ( "../../../hotime" "../mysql" "../sqlite" - "strings" ) func SetDB(appIns *hotime.Application) { - if appIns.Config.GetString("dbType") == "sqlite" || strings.Index(appIns.Config.GetString("dbName"), ".db") == len(appIns.Config.GetString("dbName"))-3 { - appIns.Config["dbType"] = "sqlite" - sqlite.SetDB(appIns) - } else { - appIns.Config["dbType"] = "mysql" - mysql.SetDB(appIns) + db := appIns.Config.GetMap("db") + dbSqlite := db.GetMap("sqlite") + dbMysql := db.GetMap("mysql") + if db != nil && dbSqlite != nil { + sqlite.SetDB(appIns, dbSqlite) + } + if db != nil && dbMysql != nil { + mysql.SetDB(appIns, dbMysql) } } diff --git a/tools/mysql/mysql.go b/tools/mysql/mysql.go index 7942286..3e420fa 100644 --- a/tools/mysql/mysql.go +++ b/tools/mysql/mysql.go @@ -6,14 +6,15 @@ import ( _ "github.com/go-sql-driver/mysql" ) -func SetDB(appIns *hotime.Application) { +func SetDB(appIns *hotime.Application, config hotime.Map) { 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" + query := config.GetString("user") + ":" + config.GetString("password") + + "@tcp(" + config.GetString("host") + ":" + config.GetString("port") + ")/" + config.GetString("name") + "?charset=utf8" DB, e := sql.Open("mysql", query) if e != nil && len(err) != 0 { err[0].SetError(e) } return DB }) + appIns.Db.Type = "mysql" } diff --git a/tools/sqlite/sqlite.go b/tools/sqlite/sqlite.go index e101bc5..69786c6 100644 --- a/tools/sqlite/sqlite.go +++ b/tools/sqlite/sqlite.go @@ -6,12 +6,13 @@ import ( _ "github.com/mattn/go-sqlite3" ) -func SetDB(appIns *hotime.Application) { +func SetDB(appIns *hotime.Application, config hotime.Map) { appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB { - db, e := sql.Open("sqlite3", appIns.Config.GetString("dbName")) + db, e := sql.Open("sqlite3", config.GetString("path")) if e != nil && len(err) != 0 { err[0].SetError(e) } return db }) + appIns.Db.Type = "sqlite" } diff --git a/var.go b/var.go index be6e912..08a03c3 100644 --- a/var.go +++ b/var.go @@ -27,11 +27,10 @@ var Config = Map{ "4": "数据处理异常", "5": "数据结果异常", }, - "tpt": "tpt", - "defFile": []string{"index.html", "index.htm"}, - "modeRouterStrict": false, - "port": "80", - "sessionName": "HOTIME", + "tpt": "tpt", + "defFile": []string{"index.html", "index.htm"}, + "port": "80", + "sessionName": "HOTIME", } var ConfigNote = Map{