forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
175 lines (156 loc) · 4.29 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
package rest
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/model/flow"
"github.com/onflow/flow-go/module"
)
func newRouter(backend access.API, logger zerolog.Logger, chain flow.Chain, restCollector module.RestMetrics) (*mux.Router, error) {
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, URLToRoute))
linkGenerator := models.NewLinkGeneratorImpl(v1SubRouter)
for _, r := range Routes {
h := NewHandler(logger, backend, r.Handler, linkGenerator, chain)
v1SubRouter.
Methods(r.Method).
Path(r.Pattern).
Name(r.Name).
Handler(h)
}
return router, nil
}
type route struct {
Name string
Method string
Pattern string
Handler ApiHandlerFunc
}
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: "/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 routeUrlMap = map[string]string{}
var routeRE = regexp.MustCompile(`(?i)/v1/(\w+)(/(\w+)(/(\w+))?)?`)
func init() {
for _, r := range Routes {
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}")
case 16:
// address based resource. e.g. /v1/accounts/1234567890abcdef
parts = append(parts, "{address}")
default:
// named resource. e.g. /v1/network/parameters
parts = append(parts, matches[0][3])
}
if matches[0][5] != "" {
parts = append(parts, matches[0][5])
}
return "/" + strings.Join(parts, "/"), nil
}