forked from elseym/mielke
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
35 lines (30 loc) · 692 Bytes
/
router.go
File metadata and controls
35 lines (30 loc) · 692 Bytes
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
package main
import (
"net/http"
"strings"
)
type route struct {
path string
method string
headers map[string]string
handler http.HandlerFunc
}
type router struct {
routes []route
}
func (t *router) add(path, method string, headers map[string]string, handler http.HandlerFunc) {
t.routes = append(t.routes, route{path, method, headers, handler})
}
func (t router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, o := range t.routes {
matches := o.path == r.URL.Path && o.method == r.Method
for n, v := range o.headers {
matches = matches && strings.Contains(r.Header.Get(n), v)
}
if matches {
o.handler(w, r)
return
}
}
http.NotFound(w, r)
}