/
router.go
76 lines (59 loc) · 2.16 KB
/
router.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/NYTimes/gziphandler"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"net/http"
"path"
"strings"
)
// CreateRouter creates a new router with all HTTP handlers and middlewares.
func CreateRouter() http.Handler {
// Instanciate a router cf. https://github.com/julienschmidt/httprouter
router := httprouter.New()
// Middlewares shared by all dynamic pages
commonMiddleware := alice.New(
gziphandler.GzipHandler,
RedirectWWW,
)
// Main HTML search route
router.Handler("GET", "/", commonMiddleware.ThenFunc(SearchHandler))
// Main JSON search route
router.Handler("GET", "/api/search", commonMiddleware.ThenFunc(APISearchHandler))
// Static asset directories
ServeStaticDirectory(router, "js", true)
ServeStaticDirectory(router, "css", true)
ServeStaticDirectory(router, "img", false)
// We have a whitelist of allowed static files in the root directory
staticFiles := []string{"/favicon.ico", "/apple-touch-icon-precomposed.png"}
for _, file := range staticFiles {
router.Handler("GET", file, http.FileServer(http.Dir(path.Join(Config.PathFront, "static/"))))
}
return router
}
// ServeStaticDirectory allows any file inside a directory to be served over HTTP.
func ServeStaticDirectory(r *httprouter.Router, directory string, gzip bool) {
fileServer := http.FileServer(http.Dir(path.Join(Config.PathFront, "static/"+directory)))
gzFileServer := gziphandler.GzipHandler(fileServer)
handler := func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
req.URL.Path = ps.ByName("filepath")
if gzip {
gzFileServer.ServeHTTP(w, req)
} else {
fileServer.ServeHTTP(w, req)
}
}
r.GET("/"+directory+"/*filepath", handler)
}
// RedirectWWW is a midddleware that sends a redirect to the client based on the host
func RedirectWWW(handler http.Handler) http.Handler {
middleware := func(w http.ResponseWriter, r *http.Request) {
host := strings.Split(r.Host, ":")[0]
if host == "www.commonsearch.org" || host == "commonsearch.org" {
http.Redirect(w, r, "https://about.commonsearch.org/", http.StatusFound)
} else {
handler.ServeHTTP(w, r)
}
}
return http.HandlerFunc(middleware)
}