f5/context.go
aozhiwei 17a44afd7c 1
2021-01-07 10:52:58 +08:00

104 lines
1.9 KiB
Go

package f5
import (
"net/http"
"q5"
)
type Context struct {
s *HttpServer
w *http.ResponseWriter
r *http.Request
c *middleware
l *q5.ListHead
aborted bool
suspended bool
userData map[string]interface{}
}
func (this *Context) do() {
this.l.ForEach(
func(data interface{}) bool {
m := data.(*middleware)
this.c = m
m.handlerFunc(this)
return !this.aborted && !this.suspended
})
}
func (this *Context) Next() {
this.suspended = true
this.l.ForEachFrom(this.c.entry.Next(),
func(data interface{}) bool {
m := data.(*middleware)
this.c = m
m.handlerFunc(this)
return !this.aborted
})
}
func (this *Context) Abort() {
this.aborted = true
}
func (this *Context) Method() string {
return this.r.Method
}
func (this *Context) GetRemoteAddr() string {
return q5.GetRequestRemoteAddr(this.r)
}
func (this *Context) Request(name string) *q5.XValue {
return q5.Request(this.r, name)
}
func (this *Context) GetBody() *q5.XValue {
return q5.GetPostBody(this.r)
}
func (this *Context) Header(name string) string {
if val, ok := this.r.Header[name]; ok {
return val[0]
} else {
return ""
}
}
func (this *Context) Response(data string) {
q5.Response(this.w, data)
}
func (this *Context)ResponseErr(errCode int32, errMsg string) {
respObj := q5.NewMxoObject()
respObj.SetXValue("errcode", q5.NewXInt32(errCode))
respObj.SetXValue("errmsg", q5.NewXString(errMsg))
q5.Response(this.w, respObj.ToJsonStr())
}
func (this *Context) ResponseOk() {
q5.ResponseOk(this.w)
}
func (this *Context) ResponseInt32Ok(key string, val int32) {
q5.ResponseInt32Ok(this.w, key, val)
}
func (this *Context) Set(key string, val interface{}) {
if this.userData == nil {
this.userData = make(map[string]interface{})
}
this.userData[key] = val
}
func (this *Context) Get(key string) interface{} {
if this.userData == nil {
return nil
}
if val, ok := this.userData[key]; ok {
return val
} else {
return nil
}
}