Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Refactor REST Routes Package #6616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ endif

.PHONY: generate-openapi
generate-openapi:
swagger-codegen generate -l go -i https://raw.githubusercontent.com/onflow/flow/master/openapi/access.yaml -D packageName=models,modelDocs=false,models -o engine/access/rest/models;
go fmt ./engine/access/rest/models
swagger-codegen generate -l go -i https://raw.githubusercontent.com/onflow/flow/master/openapi/access.yaml -D packageName=models,modelDocs=false,models -o engine/access/rest/http/models;
go fmt ./engine/access/rest/http/models

.PHONY: generate
generate: generate-proto generate-mocks generate-fvm-env-wrappers
Expand Down
7 changes: 4 additions & 3 deletions cmd/access/node_builder/access_node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import (
"github.com/onflow/flow-go/engine/access/ingestion/tx_error_messages"
pingeng "github.com/onflow/flow-go/engine/access/ping"
"github.com/onflow/flow-go/engine/access/rest"
"github.com/onflow/flow-go/engine/access/rest/routes"
commonrest "github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/engine/access/rest/router"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
rpcConnection "github.com/onflow/flow-go/engine/access/rpc/connection"
Expand Down Expand Up @@ -224,7 +225,7 @@ func DefaultAccessNodeConfig() *AccessNodeConfig {
WriteTimeout: rest.DefaultWriteTimeout,
ReadTimeout: rest.DefaultReadTimeout,
IdleTimeout: rest.DefaultIdleTimeout,
MaxRequestSize: routes.DefaultMaxRequestSize,
MaxRequestSize: commonrest.DefaultMaxRequestSize,
},
MaxMsgSize: grpcutils.DefaultMaxMsgSize,
CompressorName: grpcutils.NoCompressor,
Expand Down Expand Up @@ -1724,7 +1725,7 @@ func (builder *FlowAccessNodeBuilder) Build() (cmd.Node, error) {
return nil
}).
Module("rest metrics", func(node *cmd.NodeConfig) error {
m, err := metrics.NewRestCollector(routes.URLToRoute, node.MetricsRegisterer)
m, err := metrics.NewRestCollector(router.URLToRoute, node.MetricsRegisterer)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/observer/node_builder/observer_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import (
"github.com/onflow/flow-go/engine/access/index"
"github.com/onflow/flow-go/engine/access/rest"
restapiproxy "github.com/onflow/flow-go/engine/access/rest/apiproxy"
"github.com/onflow/flow-go/engine/access/rest/routes"
commonrest "github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/engine/access/rest/router"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
rpcConnection "github.com/onflow/flow-go/engine/access/rpc/connection"
Expand Down Expand Up @@ -195,7 +196,7 @@ func DefaultObserverServiceConfig() *ObserverServiceConfig {
WriteTimeout: rest.DefaultWriteTimeout,
ReadTimeout: rest.DefaultReadTimeout,
IdleTimeout: rest.DefaultIdleTimeout,
MaxRequestSize: routes.DefaultMaxRequestSize,
MaxRequestSize: commonrest.DefaultMaxRequestSize,
},
MaxMsgSize: grpcutils.DefaultMaxMsgSize,
CompressorName: grpcutils.NoCompressor,
Expand Down Expand Up @@ -1691,7 +1692,7 @@ func (builder *ObserverServiceBuilder) enqueueRPCServer() {
return nil
})
builder.Module("rest metrics", func(node *cmd.NodeConfig) error {
m, err := metrics.NewRestCollector(routes.URLToRoute, node.MetricsRegisterer)
m, err := metrics.NewRestCollector(router.URLToRoute, node.MetricsRegisterer)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions engine/access/handle_irrecoverable_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

accessmock "github.com/onflow/flow-go/engine/access/mock"
"github.com/onflow/flow-go/engine/access/rest"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/rest/router"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
statestreambackend "github.com/onflow/flow-go/engine/access/state_stream/backend"
Expand Down Expand Up @@ -249,7 +249,7 @@ func (suite *IrrecoverableStateTestSuite) TestRestInconsistentNodeState() {
// optionsForBlocksIdGetOpts returns options for the BlocksApi.BlocksIdGet function.
func optionsForBlocksIdGetOpts() *restclient.BlocksApiBlocksIdGetOpts {
return &restclient.BlocksApiBlocksIdGetOpts{
Expand: optional.NewInterface([]string{routes.ExpandableFieldPayload}),
Expand: optional.NewInterface([]string{router.ExpandableFieldPayload}),
Select_: optional.NewInterface([]string{"header.id"}),
}
}
15 changes: 9 additions & 6 deletions engine/access/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ the [Flow OpenAPI definition](https://github.com/onflow/flow/blob/master/openapi
available on our [docs site](https://docs.onflow.org/http-api/).

## Packages

- `rest`: The HTTP handlers for the server generator and the select filter, implementation of handling local requests.
- `middleware`: The common [middlewares](https://github.com/gorilla/mux#middleware) that all request pass through.
- `models`: The generated models using openapi generators and implementation of model builders.
- `request`: Implementation of API requests that provide validation for input data and build request models.
- `routes`: The common HTTP handlers for all the requests, tests for each request.
- `common`: Includes shared components for REST requests.
- `middleware`: The common [middlewares](https://github.com/gorilla/mux#middleware) that all request pass through.
- `models`: The common generated models using openapi generators.
- `http`: Implements core HTTP handling functionality for access node.
- `models`: The generated models using openapi generators and implementation of model builders.
- `request`: Implementation of API requests that provide validation for input data and build request models.
- `routes`: The HTTP handlers for all http requests, tests for each request.
- `router`: Implementation of building HTTP routers with common middleware and routes.
- `apiproxy`: Implementation of proxy backend handler which includes the local backend and forwards the methods which
can't be handled locally to an upstream using gRPC API. This is used by observers that don't have all data in their
local db.
Expand All @@ -19,7 +22,7 @@ local db.

1. Every incoming request passes through a common set of middlewares - logging middleware, query expandable and query
select middleware defined in the middleware package.
2. Each request is then wrapped by our handler (`rest/handler.go`) and request input data is used to build the request
2. Each request is then wrapped by our handler (`rest/http/handler.go`) and request input data is used to build the request
models defined in request package.
3. The request is then sent to the corresponding API handler based on the configuration in the router.
4. Each handler implements actions to perform the request (database lookups etc) and after the response is built using
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package common

import "net/http"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package common

import (
"encoding/json"
Expand All @@ -11,7 +11,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/common/models"
fvmErrors "github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
Expand Down Expand Up @@ -50,15 +50,15 @@ func (h *HttpHandler) VerifyRequest(w http.ResponseWriter, r *http.Request) erro
r.Body = http.MaxBytesReader(w, r.Body, h.MaxRequestSize)
err := r.ParseForm()
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return err
}
return nil
}

func (h *HttpHandler) errorHandler(w http.ResponseWriter, err error, errorLogger zerolog.Logger) {
func (h *HttpHandler) ErrorHandler(w http.ResponseWriter, err error, errorLogger zerolog.Logger) {
// rest status type error should be returned with status and user message provided
var statusErr models.StatusError
var statusErr StatusError
if errors.As(err, &statusErr) {
h.errorResponse(w, statusErr.Status(), statusErr.UserMessage(), errorLogger)
return
Expand Down Expand Up @@ -102,8 +102,8 @@ func (h *HttpHandler) errorHandler(w http.ResponseWriter, err error, errorLogger
h.errorResponse(w, http.StatusInternalServerError, msg, errorLogger)
}

// jsonResponse builds a JSON response and send it to the client
func (h *HttpHandler) jsonResponse(w http.ResponseWriter, code int, response interface{}, errLogger zerolog.Logger) {
// JsonResponse builds a JSON response and send it to the client
func (h *HttpHandler) JsonResponse(w http.ResponseWriter, code int, response interface{}, errLogger zerolog.Logger) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")

// serialize response to JSON and handler errors
Expand Down Expand Up @@ -135,5 +135,5 @@ func (h *HttpHandler) errorResponse(
Code: int32(returnCode),
Message: responseMessage,
}
h.jsonResponse(w, returnCode, modelError, logger)
h.JsonResponse(w, returnCode, modelError, logger)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package request
package common

import (
"net/http"
"strings"

"github.com/gorilla/mux"

"github.com/onflow/flow-go/engine/access/rest/middleware"
"github.com/onflow/flow-go/engine/access/rest/common/middleware"
"github.com/onflow/flow-go/model/flow"
)

Expand All @@ -18,102 +18,6 @@ type Request struct {
Chain flow.Chain
}

func (rd *Request) GetScriptRequest() (GetScript, error) {
var req GetScript
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockRequest() (GetBlock, error) {
var req GetBlock
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockByIDsRequest() (GetBlockByIDs, error) {
var req GetBlockByIDs
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockPayloadRequest() (GetBlockPayload, error) {
var req GetBlockPayload
err := req.Build(rd)
return req, err
}

func (rd *Request) GetCollectionRequest() (GetCollection, error) {
var req GetCollection
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountRequest() (GetAccount, error) {
var req GetAccount
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountBalanceRequest() (GetAccountBalance, error) {
var req GetAccountBalance
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountKeysRequest() (GetAccountKeys, error) {
var req GetAccountKeys
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountKeyRequest() (GetAccountKey, error) {
var req GetAccountKey
err := req.Build(rd)
return req, err
}

func (rd *Request) GetExecutionResultByBlockIDsRequest() (GetExecutionResultByBlockIDs, error) {
var req GetExecutionResultByBlockIDs
err := req.Build(rd)
return req, err
}

func (rd *Request) GetExecutionResultRequest() (GetExecutionResult, error) {
var req GetExecutionResult
err := req.Build(rd)
return req, err
}

func (rd *Request) GetTransactionRequest() (GetTransaction, error) {
var req GetTransaction
err := req.Build(rd)
return req, err
}

func (rd *Request) GetTransactionResultRequest() (GetTransactionResult, error) {
var req GetTransactionResult
err := req.Build(rd)
return req, err
}

func (rd *Request) GetEventsRequest() (GetEvents, error) {
var req GetEvents
err := req.Build(rd)
return req, err
}

func (rd *Request) CreateTransactionRequest() (CreateTransaction, error) {
var req CreateTransaction
err := req.Build(rd)
return req, err
}

func (rd *Request) SubscribeEventsRequest() (SubscribeEvents, error) {
var req SubscribeEvents
err := req.Build(rd)
return req, err
}

func (rd *Request) Expands(field string) bool {
return rd.ExpandFields[field]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package routes
package http

import (
"net/http"

"github.com/rs/zerolog"

"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/engine/access/rest/http/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)

// ApiHandlerFunc is a function that contains endpoint handling logic,
// it fetches necessary resources and returns an error or response model.
type ApiHandlerFunc func(
r *request.Request,
r *common.Request,
backend access.API,
generator models.LinkGenerator,
) (interface{}, error)
Expand All @@ -24,7 +24,7 @@ type ApiHandlerFunc func(
// Handler function allows easier handling of errors and responses as it
// wraps functionality for handling error and responses outside of endpoint handling.
type Handler struct {
*HttpHandler
*common.HttpHandler
backend access.API
linkGenerator models.LinkGenerator
apiHandlerFunc ApiHandlerFunc
Expand All @@ -42,7 +42,7 @@ func NewHandler(
backend: backend,
apiHandlerFunc: handlerFunc,
linkGenerator: generator,
HttpHandler: NewHttpHandler(logger, chain, maxRequestSize),
HttpHandler: common.NewHttpHandler(logger, chain, maxRequestSize),
}

return handler
Expand All @@ -58,22 +58,22 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
decoratedRequest := request.Decorate(r, h.Chain)
decoratedRequest := common.Decorate(r, h.Chain)

// execute handler function and check for error
response, err := h.apiHandlerFunc(decoratedRequest, h.backend, h.linkGenerator)
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return
}

// apply the select filter if any select fields have been specified
response, err = util.SelectFilter(response, decoratedRequest.Selects())
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return
}

// write response to response stream
h.jsonResponse(w, http.StatusOK, response, errLog)
h.JsonResponse(w, http.StatusOK, response, errLog)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package request
import (
"io"

"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/model/flow"
)

type CreateTransaction struct {
Transaction flow.TransactionBody
}

func (c *CreateTransaction) Build(r *Request) error {
func CreateTransactionRequest(r *common.Request) (CreateTransaction, error) {
var req CreateTransaction
err := req.Build(r)
return req, err
}

func (c *CreateTransaction) Build(r *common.Request) error {
return c.Parse(r.Body, r.Chain)
}

Expand Down
Loading
Loading