This commit is contained in:
hoteas
2017-08-22 08:24:55 +00:00
parent 0219261dac
commit 4a0f8d9b88
4 changed files with 247 additions and 102 deletions
+58 -48
View File
@@ -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()