1
This commit is contained in:
parent
72fe2ec77d
commit
089d415abc
16
context.go
16
context.go
@ -13,6 +13,7 @@ type Context struct {
|
|||||||
l *q5.ListHead
|
l *q5.ListHead
|
||||||
aborted bool
|
aborted bool
|
||||||
suspended bool
|
suspended bool
|
||||||
|
userData map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Context) do() {
|
func (this *Context) do() {
|
||||||
@ -45,7 +46,11 @@ func (this *Context) Request(name string) *q5.XValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *Context) Header(name string) string {
|
func (this *Context) Header(name string) string {
|
||||||
|
if val, ok := this.r.Header[name]; ok {
|
||||||
|
return val[0]
|
||||||
|
} else {
|
||||||
return ""
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Context) Response(data string) {
|
func (this *Context) Response(data string) {
|
||||||
@ -64,8 +69,19 @@ func (this *Context) ResponseOk() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *Context) Set(key string, val interface{}) {
|
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{} {
|
func (this *Context) Get(key string) interface{} {
|
||||||
|
if this.userData == nil {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
if val, ok := this.userData[key]; ok {
|
||||||
|
return val
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,13 +21,19 @@ type HttpServer struct {
|
|||||||
restHandlersMutex sync.RWMutex
|
restHandlersMutex sync.RWMutex
|
||||||
restHandlers map[string]*q5.ListHead
|
restHandlers map[string]*q5.ListHead
|
||||||
|
|
||||||
|
groupHandlersMutex sync.RWMutex
|
||||||
|
groupHandlers map[string]*q5.ListHead
|
||||||
|
|
||||||
globalMiddlewareMutex sync.RWMutex
|
globalMiddlewareMutex sync.RWMutex
|
||||||
globalMiddlewareList q5.ListHead
|
globalMiddlewareList q5.ListHead
|
||||||
|
|
||||||
|
currGroups []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
|
func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
|
||||||
this.handlers = make(map[string]HandlerFunc)
|
this.handlers = make(map[string]HandlerFunc)
|
||||||
this.restHandlers = make(map[string]*q5.ListHead)
|
this.restHandlers = make(map[string]*q5.ListHead)
|
||||||
|
this.groupHandlers = make(map[string]*q5.ListHead)
|
||||||
this.globalMiddlewareList.Init(nil)
|
this.globalMiddlewareList.Init(nil)
|
||||||
this.RegisterRestHandle("/webapp/index", this.dispatchRequest)
|
this.RegisterRestHandle("/webapp/index", this.dispatchRequest)
|
||||||
this.RegisterHandle("Ops", "selfChecking",
|
this.RegisterHandle("Ops", "selfChecking",
|
||||||
@ -111,13 +117,15 @@ func (this *HttpServer) RegisterRestHandle(pattern string,
|
|||||||
defer this.restHandlersMutex.Unlock()
|
defer this.restHandlersMutex.Unlock()
|
||||||
|
|
||||||
p := new(middleware)
|
p := new(middleware)
|
||||||
p.entry.Init(nil)
|
p.entry.Init(p)
|
||||||
p.handlerFunc = handle
|
p.handlerFunc = handle
|
||||||
if l, ok := this.restHandlers[pattern]; ok {
|
if l, ok := this.restHandlers[pattern]; ok {
|
||||||
l.AddTail(&p.entry)
|
l.AddTail(&p.entry)
|
||||||
|
this.applyCurrGroup(l)
|
||||||
} else {
|
} else {
|
||||||
l := q5.MakeListHead()
|
l := q5.MakeListHead()
|
||||||
l.AddTail(&p.entry)
|
l.AddTail(&p.entry)
|
||||||
|
this.applyCurrGroup(l)
|
||||||
this.restHandlers[pattern] = l
|
this.restHandlers[pattern] = l
|
||||||
http.HandleFunc(pattern,
|
http.HandleFunc(pattern,
|
||||||
func (w http.ResponseWriter, r *http.Request) {
|
func (w http.ResponseWriter, r *http.Request) {
|
||||||
@ -150,16 +158,70 @@ func (this *HttpServer) installGlobalMiddleware() {
|
|||||||
defer this.restHandlersMutex.Unlock()
|
defer this.restHandlersMutex.Unlock()
|
||||||
defer this.globalMiddlewareMutex.Unlock()
|
defer this.globalMiddlewareMutex.Unlock()
|
||||||
|
|
||||||
/*for key, val := range this.restHandlers {
|
for _, l := range this.restHandlers {
|
||||||
|
this.globalMiddlewareList.ForEach_r(
|
||||||
}*/
|
func (data interface{}) bool {
|
||||||
|
m := data.(*middleware)
|
||||||
|
p := new(middleware)
|
||||||
|
p.entry.Init(p)
|
||||||
|
p.handlerFunc = m.handlerFunc
|
||||||
|
l.AddHead(&p.entry)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) UseGroupBegin(groupNames ...string) {
|
func (this *HttpServer) UseGroupBegin(groupNames ...string) {
|
||||||
|
if this.currGroups != nil {
|
||||||
|
panic("")
|
||||||
|
}
|
||||||
|
this.currGroups = groupNames
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) UseGroupEnd() {
|
func (this *HttpServer) UseGroupEnd() {
|
||||||
|
if this.currGroups == nil {
|
||||||
|
panic("")
|
||||||
|
}
|
||||||
|
this.currGroups = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *HttpServer) applyCurrGroup(l *q5.ListHead) {
|
||||||
|
if this.currGroups != nil {
|
||||||
|
for _, groupName := range this.currGroups {
|
||||||
|
this.groupHandlersMutex.Lock()
|
||||||
|
defer this.groupHandlersMutex.Unlock()
|
||||||
|
|
||||||
|
gl, ok := this.groupHandlers[groupName];
|
||||||
|
if ok {
|
||||||
|
gl.ForEach_r(
|
||||||
|
func (data interface{}) bool {
|
||||||
|
m := data.(*middleware)
|
||||||
|
p := new(middleware)
|
||||||
|
p.entry.Init(p)
|
||||||
|
p.handlerFunc = m.handlerFunc
|
||||||
|
l.AddHead(&p.entry)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
panic("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) DefineGroup(groupName string, handlers ...HandlerFunc) {
|
func (this *HttpServer) DefineGroup(groupName string, handlers ...HandlerFunc) {
|
||||||
|
this.groupHandlersMutex.Lock()
|
||||||
|
defer this.groupHandlersMutex.Unlock()
|
||||||
|
|
||||||
|
l, ok := this.groupHandlers[groupName];
|
||||||
|
if !ok {
|
||||||
|
l = q5.MakeListHead()
|
||||||
|
this.groupHandlers[groupName] = l
|
||||||
|
}
|
||||||
|
for i := 0; i < len(handlers); i++ {
|
||||||
|
p := new(middleware)
|
||||||
|
p.entry.Init(p)
|
||||||
|
p.handlerFunc = handlers[i]
|
||||||
|
l.AddTail(&p.entry)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user