-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
196 lines (165 loc) · 4.44 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"net"
"net/http"
"strings"
"time"
"net/http/pprof"
_ "net/http/pprof"
"github.com/JojiiOfficial/ZimWiki/handlers"
"github.com/JojiiOfficial/ZimWiki/zim"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
// Route defining a route
type Route struct {
Name string
Methods []HTTPMethod
Pattern string
HandlerFunc RouteFunction
}
func (r Route) getMethods() []string {
methods := make([]string, len(r.Methods))
for i := range r.Methods {
methods[i] = string(r.Methods[i])
}
return methods
}
// HTTPMethod http method. GET, POST, DELETE, HEADER, etc...
type HTTPMethod string
// HTTP methods
const (
GetMethod HTTPMethod = "GET"
POSTMethod HTTPMethod = "POST"
PUTMethod HTTPMethod = "PUT"
DeleteMethod HTTPMethod = "DELETE"
)
// Routes all HTTP routes
type Routes []Route
// RouteFunction function for handling a route
type RouteFunction func(http.ResponseWriter, *http.Request, handlers.HandlerData) error
// Routes
var (
globalRoutes = Routes{
// -- Index routes
// Main/Home pages and aliases
{
Name: "IndexRoot",
Pattern: "/",
Methods: []HTTPMethod{GetMethod},
HandlerFunc: handlers.Index,
},
{
Name: "IndexHtml",
Pattern: "/index.html",
Methods: []HTTPMethod{GetMethod},
HandlerFunc: handlers.Index,
},
// -- Assets
// Requests for static files
{
Name: "",
Pattern: "/assets/{type}/{file}",
Methods: []HTTPMethod{GetMethod},
HandlerFunc: handlers.Assets,
},
{
Name: "Search",
Pattern: "/search/{wiki}/",
Methods: []HTTPMethod{GetMethod, POSTMethod},
HandlerFunc: handlers.Search,
},
}
)
// WikiRoutes
var (
// Raw wiki page
wikiRaw = Route{
Name: "",
Methods: []HTTPMethod{GetMethod},
HandlerFunc: handlers.WikiRaw,
}
// Raw wiki page
wikiView = Route{
Name: "WikiView",
Methods: []HTTPMethod{GetMethod},
HandlerFunc: handlers.WikiView,
}
)
// NewRouter create new router and its required components
func NewRouter(zimService *zim.Handler) *mux.Router {
hd := handlers.HandlerData{
ZimService: zimService,
}
router := mux.NewRouter().StrictSlash(true)
for _, route := range globalRoutes {
router.
Methods(route.getMethods()...).
Path(route.Pattern).
Name(route.Name).
Handler(RouteHandler(route.HandlerFunc, route.Name, hd))
}
// Add view handler
router.Methods(wikiView.getMethods()...).
PathPrefix("/wiki/view/").
Name(wikiView.Name).
Handler(RouteHandler(wikiView.HandlerFunc, wikiView.Name, hd))
// Add raw handler
router.Methods(wikiRaw.getMethods()...).
PathPrefix("/wiki/raw/").
Name(wikiRaw.Name).
Handler(RouteHandler(wikiRaw.HandlerFunc, wikiRaw.Name, hd))
attachProfiler(router)
return router
}
func attachProfiler(router *mux.Router) {
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
// Manually add support for paths linked to by index page at /debug/pprof/
router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handle("/debug/pprof/block", pprof.Handler("block"))
router.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
}
// RouteHandler logs stuff
func RouteHandler(inner RouteFunction, name string, hd handlers.HandlerData) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := r.Body.Close(); err != nil {
log.Error(err)
}
}()
// Only debug routes which have a names
needDebug := len(name) > 0
if needDebug {
log.Infof("[%s] %s\n", r.Method, name)
}
start := time.Now()
// Set accept gzip
hd.AcceptGzip = strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
// Use ResponseProxy
// This way we can automatically
// send content compressed using gzip
rp := handlers.NewResponseProxy(hd.AcceptGzip, w)
defer rp.Done()
// Process request and handle its error
if err := inner(rp, r, hd); err != nil {
if _, ok := err.(*net.OpError); ok {
log.Warn(err)
return
}
if err != handlers.ErrNotFound {
sendServerError(rp)
}
log.Error(err)
return
}
// Print duration of processing
if needDebug {
printProcessingDuration(start)
}
})
}