-
Notifications
You must be signed in to change notification settings - Fork 177
/
router.go
242 lines (217 loc) · 5.89 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package routes
import (
"fmt"
"net/http"
"regexp"
"strings"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/middleware"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/state_stream"
"github.com/onflow/flow-go/engine/access/state_stream/backend"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
// RouterBuilder is a utility for building HTTP routers with common middleware and routes.
type RouterBuilder struct {
logger zerolog.Logger
router *mux.Router
v1SubRouter *mux.Router
}
// NewRouterBuilder creates a new RouterBuilder instance with common middleware and a v1 sub-router.
func NewRouterBuilder(
logger zerolog.Logger,
restCollector module.RestMetrics) *RouterBuilder {
router := mux.NewRouter().StrictSlash(true)
v1SubRouter := router.PathPrefix("/v1").Subrouter()
// common middleware for all request
v1SubRouter.Use(middleware.LoggingMiddleware(logger))
v1SubRouter.Use(middleware.QueryExpandable())
v1SubRouter.Use(middleware.QuerySelect())
v1SubRouter.Use(middleware.MetricsMiddleware(restCollector))
return &RouterBuilder{
logger: logger,
router: router,
v1SubRouter: v1SubRouter,
}
}
// AddRestRoutes adds rest routes to the router.
func (b *RouterBuilder) AddRestRoutes(backend access.API, chain flow.Chain) *RouterBuilder {
linkGenerator := models.NewLinkGeneratorImpl(b.v1SubRouter)
for _, r := range Routes {
h := NewHandler(b.logger, backend, r.Handler, linkGenerator, chain)
b.v1SubRouter.
Methods(r.Method).
Path(r.Pattern).
Name(r.Name).
Handler(h)
}
return b
}
// AddWsRoutes adds WebSocket routes to the router.
func (b *RouterBuilder) AddWsRoutes(
stateStreamApi state_stream.API,
chain flow.Chain,
stateStreamConfig backend.Config,
) *RouterBuilder {
for _, r := range WSRoutes {
h := NewWSHandler(b.logger, stateStreamApi, r.Handler, chain, stateStreamConfig)
b.v1SubRouter.
Methods(r.Method).
Path(r.Pattern).
Name(r.Name).
Handler(h)
}
return b
}
func (b *RouterBuilder) Build() *mux.Router {
return b.router
}
type route struct {
Name string
Method string
Pattern string
Handler ApiHandlerFunc
}
type wsroute struct {
Name string
Method string
Pattern string
Handler SubscribeHandlerFunc
}
var Routes = []route{{
Method: http.MethodGet,
Pattern: "/transactions/{id}",
Name: "getTransactionByID",
Handler: GetTransactionByID,
}, {
Method: http.MethodPost,
Pattern: "/transactions",
Name: "createTransaction",
Handler: CreateTransaction,
}, {
Method: http.MethodGet,
Pattern: "/transaction_results/{id}",
Name: "getTransactionResultByID",
Handler: GetTransactionResultByID,
}, {
Method: http.MethodGet,
Pattern: "/blocks/{id}",
Name: "getBlocksByIDs",
Handler: GetBlocksByIDs,
}, {
Method: http.MethodGet,
Pattern: "/blocks",
Name: "getBlocksByHeight",
Handler: GetBlocksByHeight,
}, {
Method: http.MethodGet,
Pattern: "/blocks/{id}/payload",
Name: "getBlockPayloadByID",
Handler: GetBlockPayloadByID,
}, {
Method: http.MethodGet,
Pattern: "/execution_results/{id}",
Name: "getExecutionResultByID",
Handler: GetExecutionResultByID,
}, {
Method: http.MethodGet,
Pattern: "/execution_results",
Name: "getExecutionResultByBlockID",
Handler: GetExecutionResultsByBlockIDs,
}, {
Method: http.MethodGet,
Pattern: "/collections/{id}",
Name: "getCollectionByID",
Handler: GetCollectionByID,
}, {
Method: http.MethodPost,
Pattern: "/scripts",
Name: "executeScript",
Handler: ExecuteScript,
}, {
Method: http.MethodGet,
Pattern: "/accounts/{address}",
Name: "getAccount",
Handler: GetAccount,
}, {
Method: http.MethodGet,
Pattern: "/accounts/{address}/keys/{index}",
Name: "getAccountKeyByIndex",
Handler: GetAccountKeyByIndex,
}, {
Method: http.MethodGet,
Pattern: "/events",
Name: "getEvents",
Handler: GetEvents,
}, {
Method: http.MethodGet,
Pattern: "/network/parameters",
Name: "getNetworkParameters",
Handler: GetNetworkParameters,
}, {
Method: http.MethodGet,
Pattern: "/node_version_info",
Name: "getNodeVersionInfo",
Handler: GetNodeVersionInfo,
}}
var WSRoutes = []wsroute{{
Method: http.MethodGet,
Pattern: "/subscribe_events",
Name: "subscribeEvents",
Handler: SubscribeEvents,
}}
var routeUrlMap = map[string]string{}
var routeRE = regexp.MustCompile(`(?i)/v1/(\w+)(/(\w+)(/(\w+))?)?`)
func init() {
for _, r := range Routes {
routeUrlMap[r.Pattern] = r.Name
}
for _, r := range WSRoutes {
routeUrlMap[r.Pattern] = r.Name
}
}
func URLToRoute(url string) (string, error) {
normalized, err := normalizeURL(url)
if err != nil {
return "", err
}
name, ok := routeUrlMap[normalized]
if !ok {
return "", fmt.Errorf("invalid url")
}
return name, nil
}
func normalizeURL(url string) (string, error) {
matches := routeRE.FindAllStringSubmatch(url, -1)
if len(matches) != 1 || len(matches[0]) != 6 {
return "", fmt.Errorf("invalid url")
}
// given a URL like
// /v1/blocks/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef/payload
// groups [ 1 ] [ 3 ] [ 5 ]
// normalized form like /v1/blocks/{id}/payload
parts := []string{matches[0][1]}
switch len(matches[0][3]) {
case 0:
// top level resource. e.g. /v1/blocks
case 64:
// id based resource. e.g. /v1/blocks/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
parts = append(parts, "{id}")
if matches[0][5] != "" {
parts = append(parts, matches[0][5])
}
case 16:
// address based resource. e.g. /v1/accounts/1234567890abcdef
parts = append(parts, "{address}")
if matches[0][5] == "keys" {
parts = append(parts, "keys", "{index}")
}
default:
// named resource. e.g. /v1/network/parameters
parts = append(parts, matches[0][3])
}
return "/" + strings.Join(parts, "/"), nil
}