-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.go
More file actions
54 lines (44 loc) · 1.33 KB
/
serve.go
File metadata and controls
54 lines (44 loc) · 1.33 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
package routex
import (
"net/http"
"strings"
)
func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, rt := range router.routes {
// MATCHING THE ROUTE
//check of accept trailing-slash settings. By default the router will accept them even if none is set in the Handle-Path
rt.route = strings.TrimRight(rt.route, "/")
if rt.acceptTrailingSlash && rt.route+"/" == r.URL.Path || rt.route == r.URL.Path {
// OTHER CHECKS
//check allowed methods
if !rt.checkMethods(r) {
forbidden(w)
return
}
//check if CORS are allowed, if so, set headers
rt.checkAllowCors(w)
// finally execute the configured HandlerFunc
rt.handlerfn(w, r)
return
}
}
// STATIC SERVING (will be always overruled in case of double configuration)
// check if route is configured for serving static files and serve them
for _, rt := range router.staticRoutes {
rt.route = strings.TrimRight(rt.route, "/")
if strings.Index(r.URL.Path, rt.route) == 0 {
staticServing(r.URL.Path, rt, w)
return
}
}
//NOT FOUND
//if the function rund until here, no route was configured for this request. Return a not found error.
notfound(w)
}
// error funcs
func forbidden(w http.ResponseWriter) {
w.WriteHeader(http.StatusForbidden)
}
func notfound(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
}