Skip to content

Commit

Permalink
middleware: don't use middleware when loading images
Browse files Browse the repository at this point in the history
check url for gets directed at "/assets" and "/captchas" and ignore them
in the middleware
  • Loading branch information
JoeGruffins committed Jun 29, 2019
1 parent 64d9c59 commit 27d4be5
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions system/middleware.go
Expand Up @@ -12,31 +12,39 @@ import (
"github.com/zenazn/goji/web"
)

// A custom http.HandlerFunc to apply to all middleware
func makeHandler(h http.Handler, fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// only process the middleware function if not a call to images
if url := r.URL.String(); !strings.HasPrefix(url, "/assets") && !strings.HasPrefix(url, "/captchas") {
fn(w, r)
}
h.ServeHTTP(w, r)
}
}

// Makes sure templates are stored in the context
func (application *Application) ApplyTemplates(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c.Env["Template"] = application.Template
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

// Makes sure controllers can have access to session
func (application *Application) ApplySessions(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session, _ := application.Store.Get(r, "session")
c.Env["Session"] = session
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

func (application *Application) ApplyDbMap(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c.Env["DbMap"] = application.DbMap
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

func (application *Application) ApplyAPI(c *web.C, h http.Handler) http.Handler {
Expand Down Expand Up @@ -69,9 +77,8 @@ func (application *Application) ApplyAPI(c *web.C, h http.Handler) http.Handler
}
}
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

func (application *Application) ApplyCaptcha(c *web.C, h http.Handler) http.Handler {
Expand All @@ -82,9 +89,8 @@ func (application *Application) ApplyCaptcha(c *web.C, h http.Handler) http.Hand
} else {
c.Env["CaptchaDone"] = false
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

func (application *Application) ApplyAuth(c *web.C, h http.Handler) http.Handler {
Expand All @@ -101,7 +107,6 @@ func (application *Application) ApplyAuth(c *web.C, h http.Handler) http.Handler
c.Env["User"] = user
}
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return makeHandler(h, fn)
}

0 comments on commit 27d4be5

Please sign in to comment.