This commit is contained in:
aozhiwei 2020-10-30 13:56:08 +08:00
parent 291f7a680e
commit 69df47f48e

View File

@ -15,15 +15,16 @@ type HttpServer struct {
maxHandleTime int64
handlersMutex sync.RWMutex
handlers map[string]func(http.ResponseWriter, *http.Request)
handlers map[string]func(*http.ResponseWriter, *http.Request)
}
func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
this.handlers = make(map[string]func(http.ResponseWriter, *http.Request))
this.handlers = make(map[string]func(*http.ResponseWriter, *http.Request))
http.HandleFunc("/webapp/index.php", this.dispatchRequest)
this.RegisterHandle("Ops", "selfChecking", func (w http.ResponseWriter,r *http.Request) {
w.Write([]byte(`{"errcode":0, "errmsg":"", "healthy":1, "max_rundelay":10}`))
})
this.RegisterHandle("Ops", "selfChecking",
func (w* http.ResponseWriter, r *http.Request) {
(*w).Write([]byte(`{"errcode":0, "errmsg":"", "healthy":1, "max_rundelay":10}`))
})
SysLog().Info("HttpServer.Init")
if logOutputTime > 0 {
Timer().AddRepeatTimer(
@ -61,7 +62,7 @@ func (this *HttpServer) dispatchRequest(w http.ResponseWriter, r *http.Request)
handle := this.getHandle(handleName)
if handle != nil {
beginTick := q5.GetTickCount()
handle(w, r)
handle(&w, r)
endTick := q5.GetTickCount()
if oldVal := atomic.LoadInt64(&this.maxHandleTime); beginTick - endTick > oldVal {
atomic.CompareAndSwapInt64(&this.maxHandleTime, oldVal, endTick - beginTick)
@ -73,14 +74,15 @@ func (this *HttpServer) dispatchRequest(w http.ResponseWriter, r *http.Request)
}
}
func (this *HttpServer) getHandle(handleName string) (func(http.ResponseWriter, *http.Request)) {
func (this *HttpServer) getHandle(handleName string) (func(*http.ResponseWriter, *http.Request)) {
this.handlersMutex.Lock()
defer this.handlersMutex.Unlock()
handle, _ := this.handlers[handleName]
return handle
}
func (this *HttpServer) RegisterHandle(c string, a string, handle func(http.ResponseWriter, *http.Request)) {
func (this *HttpServer) RegisterHandle(c string, a string,
handle func(*http.ResponseWriter, *http.Request)) {
this.handlersMutex.Lock()
defer this.handlersMutex.Unlock()
handleName := c + "$" + a