2014-01-09 06:42:05 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-01-09 23:18:49 +00:00
|
|
|
|
|
|
|
ctx "github.com/gorilla/context"
|
2014-01-09 06:42:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Use allows us to stack middleware to process the request
|
|
|
|
// Example taken from https://github.com/gorilla/mux/pull/36#issuecomment-25849172
|
|
|
|
func Use(handler http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
|
|
|
|
for _, m := range middleware {
|
|
|
|
handler = m(handler)
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContext wraps each request in a function which fills in the context for a given request.
|
|
|
|
// This includes setting the User and Session keys and values as necessary for use in later functions.
|
|
|
|
func GetContext(handler http.Handler) http.Handler {
|
|
|
|
// Set the context here
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-01-09 23:18:49 +00:00
|
|
|
// Set the context appropriately here.
|
2014-01-09 06:42:05 +00:00
|
|
|
handler.ServeHTTP(w, r)
|
2014-01-09 23:18:49 +00:00
|
|
|
ctx.Clear(r)
|
2014-01-09 06:42:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequireLogin is a simple middleware which checks to see if the user is currently logged in.
|
|
|
|
// If not, the function returns a 302 redirect to the login page.
|
|
|
|
func RequireLogin(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Println("RequireLogin called!!")
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|