From 474a16a7a00243c3ae5a0c224a6ee27d58e096c6 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 4 Aug 2014 15:31:27 +0800 Subject: [PATCH] beego: improve the static file server --- beego.go | 4 ---- router.go | 12 +++++++++++- staticfile.go | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/beego.go b/beego.go index 05737a88..8dbed61b 100644 --- a/beego.go +++ b/beego.go @@ -383,10 +383,6 @@ func initBeforeHttpRun() { middleware.AppName = AppName middleware.RegisterErrorHandler() - for u, _ := range StaticDir { - Get(u, serverStaticRouter) - Get(u+"/*", serverStaticRouter) - } if EnableDocs { Get("/docs", serverDocs) Get("/docs/*", serverDocs) diff --git a/router.go b/router.go index f66915cb..254e8cd7 100644 --- a/router.go +++ b/router.go @@ -32,7 +32,8 @@ import ( const ( // default filter execution points - BeforeRouter = iota + BeforeStatic = iota + BeforeRouter BeforeExec AfterExec FinishRouter @@ -577,6 +578,15 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) return false } + if do_filter(BeforeStatic) { + goto Admin + } + + serverStaticRouter(context) + if w.started { + goto Admin + } + // session init if SessionOn { context.Input.CruSession = GlobalSessions.SessionStart(w, r) diff --git a/staticfile.go b/staticfile.go index 7fa03b54..1881b711 100644 --- a/staticfile.go +++ b/staticfile.go @@ -22,6 +22,9 @@ import ( ) func serverStaticRouter(ctx *context.Context) { + if ctx.Input.Method() != "GET" && ctx.Input.Method() != "HEAD" { + return + } requestPath := path.Clean(ctx.Input.Request.URL.Path) for prefix, staticDir := range StaticDir { if len(prefix) == 0 {