This repository was archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
62 lines (52 loc) · 1.4 KB
/
utils.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
package kwiscale
import (
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// Log print logs on STDOUT if debug is activated.
func Log(v ...interface{}) {
if debug {
log.Println(v...)
}
}
// Error prints error on STDOUT.
func Error(v ...interface{}) {
msg := []interface{}{"[ERROR]"}
msg = append(msg, v...)
log.Println(msg...)
}
// getMatchRoute returns the better handler that matched request url.
// It returns handlername, mux.Route and mux.RouteMatch.
//
// Rule is simple:
// - if A url path length is greater than B url path length, B wins
// - if A url path length is equal that B url path length, then:
// - if A number of path vars is greater than B number if path vars, B wins
//
// So:
//
// - /path/A vs /B => B wins
// - /path/A/{foo:.*} vs /path/B/bar => B wins
func getBestRoute(app *App, r *http.Request) (handlerName string, route *mux.Route, match mux.RouteMatch) {
points := -1
for handlerRoute, handler := range app.handlers {
var routematch mux.RouteMatch
if handlerRoute.Match(r, &routematch) {
plength := len(strings.Split(r.URL.Path, "/")) + len(routematch.Vars)
if points == -1 {
points = plength
} else if plength > points {
continue
}
Log("Matches route vars", handler, routematch.Vars)
points = plength
Log("Handler to fetch", handler)
handlerName = handler.handlername
route = handlerRoute
match = routematch
}
}
return
}