-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
58 lines (49 loc) · 1.15 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
package main
import (
"context"
"log"
"net/http"
"regexp"
)
type ParamsType string
const (
ParamsKey ParamsType = "params"
)
func r(s string) *regexp.Regexp {
reg, _ := regexp.Compile(s)
return reg
}
type Route struct {
pattern *regexp.Regexp
handler http.HandlerFunc
}
type Router struct {
routes []Route
}
func NewRouter(routes []Route) *Router {
return &Router{routes: routes}
}
func newContextWithParams(ctx context.Context, params map[string]string) context.Context {
return context.WithValue(ctx, ParamsKey, params)
}
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path)
for _, route := range router.routes {
re := route.pattern
match := re.FindStringSubmatch(r.URL.Path)
if len(match) > 0 {
// Extract parameter values from the URL
params := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
params[name] = match[i]
}
}
// Call the handler with the extracted parameter values
route.handler(w, r.WithContext(newContextWithParams(r.Context(), params)))
return
}
}
// No matching route found
http.NotFound(w, r)
}