-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.go
64 lines (54 loc) · 1.49 KB
/
dispatch.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
package main
import (
"database/sql"
"fmt"
"net/http"
"regexp"
"strings"
)
type RouteFunc func(db *sql.DB, params map[string]string, w http.ResponseWriter, r *http.Request)
type Route struct {
Path string
Handler RouteFunc
}
type CompiledRoute struct {
Path string
Handler RouteFunc
Regex *regexp.Regexp
Params []string
}
func CompileRoutes(routes []Route) []CompiledRoute {
compiledRoutes := []CompiledRoute{}
for _, route := range routes {
originalRoute := route.Path
var params []string
for open := strings.Index(route.Path, "{"); open > 0; open = strings.Index(route.Path, "{") {
close := strings.Index(route.Path, "}")
if close < 0 {
panic("malformed route")
}
param := route.Path[open+1 : close]
params = append(params, param)
route.Path = route.Path[:open] + `(.+)` + route.Path[close+1:]
}
regex := regexp.MustCompile(route.Path)
compiledRoutes = append(compiledRoutes, CompiledRoute{Path: originalRoute, Handler: route.Handler, Regex: regex, Params: params})
}
return compiledRoutes
}
func Handler(db *sql.DB, routes []CompiledRoute, w http.ResponseWriter, r *http.Request) {
requested := r.URL.Path[1:]
for _, compiledRoute := range routes {
match := compiledRoute.Regex.FindStringSubmatch(requested)
if len(match) == 0 {
continue
}
params := map[string]string{}
for i, v := range compiledRoute.Params {
params[v] = match[i+1]
}
compiledRoute.Handler(db, params, w, r)
return
}
fmt.Fprintf(w, "You didn't ask for anything")
}