github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/router.go (about)

     1  package rest
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  	"github.com/rs/zerolog"
     8  
     9  	"github.com/koko1123/flow-go-1/access"
    10  	"github.com/koko1123/flow-go-1/engine/access/rest/middleware"
    11  	"github.com/koko1123/flow-go-1/engine/access/rest/models"
    12  	"github.com/koko1123/flow-go-1/model/flow"
    13  )
    14  
    15  func newRouter(backend access.API, logger zerolog.Logger, chain flow.Chain) (*mux.Router, error) {
    16  	router := mux.NewRouter().StrictSlash(true)
    17  	v1SubRouter := router.PathPrefix("/v1").Subrouter()
    18  
    19  	// common middleware for all request
    20  	v1SubRouter.Use(middleware.LoggingMiddleware(logger))
    21  	v1SubRouter.Use(middleware.QueryExpandable())
    22  	v1SubRouter.Use(middleware.QuerySelect())
    23  
    24  	linkGenerator := models.NewLinkGeneratorImpl(v1SubRouter)
    25  
    26  	for _, r := range Routes {
    27  		h := NewHandler(logger, backend, r.Handler, linkGenerator, chain)
    28  		v1SubRouter.
    29  			Methods(r.Method).
    30  			Path(r.Pattern).
    31  			Name(r.Name).
    32  			Handler(h)
    33  	}
    34  	return router, nil
    35  }
    36  
    37  type route struct {
    38  	Name    string
    39  	Method  string
    40  	Pattern string
    41  	Handler ApiHandlerFunc
    42  }
    43  
    44  var Routes = []route{{
    45  	Method:  http.MethodGet,
    46  	Pattern: "/transactions/{id}",
    47  	Name:    "getTransactionByID",
    48  	Handler: GetTransactionByID,
    49  }, {
    50  	Method:  http.MethodPost,
    51  	Pattern: "/transactions",
    52  	Name:    "createTransaction",
    53  	Handler: CreateTransaction,
    54  }, {
    55  	Method:  http.MethodGet,
    56  	Pattern: "/transaction_results/{id}",
    57  	Name:    "getTransactionResultByID",
    58  	Handler: GetTransactionResultByID,
    59  }, {
    60  	Method:  http.MethodGet,
    61  	Pattern: "/blocks/{id}",
    62  	Name:    "getBlocksByIDs",
    63  	Handler: GetBlocksByIDs,
    64  }, {
    65  	Method:  http.MethodGet,
    66  	Pattern: "/blocks",
    67  	Name:    "getBlocksByHeight",
    68  	Handler: GetBlocksByHeight,
    69  }, {
    70  	Method:  http.MethodGet,
    71  	Pattern: "/blocks/{id}/payload",
    72  	Name:    "getBlockPayloadByID",
    73  	Handler: GetBlockPayloadByID,
    74  }, {
    75  	Method:  http.MethodGet,
    76  	Pattern: "/execution_results/{id}",
    77  	Name:    "getExecutionResultByID",
    78  	Handler: GetExecutionResultByID,
    79  }, {
    80  	Method:  http.MethodGet,
    81  	Pattern: "/execution_results",
    82  	Name:    "getExecutionResultByBlockID",
    83  	Handler: GetExecutionResultsByBlockIDs,
    84  }, {
    85  	Method:  http.MethodGet,
    86  	Pattern: "/collections/{id}",
    87  	Name:    "getCollectionByID",
    88  	Handler: GetCollectionByID,
    89  }, {
    90  	Method:  http.MethodPost,
    91  	Pattern: "/scripts",
    92  	Name:    "executeScript",
    93  	Handler: ExecuteScript,
    94  }, {
    95  	Method:  http.MethodGet,
    96  	Pattern: "/accounts/{address}",
    97  	Name:    "getAccount",
    98  	Handler: GetAccount,
    99  }, {
   100  	Method:  http.MethodGet,
   101  	Pattern: "/events",
   102  	Name:    "getEvents",
   103  	Handler: GetEvents,
   104  }, {
   105  	Method:  http.MethodGet,
   106  	Pattern: "/network/parameters",
   107  	Name:    "getNetworkParameters",
   108  	Handler: GetNetworkParameters,
   109  }}