diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0477f02..098edff 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,8 +5,8 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -154,6 +227,20 @@ - @@ -454,12 +548,12 @@ - + - + @@ -495,23 +589,6 @@ - - - - - - - - - - - - - - - - - @@ -725,14 +802,6 @@ - - - - - - - - @@ -757,14 +826,6 @@ - - - - - - - - @@ -785,6 +846,7 @@ + @@ -806,6 +868,7 @@ + @@ -823,6 +886,7 @@ + @@ -830,6 +894,7 @@ + @@ -855,10 +920,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/application.go b/application.go index 507d43f..219d960 100644 --- a/application.go +++ b/application.go @@ -16,7 +16,7 @@ type Application struct { Router Error Port string //端口号 - connectListener func(context Context) bool //所有的访问监听,true按原计划继续使用,false表示有监听器处理 + connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用,false表示有监听器处理 connectDbFunc func(err ...*Error) *sql.DB configPath string Config Map @@ -142,8 +142,8 @@ func (this *Application) SetConfig(configPath ...string) { } //连接判断,返回true继续传输至控制层,false则停止传输 -func (this *Application) SetConnectListener(lis func(context Context) bool) { - this.connectListener = lis +func (this *Application) SetConnectListener(lis func(this *Context) bool) { + this.connectListener = append(this.connectListener, lis) } //网络错误 @@ -151,14 +151,13 @@ func (this *Application) session(w http.ResponseWriter, req *http.Request) { } -//访问 -func (this *Application) handler(w http.ResponseWriter, req *http.Request) { - - q := strings.Index(req.RequestURI, "?") +//序列化链接 +func (this *Application) urlSer(url string) (string, []string) { + q := strings.Index(url, "?") if q == -1 { - q = len(req.RequestURI) + q = len(url) } - o := Substr(req.RequestURI, 0, q) + o := Substr(url, 0, q) r := strings.SplitN(o, "/", -1) @@ -169,50 +168,61 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { s = append(s, r[i]) } } + return o, s +} +//访问 +func (this *Application) handler(w http.ResponseWriter, req *http.Request) { + + o, s := this.urlSer(req.RequestURI) + //获取cookie + // 如果cookie存在直接将sessionId赋值为cookie.Value + // 如果cookie不存在就查找传入的参数中是否有token + // 如果token不存在就生成随机的sessionId + // 如果token存在就判断token是否在Session中有保存 + // 如果有取出token并复制给cookie + // 没有保存就生成随机的session + cookie, err := req.Cookie((Config["sessionName"]).(string)) + sessionId := Md5(strconv.Itoa(Rand(10))) + token := req.FormValue("token") + //isFirst:=false + if err != nil || (len(token) == 32 && cookie.Value != token) { + if len(token) == 32 { + sessionId = token + } + //else{ + // isFirst=true; + //} + http.SetCookie(w, &http.Cookie{Name: Config["sessionName"].(string), Value: sessionId, Path: "/"}) + } else { + sessionId = cookie.Value + } + + //访问实例 + context := Context{SessionIns: SessionIns{SessionId: sessionId, + LongCache: this.sessionLong, + ShortCache: this.sessionShort, + }, + CacheIns: this.CacheIns, + Resp: w, Req: req, Application: this, RouterString: s, Config: this.Config, Db: &this.Db, HandlerStr: req.RequestURI} + + //访问拦截true继续false暂停 + connectListenerLen:=len(this.connectListener) + if connectListenerLen!= 0 { + for i := 0; i < connectListenerLen; i++ { + + if !this.connectListener[i](&context) { + context.View() + return + } + } + } + o, s = this.urlSer(context.HandlerStr) + context.RouterString = s //接口服务 if len(s) == 3 { //如果满足规则则路由到对应控制器去 if this.Router[s[0]] != nil && this.Router[s[0]][s[1]] != nil && this.Router[s[0]][s[1]][s[2]] != nil { - - //获取cookie - // 如果cookie存在直接将sessionId赋值为cookie.Value - // 如果cookie不存在就查找传入的参数中是否有token - // 如果token不存在就生成随机的sessionId - // 如果token存在就判断token是否在Session中有保存 - // 如果有取出token并复制给cookie - // 没有保存就生成随机的session - cookie, err := req.Cookie((Config["sessionName"]).(string)) - sessionId := Md5(strconv.Itoa(Rand(10))) - token := req.FormValue("token") - //isFirst:=false - if err != nil || (len(token) == 32 && cookie.Value != token) { - if len(token) == 32 { - sessionId = token - } - //else{ - // isFirst=true; - //} - http.SetCookie(w, &http.Cookie{Name: Config["sessionName"].(string), Value: sessionId, Path: "/"}) - } else { - sessionId = cookie.Value - } - - //访问实例 - context := Context{SessionIns: SessionIns{SessionId: sessionId, - LongCache: this.sessionLong, - ShortCache: this.sessionShort, - }, - CacheIns: this.CacheIns, - Resp: w, Req: req, Application: this, RouterString: s, Config: this.Config, Db: &this.Db} - - //访问拦截 - if this.connectListener != nil { - if !this.connectListener(context) { - context.View() - return - } - } //控制层 this.Router[s[0]][s[1]][s[2]](&context) context.View() diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..3d4ee02 --- /dev/null +++ b/config/config.json @@ -0,0 +1,18 @@ +{ + "cacheLongTime": 2592000, + "cacheShortTime": 7200, + "dbHost": "127.0.0.1", + "dbName": "test", + "dbPort": "3306", + "dbPwd": "root", + "dbUser": "root", + "defFile": [ + "index.html", + "index.htm" + ], + "error": {}, + "logLevel": 0, + "port": "80", + "sessionName": "HOTIME", + "tpt": "tpt" +} \ No newline at end of file diff --git a/context.go b/context.go index 3c5892e..f49fae7 100644 --- a/context.go +++ b/context.go @@ -2,9 +2,9 @@ package hotime import ( "encoding/json" + "errors" "net/http" "time" - "errors" ) type Context struct { @@ -12,13 +12,14 @@ type Context struct { tag int64 Resp http.ResponseWriter Req *http.Request - Application *Application + Application *Application RouterString []string Config Map Db *HoTimeDB - resp Map + RespData Map CacheIns SessionIns + HandlerStr string //复写请求url } //唯一标志 @@ -33,8 +34,8 @@ func (this *Context) GetTag() int64 { //唯一标志 func (this *Context) Mtd(router [3]string) Map { this.Application.Router[router[0]][router[1]][router[2]](this) - d := this.resp - this.resp = nil + d := this.RespData + this.RespData = nil return d } @@ -45,8 +46,8 @@ func (this *Context) Display(statu int, data interface{}) { if statu != 0 { temp := Map{} - tpe:=this.Config.GetMap("error").GetString(ObjToStr(statu)) - if tpe==""{ + tpe := this.Config.GetMap("error").GetString(ObjToStr(statu)) + if tpe == "" { this.LastErr.SetError(errors.New("找不到对应的错误码")) } @@ -57,19 +58,19 @@ func (this *Context) Display(statu int, data interface{}) { resp["result"] = data } - this.resp = resp + this.RespData = resp //this.Data=d; } func (this *Context) View() { - if this.resp == nil { + if this.RespData == nil { return } - d, err := json.Marshal(this.resp) + d, err := json.Marshal(this.RespData) if err != nil { return } - this.resp = nil + this.RespData = nil this.Resp.Write(d) }