优化多服务

This commit is contained in:
hoteas
2017-10-27 04:28:47 +00:00
parent fdc4a00176
commit 7423a7dd57
7 changed files with 76 additions and 72 deletions
+22 -7
View File
@@ -22,9 +22,15 @@ type Application struct {
configPath string
Config Map
Db HoTimeDB
Server *http.Server
CacheIns
sessionLong CacheIns
sessionShort CacheIns
http.Handler
}
func (this *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
this.handler(w, req)
}
//启动实例
@@ -50,7 +56,15 @@ func (this *Application) Run(router Router) {
}
}()
run(this)
this.Server = &http.Server{}
if !IsRun {
IsRun = true
}
App[this.Port] = this
this.Server.Handler = this
//启动服务
this.Server.Addr = ":" + this.Port
this.Server.ListenAndServe()
}
@@ -71,7 +85,7 @@ func (this *Application) SetSession(short CacheIns, Long CacheIns) {
func (this *Application) SetDefault(connect func(err ...*Error) *sql.DB) {
this.SetConfig()
this.connectDbFunc = connect
if this.connectDbFunc!=nil{
if this.connectDbFunc != nil {
this.Db.SetConnect(this.connectDbFunc)
}
@@ -196,7 +210,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
// 如果token存在就判断token是否在Session中有保存
// 如果有取出token并复制给cookie
// 没有保存就生成随机的session
cookie, err := req.Cookie((Config["sessionName"]).(string))
cookie, err := req.Cookie((this.Config["sessionName"]).(string))
sessionId := Md5(strconv.Itoa(Rand(10)))
token := req.FormValue("token")
//isFirst:=false
@@ -207,7 +221,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
//else{
// isFirst=true;
//}
http.SetCookie(w, &http.Cookie{Name: Config["sessionName"].(string), Value: sessionId, Path: "/"})
http.SetCookie(w, &http.Cookie{Name: this.Config["sessionName"].(string), Value: sessionId, Path: "/"})
} else {
sessionId = cookie.Value
}
@@ -250,13 +264,14 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
}
//url赋值
path := Config.GetString("tpt") + o
path := this.Config.GetString("tpt") + o
//判断是否为默认
if path[len(path)-1] == '/' {
defFile := Config["defFile"].([]string)
defFile := this.Config.GetSlice("defFile")
for i := 0; i < len(defFile); i++ {
temp := path + defFile[i]
temp := path + defFile.GetString(i)
_, err := os.Stat(temp)
if err == nil {