-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.go
More file actions
58 lines (48 loc) · 1.28 KB
/
Copy pathroutes.go
File metadata and controls
58 lines (48 loc) · 1.28 KB
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
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"politics/go/server"
"path/filepath"
"log"
)
func Router(s *server.Server) *mux.Router {
r := mux.NewRouter().StrictSlash(true)
if s.Flags.Reload {
r.Use(makeMiddleware(s, reloadMiddle))
}
r.HandleFunc("/", makeHandler(s, front))
r.HandleFunc("/rl/", makeHandler(s, reload))
r.PathPrefix("/files/").HandlerFunc(makeHandler(s, files))
r.PathPrefix("/static/").HandlerFunc(makeHandler(s, static))
return r
}
func makeHandler(s *server.Server, fn func(*server.Server, http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fn(s, w, r)
}
}
func makeMiddleware(s *server.Server, fn func(*server.Server, http.Handler) http.Handler) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return fn(s, next)
}
}
func reloadMiddle(s *server.Server, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if filepath.Ext(r.URL.Path) == "" {
err := s.Load()
if err != nil {
log.Println(err)
}
}
next.ServeHTTP(w, r)
})
}
func reload(s *server.Server, w http.ResponseWriter, r *http.Request) {
err := s.Load()
if err != nil {
fmt.Fprint(w, err)
}
fmt.Fprint(w, "ok")
}