-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
45 lines (39 loc) · 1.22 KB
/
handlers.go
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
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/gorilla/handlers"
)
func RedirectMainPageHandler() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Add("Location", fmt.Sprintf("/view/%s", *mainPage))
writer.WriteHeader(http.StatusSeeOther)
}
}
func LoggingHandler(dst io.Writer) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return handlers.LoggingHandler(dst, h)
}
}
func LowerCaseCanonical(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
url := request.URL
url.Path = strings.ToLower(url.Path)
if url.RequestURI() != request.RequestURI {
http.Redirect(writer, request, url.RequestURI(), http.StatusPermanentRedirect)
} else {
next.ServeHTTP(writer, request)
}
})
}
func StripSlashes(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/" && strings.HasSuffix(request.URL.Path, "/") {
http.Redirect(writer, request, strings.TrimSuffix(request.URL.Path, "/"), http.StatusPermanentRedirect)
return
}
next.ServeHTTP(writer, request)
})
}