-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
29 lines (24 loc) · 971 Bytes
/
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
package main
// Represents a function to 'handle' the incoming request
// provided it is multiplexed routed to
type HandlerCallback func(req *Request, res *Response)
// Holds the method and path for a given request
type RequestPath struct {
Method string
Path string
}
// Represents a multiplex router responsible for handling
// incoming requests based on their path (just in a map for now)
type Router struct {
Handlers map[RequestPath]HandlerCallback
}
// Adds a handler to the router, if a path already exists for the provided handler
// it overwrites the existing callback
// TODO: validate path in regex before adding handler, and return err if no valid
func (r *Router) AddHandler(method string, path string, callback HandlerCallback) {
r.Handlers[RequestPath{method, path}] = callback
}
// Find a handler for a given requestpath
func (r *Router) FindHandler(method string, path string) HandlerCallback {
return r.Handlers[RequestPath{method, path}]
}