-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.go
More file actions
34 lines (27 loc) · 771 Bytes
/
Copy pathmiddleware.go
File metadata and controls
34 lines (27 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"log"
"net/http"
"strings"
"time"
)
func middlewareLogging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t0 := time.Now()
log.Printf("%v %v", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
log.Printf("request took %v", time.Now().Sub(t0))
})
}
func middlewareTrailingSlash(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
orig := r.URL.Path
trimmed := strings.TrimSuffix(orig, "/")
if orig != trimmed && len(trimmed) > 0 {
log.Printf("redirecting request to %v to %v", orig, trimmed)
http.RedirectHandler(trimmed, http.StatusMovedPermanently).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}