1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-02 13:00:17 +00:00

support profile & statistics in another port

This commit is contained in:
astaxie
2013-11-13 21:11:03 +08:00
parent a981dab536
commit 6c13bdde25
9 changed files with 364 additions and 27 deletions

View File

@ -2,6 +2,7 @@ package beego
import (
"fmt"
"github.com/astaxie/beego/admin"
beecontext "github.com/astaxie/beego/context"
"github.com/astaxie/beego/middleware"
"net/http"
@ -12,6 +13,7 @@ import (
"runtime"
"strconv"
"strings"
"time"
)
var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
@ -406,6 +408,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}()
starttime := time.Now()
requestPath := r.URL.Path
var runrouter *controllerInfo
var findrouter bool
params := make(map[string]string)
w := &responseWriter{writer: rw}
w.Header().Set("Server", BeegoServerName)
context := &beecontext.Context{
@ -422,27 +430,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Output = beecontext.NewOutput(rw)
}
if SessionOn {
context.Input.CruSession = GlobalSessions.SessionStart(w, r)
}
if !inSlice(strings.ToLower(r.Method), HTTPMETHOD) {
http.Error(w, "Method Not Allowed", 405)
return
goto Admin
}
var runrouter *controllerInfo
var findrouter bool
params := make(map[string]string)
if p.enableFilter {
if l, ok := p.filters["BeforRouter"]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
@ -455,7 +454,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
file := staticDir + r.URL.Path
http.ServeFile(w, r, file)
w.started = true
return
goto Admin
}
if strings.HasPrefix(r.URL.Path, prefix) {
file := staticDir + r.URL.Path[len(prefix):]
@ -465,32 +464,36 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
Warn(err)
}
http.NotFound(w, r)
return
goto Admin
}
//if the request is dir and DirectoryIndex is false then
if finfo.IsDir() && !DirectoryIndex {
middleware.Exception("403", rw, r, "403 Forbidden")
return
goto Admin
}
http.ServeFile(w, r, file)
w.started = true
return
goto Admin
}
}
// session init after static file
if SessionOn {
context.Input.CruSession = GlobalSessions.SessionStart(w, r)
}
if p.enableFilter {
if l, ok := p.filters["AfterStatic"]; ok {
for _, filterR := range l {
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
}
}
requestPath := r.URL.Path
if CopyRequestBody {
context.Input.Body()
@ -509,7 +512,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if requestPath[n-1] != '/' && len(route.pattern) == n+1 &&
route.pattern[n] == '/' && route.pattern[:n] == requestPath {
http.Redirect(w, r, requestPath+"/", 301)
return
goto Admin
}
}
@ -546,6 +549,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
break
}
}
context.Input.Param = params
if runrouter != nil {
@ -559,7 +563,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
@ -714,7 +718,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
@ -740,7 +744,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if strings.ToLower(requestPath) == "/"+cName {
http.Redirect(w, r, requestPath+"/", 301)
return
goto Admin
}
if strings.ToLower(requestPath) == "/"+cName+"/" {
@ -754,6 +758,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if r.Method == "POST" {
r.ParseMultipartForm(MaxMemory)
}
// set find
findrouter = true
//execute middleware filters
if p.enableFilter {
if l, ok := p.filters["BeforExec"]; ok {
@ -761,7 +767,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
@ -816,7 +822,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if filterR.ValidRouter(r.URL.Path) {
filterR.filterFunc(context)
if w.started {
return
goto Admin
}
}
}
@ -824,9 +830,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
method = vc.MethodByName("Destructor")
method.Call(in)
// set find
findrouter = true
goto Last
goto Admin
}
}
}
@ -834,11 +838,16 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
Last:
//if no matches to url, throw a not found exception
if !findrouter {
middleware.Exception("404", rw, r, "")
}
Admin:
//admin module record QPS
if EnableAdmin {
go admin.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), time.Since(starttime))
}
}
//responseWriter is a wrapper for the http.ResponseWriter