-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
123 lines (98 loc) · 2.71 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
)
type RouteHandler func(w http.ResponseWriter, r *http.Request)
type Route struct {
Method string
Uri string
Handler RouteHandler
}
type Router struct {
routes []Route
}
func (r *Router) ServeHTTP (writer http.ResponseWriter, request *http.Request) {
routeUriMatch := false
for _, route := range r.routes {
matches := routeMatches(request.RequestURI, route.Uri)
if !matches {
fmt.Printf("no match\n")
continue
}
fmt.Printf("Found a match %s\n", route.Uri)
routeUriMatch = true
if route.Method != request.Method {
continue
}
route.Handler(writer, request)
return
}
if routeUriMatch {
writer.WriteHeader(405)
writer.Write([]byte("<p>Method not allowed</p>"))
return
}
path := "./public" + request.RequestURI
fileBytes, err := os.ReadFile(path)
if err == nil {
ext := filepath.Ext(path)
contentType := mime.TypeByExtension(ext)
writer.Header().Set("Content-Type", contentType)
writer.WriteHeader(200)
writer.Write(fileBytes)
return
}
// check if valid file
writer.Header().Set("Content-Type", "text/html")
writer.WriteHeader(404)
writer.Write([]byte("<p>Not Found</p>"))
}
func (r *Router) Get(uri string, handler RouteHandler) {
r.routes = append(r.routes, Route{"GET", uri, handler})
}
func (r *Router) Post(uri string, handler RouteHandler) {
r.routes = append(r.routes, Route{"POST", uri, handler})
}
func (r *Router) Delete(uri string, handler RouteHandler) {
r.routes = append(r.routes, Route{"DELETE", uri, handler})
}
func routeMatches(requestUri, route string) bool {
fmt.Printf("Check request uri: %s matches %s\n", requestUri, route)
requestUriParts := strings.Split(requestUri, "/")
routeUriParts := strings.Split(route, "/")
fmt.Printf("requestUriParts: %#v\n\n", requestUriParts)
fmt.Printf("routeUriParts: %#v\n\n", routeUriParts)
if len(requestUriParts) != len(routeUriParts) {
fmt.Printf("lenght mismatch\n")
return false
}
length := len(requestUriParts)
for i := range length {
fmt.Printf("%d\n", i)
if strings.HasPrefix(routeUriParts[i], ":") {
fmt.Printf("has prefix\n")
// wildcard
continue
}
if requestUriParts[i] != routeUriParts[i] {
fmt.Printf("mismatch route part %s && %s\n", routeUriParts[i], requestUriParts[i])
return false
}
}
return true
}
func startServer(host string, port int, router *Router) {
serverHost := fmt.Sprintf("%s:%d", host, port)
fmt.Printf("%s\n\n", serverHost)
err := http.ListenAndServe(serverHost, router)
check(err)
}
func home(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("OK"))
}