整个包结构重构
This commit is contained in:
+72
-16
@@ -1,6 +1,9 @@
|
||||
package hotime
|
||||
|
||||
import (
|
||||
. "./cache"
|
||||
. "./common"
|
||||
. "./db"
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
@@ -17,7 +20,7 @@ import (
|
||||
type Application struct {
|
||||
MethodRouter
|
||||
Router
|
||||
contextBase
|
||||
ContextBase
|
||||
Log logrus.Logger
|
||||
Port string //端口号
|
||||
TLSPort string //ssl访问端口号
|
||||
@@ -50,14 +53,14 @@ func (this *Application) Run(router Router) {
|
||||
}
|
||||
|
||||
//防止手动设置session误伤
|
||||
if this.sessionShort == nil && this.sessionLong == nil {
|
||||
if this.connectDbFunc == nil {
|
||||
this.SetSession(CacheIns(&CacheMemory{}), nil)
|
||||
} else {
|
||||
this.SetSession(CacheIns(&CacheMemory{}), CacheIns(&CacheDb{Db: &this.Db, Time: this.Config.GetInt64("cacheLongTime")}))
|
||||
}
|
||||
|
||||
}
|
||||
//if this.sessionShort == nil && this.sessionLong == nil {
|
||||
// if this.connectDbFunc == nil {
|
||||
// this.SetSession(CacheIns(&CacheMemory{}), nil)
|
||||
// } else {
|
||||
// this.SetSession(CacheIns(&CacheMemory{}), CacheIns(&CacheDb{Db: &this.Db, Time: this.Config.GetInt64("cacheLongTime")}))
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
||||
this.Router = router
|
||||
//重新设置MethodRouter//直达路由
|
||||
@@ -105,7 +108,7 @@ func (this *Application) Run(router Router) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
//this.SetError(errors.New(fmt.Sprint(err)), LOG_FMT)
|
||||
logFmt(err, 2, LOG_ERROR)
|
||||
LogFmt(err, 2, LOG_ERROR)
|
||||
|
||||
this.Run(router)
|
||||
}
|
||||
@@ -125,7 +128,7 @@ func (this *Application) Run(router Router) {
|
||||
//启动服务
|
||||
this.Server.Addr = ":" + this.Port
|
||||
err := this.Server.ListenAndServe()
|
||||
logFmt(err, 2)
|
||||
LogFmt(err, 2)
|
||||
ch <- 1
|
||||
|
||||
}()
|
||||
@@ -139,20 +142,20 @@ func (this *Application) Run(router Router) {
|
||||
//启动服务
|
||||
this.Server.Addr = ":" + this.TLSPort
|
||||
err := this.Server.ListenAndServeTLS(this.Config.GetString("tlsCert"), this.Config.GetString("tlsKey"))
|
||||
logFmt(err, 2)
|
||||
LogFmt(err, 2)
|
||||
ch <- 2
|
||||
|
||||
}()
|
||||
}
|
||||
if ObjToCeilInt(this.Port) == 0 && ObjToCeilInt(this.TLSPort) == 0 {
|
||||
logFmt("没有端口启用", 2, LOG_INFO)
|
||||
LogFmt("没有端口启用", 2, LOG_INFO)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
value := <-ch
|
||||
|
||||
logFmt("启动服务失败 : "+ObjToStr(value), 2, LOG_ERROR)
|
||||
LogFmt("启动服务失败 : "+ObjToStr(value), 2, LOG_ERROR)
|
||||
}
|
||||
|
||||
//启动实例
|
||||
@@ -211,7 +214,7 @@ func (this *Application) SetConfig(configPath ...string) {
|
||||
Config[k] = v //系统配置
|
||||
}
|
||||
} else {
|
||||
logFmt("配置文件不存在,或者配置出错,使用缺省默认配置", 2)
|
||||
LogFmt("配置文件不存在,或者配置出错,使用缺省默认配置", 2)
|
||||
|
||||
}
|
||||
|
||||
@@ -320,7 +323,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
|
||||
this.crossDomain(&context)
|
||||
//是否展示日志
|
||||
if this.Config.GetInt("connectLogShow") != 0 {
|
||||
logFmt(Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))+" "+context.HandlerStr, 0, LOG_INFO)
|
||||
LogFmt(Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))+" "+context.HandlerStr, 0, LOG_INFO)
|
||||
}
|
||||
|
||||
//访问拦截true继续false暂停
|
||||
@@ -446,6 +449,7 @@ func (this *Application) crossDomain(context *Context) {
|
||||
header.Set("Access-Control-Allow-Origin", refer)
|
||||
}
|
||||
}
|
||||
|
||||
func Init(config string) Application {
|
||||
appIns := Application{}
|
||||
//手动模式,
|
||||
@@ -454,3 +458,55 @@ func Init(config string) Application {
|
||||
//appIns.SetCache()
|
||||
return appIns
|
||||
}
|
||||
|
||||
//后期整改
|
||||
func SetDB(appIns *Application) {
|
||||
db := appIns.Config.GetMap("db")
|
||||
dbSqlite := db.GetMap("sqlite")
|
||||
dbMysql := db.GetMap("mysql")
|
||||
if db != nil && dbSqlite != nil {
|
||||
SetSqliteDB(appIns, dbSqlite)
|
||||
}
|
||||
if db != nil && dbMysql != nil {
|
||||
SetMysqlDB(appIns, dbMysql)
|
||||
}
|
||||
}
|
||||
func SetMysqlDB(appIns *Application, config Map) {
|
||||
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
|
||||
//master数据库配置
|
||||
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)
|
||||
}
|
||||
master = DB
|
||||
//slave数据库配置
|
||||
configSlave := config.GetMap("slave")
|
||||
if configSlave != nil {
|
||||
query := configSlave.GetString("user") + ":" + configSlave.GetString("password") +
|
||||
"@tcp(" + config.GetString("host") + ":" + configSlave.GetString("port") + ")/" + configSlave.GetString("name") + "?charset=utf8"
|
||||
DB1, e := sql.Open("mysql", query)
|
||||
if e != nil && len(err) != 0 {
|
||||
err[0].SetError(e)
|
||||
}
|
||||
slave = DB1
|
||||
}
|
||||
|
||||
return master, slave
|
||||
//return DB
|
||||
})
|
||||
appIns.Db.Type = "mysql"
|
||||
}
|
||||
func SetSqliteDB(appIns *Application, config Map) {
|
||||
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
|
||||
db, e := sql.Open("sqlite3", config.GetString("path"))
|
||||
if e != nil && len(err) != 0 {
|
||||
err[0].SetError(e)
|
||||
}
|
||||
master = db
|
||||
|
||||
return master, slave
|
||||
})
|
||||
appIns.Db.Type = "sqlite"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user