-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
167 lines (151 loc) · 3.72 KB
/
endpoints.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
)
/*
Method HTTP
typeNames
GET
The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
HEAD
Same as GET, but transfers the status line and header section only.
POST
A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
PUT
Replaces all current representations of the target resource with the uploaded content.
DELETE
Removes all current representations of the target resource given by a URI.
CONNECT
Establishes a tunnel to the server identified by a given URI.
OPTIONS
Describes the communication options for the target resource.
TRACE
Performs a message loop-back test along the path to the target resource.
*/
// Endpoint Standard
type Endpoint struct {
path string
subRoutes []Endpoint
methods map[string]http.Handler
secure bool
}
// AllRoutes for api
var AllRoutes = [...]Endpoint{
{
path: "/ping",
methods: map[string]http.Handler{
http.MethodGet: Pong,
http.MethodPost: Pong,
},
}, {
path: "/initialize",
methods: map[string]http.Handler{
http.MethodPost: NewRecipe,
},
}, {
path: "/currentStep",
methods: map[string]http.Handler{
http.MethodGet: GetCurrentStep,
},
}, {
path: "/increment-step",
methods: map[string]http.Handler{
http.MethodPost: GotToNStep,
},
}, {
path: "/recipes",
methods: map[string]http.Handler{
http.MethodGet: GetRecipes,
},
subRoutes: []Endpoint{
{
path: "/{id}",
methods: map[string]http.Handler{
http.MethodGet: GetRecipeByID,
},
subRoutes: []Endpoint{
{
path: "/ingredients",
methods: map[string]http.Handler{
http.MethodGet: GetRecipeIngredients,
},
subRoutes: []Endpoint{
{
path: "/step/{step_number}",
methods: map[string]http.Handler{
http.MethodGet: GetRecipeStepIngredients,
},
},
},
},
{
path: "/utensils",
methods: map[string]http.Handler{
http.MethodGet: GetRecipeUtensils,
},
subRoutes: []Endpoint{
{
path: "/step/{step_number}",
methods: map[string]http.Handler{
http.MethodGet: GetUtensilStepIngredients,
},
},
},
},
},
},
},
}, {
path: "/ingredients",
methods: map[string]http.Handler{
http.MethodGet: GetIngredients,
},
}, {
path: "/utensils",
methods: map[string]http.Handler{
http.MethodGet: GetUtensils,
},
},
}
// CreateRoutes for the app
func CreateRoutes(r *mux.Router, routes []Endpoint, rootURL string) {
// Loop through endpoints
for _, e := range routes {
// Loop through methods
for k, m := range e.methods {
r.Handle(rootURL+e.path, m).Methods(k)
}
// Recurse if there are subRoutes
if len(e.subRoutes) > 0 {
CreateRoutes(r, e.subRoutes, rootURL+e.path)
}
}
}
// RouteWalker goes through and prints all routes
func RouteWalker(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
pathTemplate, err := route.GetPathTemplate()
if err == nil {
fmt.Println("ROUTE:", pathTemplate)
}
pathRegexp, err := route.GetPathRegexp()
if err == nil {
fmt.Println("Path regexp:", pathRegexp)
}
queriesTemplates, err := route.GetQueriesTemplates()
if err == nil {
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
}
queriesRegexps, err := route.GetQueriesRegexp()
if err == nil {
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
}
methods, err := route.GetMethods()
if err == nil {
fmt.Println("Methods:", strings.Join(methods, ","))
}
fmt.Println()
return nil
}