forked from orsinium-labs/josh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
100 lines (88 loc) · 3.05 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
package josh
import "net/http"
// Router is a mapping of URL paths to endpoints.
type Router map[string]Endpoint
// Register all endpoints from the router in the given stdlib multiplexer.
//
// Pass nil argument if you want to use the default global multiplexer.
func (r Router) Register(mux *http.ServeMux) {
if mux == nil {
mux = http.DefaultServeMux
}
for path, endpoint := range r {
if endpoint.GET != nil {
mux.HandleFunc("GET "+path, endpoint.GET)
}
if endpoint.HEAD != nil {
mux.HandleFunc("HEAD "+path, endpoint.HEAD)
}
if endpoint.POST != nil {
mux.HandleFunc("POST "+path, endpoint.POST)
}
if endpoint.PUT != nil {
mux.HandleFunc("PUT "+path, endpoint.PUT)
}
if endpoint.DELETE != nil {
mux.HandleFunc("DELETE "+path, endpoint.DELETE)
}
if endpoint.CONNECT != nil {
mux.HandleFunc("CONNECT "+path, endpoint.CONNECT)
}
if endpoint.OPTIONS != nil {
mux.HandleFunc("OPTIONS "+path, endpoint.OPTIONS)
}
if endpoint.TRACE != nil {
mux.HandleFunc("TRACE "+path, endpoint.TRACE)
}
if endpoint.PATCH != nil {
mux.HandleFunc("PATCH "+path, endpoint.PATCH)
}
}
}
// Endpoint is a collection of handlers for the same path but different HTTP methods.
type Endpoint struct {
// GET method requests a representation of the specified resource.
// Requests using GET should only retrieve data.
//
// https://jsonapi.org/format/#fetching-resources
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
GET http.HandlerFunc
// HEAD method asks for a response identical to a GET request,
// but without the response body.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
HEAD http.HandlerFunc
// POST method submits an entity to the specified resource,
// often causing a change in state or side effects on the server.
//
// https://jsonapi.org/format/#crud-creating
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
POST http.HandlerFunc
// PUT method replaces all current representations of the target resource
// with the request payload.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
PUT http.HandlerFunc
// DELETE method deletes the specified resource.
//
// https://jsonapi.org/format/#crud-deleting
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
DELETE http.HandlerFunc
// CONNECT method establishes a tunnel to the server identified by the target resource.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
CONNECT http.HandlerFunc
// OPTIONS method describes the communication options for the target resource.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
OPTIONS http.HandlerFunc
// TRACE method performs a message loop-back test along the path to the target resource.
//
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE
TRACE http.HandlerFunc
// PATCH method applies partial modifications to a resource.
//
// https://jsonapi.org/format/#crud-updating
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
PATCH http.HandlerFunc
}