40 lines
827 B
Go
40 lines
827 B
Go
|
package routes
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"log"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
type Middleware func(http.HandlerFunc) http.HandlerFunc
|
||
|
|
||
|
func MultipleMiddleware(h http.HandlerFunc, m ...Middleware) http.HandlerFunc {
|
||
|
if len(m) < 1 {
|
||
|
return h
|
||
|
}
|
||
|
|
||
|
wrapped := h
|
||
|
|
||
|
for i := len(m) - 1; i >= 0; i-- {
|
||
|
wrapped = m[i](wrapped)
|
||
|
}
|
||
|
|
||
|
return wrapped
|
||
|
}
|
||
|
|
||
|
func authMiddleware(h http.HandlerFunc) http.HandlerFunc {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
log.SetOutput(os.Stdout)
|
||
|
log.Println(r.Method, r.URL)
|
||
|
h.ServeHTTP(w, r)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func logMiddleware(h http.HandlerFunc) http.HandlerFunc {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
log.SetOutput(os.Stdout)
|
||
|
log.Println(r.Method, r.URL)
|
||
|
h.ServeHTTP(w, r)
|
||
|
})
|
||
|
}
|