diff --git a/application.go b/application.go index 5b18446..6189e9f 100644 --- a/application.go +++ b/application.go @@ -164,7 +164,8 @@ func (that *Application) Run(router Router) { func (that *Application) SetConnectDB(connect func(err ...*Error) (master, slave *sql.DB)) { that.connectDbFunc = connect - that.Db.SetConnect(that.connectDbFunc) + + that.Db.SetConnect(that.connectDbFunc, &that.Error) } @@ -197,6 +198,7 @@ func (that *Application) SetCache(cache CacheIns) { func (that *Application) SetConfig(configPath ...string) { that.Log = GetLog("", true) + that.Error = Error{Logger: that.Log} if len(configPath) != 0 { that.configPath = configPath[0] } @@ -223,7 +225,7 @@ func (that *Application) SetConfig(configPath ...string) { } that.Log = GetLog(that.Config.GetString("logFile"), true) - + that.Error = Error{Logger: that.Log} if that.Config.GetBool("webConnectLogShow") { that.WebConnectLog = GetLog(that.Config.GetString("webConnectLogFile"), false) } @@ -465,6 +467,7 @@ func Init(config string) Application { appIns := Application{} //手动模式, appIns.SetConfig(config) + SetDB(&appIns) //appIns.SetCache() return appIns @@ -483,6 +486,8 @@ func SetDB(appIns *Application) { } } func SetMysqlDB(appIns *Application, config Map) { + + appIns.Db.Type = "mysql" appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { //master数据库配置 query := config.GetString("user") + ":" + config.GetString("password") + @@ -507,9 +512,10 @@ func SetMysqlDB(appIns *Application, config Map) { return master, slave //return DB }) - appIns.Db.Type = "mysql" } func SetSqliteDB(appIns *Application, config Map) { + + appIns.Db.Type = "sqlite" appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { db, e := sql.Open("sqlite3", config.GetString("path")) if e != nil && len(err) != 0 { @@ -519,5 +525,4 @@ func SetSqliteDB(appIns *Application, config Map) { return master, slave }) - appIns.Db.Type = "sqlite" } diff --git a/cache/cache_db.go b/cache/cache_db.go index bac059b..a20fefe 100644 --- a/cache/cache_db.go +++ b/cache/cache_db.go @@ -10,7 +10,7 @@ import ( type HoTimeDBInterface interface { Query(query string, args ...interface{}) []Map - Exec(query string, args ...interface{}) (sql.Result, Error) + Exec(query string, args ...interface{}) (sql.Result, *Error) Get(table string, qu ...interface{}) Map Select(table string, qu ...interface{}) []Map Delete(table string, data map[string]interface{}) int64 diff --git a/common/error.go b/common/error.go index 39647df..04e73c3 100644 --- a/common/error.go +++ b/common/error.go @@ -1,7 +1,12 @@ package common +import ( + "github.com/sirupsen/logrus" +) + // Error 框架层处理错误 type Error struct { + *logrus.Logger error } @@ -13,5 +18,10 @@ func (that *Error) GetError() error { func (that *Error) SetError(err error) { that.error = err + if that.Logger != nil { + //that.Logger=log.GetLog("",false) + that.Logger.Warn(err) + } + return } diff --git a/context.go b/context.go index 2b452e2..c7f315c 100644 --- a/context.go +++ b/context.go @@ -10,7 +10,6 @@ import ( type Context struct { *Application - ContextBase Resp http.ResponseWriter Req *http.Request RouterString []string diff --git a/db/db.go b/db/db.go index 2920dcf..9324d93 100644 --- a/db/db.go +++ b/db/db.go @@ -6,6 +6,8 @@ import ( "database/sql" "encoding/json" "errors" + _ "github.com/go-sql-driver/mysql" + _ "github.com/mattn/go-sqlite3" "os" "reflect" "strings" @@ -20,7 +22,7 @@ type HoTimeDB struct { LastQuery string LastData []interface{} ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB) - LastErr Error + LastErr *Error limit Slice *sql.Tx //事务对象 SlaveDB *sql.DB @@ -67,11 +69,11 @@ func (that *HoTimeDB) Action(action func(db HoTimeDB) bool) bool { return result } -func (that *HoTimeDB) InitDb(err ...*Error) Error { +func (that *HoTimeDB) InitDb(err ...*Error) *Error { if len(err) != 0 { - that.LastErr = *(err[0]) + that.LastErr = err[0] } - that.DB, that.SlaveDB = that.ConnectFunc(&that.LastErr) + that.DB, that.SlaveDB = that.ConnectFunc(that.LastErr) if that.DB == nil { return that.LastErr } @@ -149,7 +151,7 @@ func (that *HoTimeDB) Row(resl *sql.Rows) []Map { if e != nil { that.LastErr.SetError(e) } else { - lis.JsonToMap(string(jlis), &that.LastErr) + lis.JsonToMap(string(jlis), that.LastErr) } dest = append(dest, lis) @@ -342,7 +344,7 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map { return that.Row(resl) } -func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error) { +func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) { that.LastQuery = query that.LastData = args diff --git a/example/main.go b/example/main.go index d347624..dad347a 100644 --- a/example/main.go +++ b/example/main.go @@ -3,8 +3,6 @@ package main import ( "../../hotime" "../../hotime/cache" - "../../hotime/common" - "database/sql" "fmt" "golang.org/x/net/websocket" "time" @@ -12,8 +10,8 @@ import ( func main() { - appIns := hotime.Application{} - + //appIns := hotime.Application{} + appIns := hotime.Init("example/config/config.json") 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, ":"))) @@ -22,7 +20,7 @@ func main() { }) //手动模式, - appIns.SetConfig("example/config/config.json") + //appIns.SetConfig("example/config/config.json") //redis缓存接入 ca := cache.CacheIns(&cache.CacheRedis{Host: appIns.Config.GetString("redisHost"), Pwd: appIns.Config.GetString("redisPwd"), Time: appIns.Config.GetCeilInt64("cacheLongTime")}) ca.Cache("xyzm", "dasdas") @@ -38,16 +36,16 @@ func main() { appIns.SetCache(cache.CacheIns(&cache.CacheMemory{})) //快捷模式 - appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *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, nil - }) - + //appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *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, nil + //}) + //init appIns.Run(hotime.Router{ "app": hotime.Proj{ "index": hotime.Ctr{