-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
115 lines (99 loc) · 1.81 KB
/
route.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
package stgin
import "strings"
type Route struct {
Path string
Method string
Action API
}
func GET(path string, api API) Route {
return Route{
Path: path,
Method: "GET",
Action: api,
}
}
func PUT(path string, api API) Route {
return Route{
Path: path,
Method: "PUT",
Action: api,
}
}
func POST(path string, api API) Route {
return Route{
Path: path,
Method: "POST",
Action: api,
}
}
func DELETE(path string, api API) Route {
return Route{
Path: path,
Method: "DELETE",
Action: api,
}
}
func PATCH(path string, api API) Route {
return Route{
Path: path,
Method: "PATCH",
Action: api,
}
}
type RouteCreationStage struct {
method string
path string
}
func (stage RouteCreationStage) Do(api API) Route {
switch strings.ToUpper(stage.method) {
case "GET":
return GET(stage.path, api)
case "PUT":
return PUT(stage.path, api)
case "POST":
return POST(stage.path, api)
case "DELETE":
return DELETE(stage.path, api)
case "PATCH":
return PATCH(stage.path, api)
default:
return GET(stage.path, api)
}
}
func OnGET(path string) RouteCreationStage {
return RouteCreationStage{
method: "GET",
path: path,
}
}
func OnPUT(path string) RouteCreationStage {
return RouteCreationStage{
method: "PUT",
path: path,
}
}
func OnPOST(path string) RouteCreationStage {
return RouteCreationStage{
method: "POST",
path: path,
}
}
func OnDelete(path string) RouteCreationStage {
return RouteCreationStage{
method: "DELETE",
path: path,
}
}
func OnPatch(path string) RouteCreationStage {
return RouteCreationStage{
method: "PATCH",
path: path,
}
}
func OnPath(path string) RouteCreationStage {
return RouteCreationStage{path: path}
}
func (stage RouteCreationStage) WithMethod(method string) RouteCreationStage {
stage.method = method
return stage
}