From 1eaae7885dce072aa58292711ae1b5b097d744d1 Mon Sep 17 00:00:00 2001 From: Bryan Potter Date: Wed, 6 Nov 2024 18:15:16 -0700 Subject: [PATCH] revert request context changes to controller --- examples/server/main.go | 2 -- server/api_account.go | 26 ++++------------ server/api_block.go | 26 ++++------------ server/api_call.go | 24 ++++----------- server/api_construction.go | 38 ++++++++---------------- server/api_events.go | 24 ++++----------- server/api_mempool.go | 26 ++++------------ server/api_network.go | 28 +++++------------ server/api_search.go | 24 ++++----------- server/routers.go | 2 -- templates/server/controller-api.mustache | 19 ++---------- templates/server/routers.mustache | 1 - 12 files changed, 55 insertions(+), 185 deletions(-) diff --git a/examples/server/main.go b/examples/server/main.go index ba2cdaa2..6c41b033 100644 --- a/examples/server/main.go +++ b/examples/server/main.go @@ -40,14 +40,12 @@ func NewBlockchainRouter( networkAPIController := server.NewNetworkAPIController( networkAPIService, asserter, - nil, ) blockAPIService := services.NewBlockAPIService(network) blockAPIController := server.NewBlockAPIController( blockAPIService, asserter, - nil, ) return server.NewRouter(networkAPIController, blockAPIController) diff --git a/server/api_account.go b/server/api_account.go index a98d4565..af1e881a 100644 --- a/server/api_account.go +++ b/server/api_account.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A AccountAPIController binds http requests to an api service and writes the service results to // the http response type AccountAPIController struct { - service AccountAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service AccountAPIServicer + asserter *asserter.Asserter } // NewAccountAPIController creates a default api controller func NewAccountAPIController( s AccountAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &AccountAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -65,16 +61,6 @@ func (c *AccountAPIController) Routes() Routes { } } -func (c *AccountAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // AccountBalance - Get an Account's Balance func (c *AccountAPIController) AccountBalance(w http.ResponseWriter, r *http.Request) { accountBalanceRequest := &types.AccountBalanceRequest{} @@ -95,7 +81,7 @@ func (c *AccountAPIController) AccountBalance(w http.ResponseWriter, r *http.Req return } - result, serviceErr := c.service.AccountBalance(c.ContextFromRequest(r), accountBalanceRequest) + result, serviceErr := c.service.AccountBalance(r.Context(), accountBalanceRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) @@ -125,7 +111,7 @@ func (c *AccountAPIController) AccountCoins(w http.ResponseWriter, r *http.Reque return } - result, serviceErr := c.service.AccountCoins(c.ContextFromRequest(r), accountCoinsRequest) + result, serviceErr := c.service.AccountCoins(r.Context(), accountCoinsRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) diff --git a/server/api_block.go b/server/api_block.go index a3d22732..61655a6f 100644 --- a/server/api_block.go +++ b/server/api_block.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A BlockAPIController binds http requests to an api service and writes the service results to the // http response type BlockAPIController struct { - service BlockAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service BlockAPIServicer + asserter *asserter.Asserter } // NewBlockAPIController creates a default api controller func NewBlockAPIController( s BlockAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &BlockAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -65,16 +61,6 @@ func (c *BlockAPIController) Routes() Routes { } } -func (c *BlockAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // Block - Get a Block func (c *BlockAPIController) Block(w http.ResponseWriter, r *http.Request) { blockRequest := &types.BlockRequest{} @@ -95,7 +81,7 @@ func (c *BlockAPIController) Block(w http.ResponseWriter, r *http.Request) { return } - result, serviceErr := c.service.Block(c.ContextFromRequest(r), blockRequest) + result, serviceErr := c.service.Block(r.Context(), blockRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) @@ -126,7 +112,7 @@ func (c *BlockAPIController) BlockTransaction(w http.ResponseWriter, r *http.Req } result, serviceErr := c.service.BlockTransaction( - c.ContextFromRequest(r), + r.Context(), blockTransactionRequest, ) if serviceErr != nil { diff --git a/server/api_call.go b/server/api_call.go index 7e965636..20abff66 100644 --- a/server/api_call.go +++ b/server/api_call.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A CallAPIController binds http requests to an api service and writes the service results to the // http response type CallAPIController struct { - service CallAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service CallAPIServicer + asserter *asserter.Asserter } // NewCallAPIController creates a default api controller func NewCallAPIController( s CallAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &CallAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -59,16 +55,6 @@ func (c *CallAPIController) Routes() Routes { } } -func (c *CallAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // Call - Make a Network-Specific Procedure Call func (c *CallAPIController) Call(w http.ResponseWriter, r *http.Request) { callRequest := &types.CallRequest{} @@ -89,7 +75,7 @@ func (c *CallAPIController) Call(w http.ResponseWriter, r *http.Request) { return } - result, serviceErr := c.service.Call(c.ContextFromRequest(r), callRequest) + result, serviceErr := c.service.Call(r.Context(), callRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) diff --git a/server/api_construction.go b/server/api_construction.go index f1d91c2a..de0e1187 100644 --- a/server/api_construction.go +++ b/server/api_construction.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A ConstructionAPIController binds http requests to an api service and writes the service results // to the http response type ConstructionAPIController struct { - service ConstructionAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service ConstructionAPIServicer + asserter *asserter.Asserter } // NewConstructionAPIController creates a default api controller func NewConstructionAPIController( s ConstructionAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &ConstructionAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -101,16 +97,6 @@ func (c *ConstructionAPIController) Routes() Routes { } } -func (c *ConstructionAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // ConstructionCombine - Create Network Transaction from Signatures func (c *ConstructionAPIController) ConstructionCombine(w http.ResponseWriter, r *http.Request) { constructionCombineRequest := &types.ConstructionCombineRequest{} @@ -132,7 +118,7 @@ func (c *ConstructionAPIController) ConstructionCombine(w http.ResponseWriter, r } result, serviceErr := c.service.ConstructionCombine( - c.ContextFromRequest(r), + r.Context(), constructionCombineRequest, ) if serviceErr != nil { @@ -165,7 +151,7 @@ func (c *ConstructionAPIController) ConstructionDerive(w http.ResponseWriter, r } result, serviceErr := c.service.ConstructionDerive( - c.ContextFromRequest(r), + r.Context(), constructionDeriveRequest, ) if serviceErr != nil { @@ -198,7 +184,7 @@ func (c *ConstructionAPIController) ConstructionHash(w http.ResponseWriter, r *h } result, serviceErr := c.service.ConstructionHash( - c.ContextFromRequest(r), + r.Context(), constructionHashRequest, ) if serviceErr != nil { @@ -231,7 +217,7 @@ func (c *ConstructionAPIController) ConstructionMetadata(w http.ResponseWriter, } result, serviceErr := c.service.ConstructionMetadata( - c.ContextFromRequest(r), + r.Context(), constructionMetadataRequest, ) if serviceErr != nil { @@ -264,7 +250,7 @@ func (c *ConstructionAPIController) ConstructionParse(w http.ResponseWriter, r * } result, serviceErr := c.service.ConstructionParse( - c.ContextFromRequest(r), + r.Context(), constructionParseRequest, ) if serviceErr != nil { @@ -297,7 +283,7 @@ func (c *ConstructionAPIController) ConstructionPayloads(w http.ResponseWriter, } result, serviceErr := c.service.ConstructionPayloads( - c.ContextFromRequest(r), + r.Context(), constructionPayloadsRequest, ) if serviceErr != nil { @@ -330,7 +316,7 @@ func (c *ConstructionAPIController) ConstructionPreprocess(w http.ResponseWriter } result, serviceErr := c.service.ConstructionPreprocess( - c.ContextFromRequest(r), + r.Context(), constructionPreprocessRequest, ) if serviceErr != nil { @@ -363,7 +349,7 @@ func (c *ConstructionAPIController) ConstructionSubmit(w http.ResponseWriter, r } result, serviceErr := c.service.ConstructionSubmit( - c.ContextFromRequest(r), + r.Context(), constructionSubmitRequest, ) if serviceErr != nil { diff --git a/server/api_events.go b/server/api_events.go index 68dbeb1b..73a1d5d5 100644 --- a/server/api_events.go +++ b/server/api_events.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A EventsAPIController binds http requests to an api service and writes the service results to the // http response type EventsAPIController struct { - service EventsAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service EventsAPIServicer + asserter *asserter.Asserter } // NewEventsAPIController creates a default api controller func NewEventsAPIController( s EventsAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &EventsAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -59,16 +55,6 @@ func (c *EventsAPIController) Routes() Routes { } } -func (c *EventsAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // EventsBlocks - [INDEXER] Get a range of BlockEvents func (c *EventsAPIController) EventsBlocks(w http.ResponseWriter, r *http.Request) { eventsBlocksRequest := &types.EventsBlocksRequest{} @@ -89,7 +75,7 @@ func (c *EventsAPIController) EventsBlocks(w http.ResponseWriter, r *http.Reques return } - result, serviceErr := c.service.EventsBlocks(c.ContextFromRequest(r), eventsBlocksRequest) + result, serviceErr := c.service.EventsBlocks(r.Context(), eventsBlocksRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) diff --git a/server/api_mempool.go b/server/api_mempool.go index 789f2b9f..2100d8cc 100644 --- a/server/api_mempool.go +++ b/server/api_mempool.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A MempoolAPIController binds http requests to an api service and writes the service results to // the http response type MempoolAPIController struct { - service MempoolAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service MempoolAPIServicer + asserter *asserter.Asserter } // NewMempoolAPIController creates a default api controller func NewMempoolAPIController( s MempoolAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &MempoolAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -65,16 +61,6 @@ func (c *MempoolAPIController) Routes() Routes { } } -func (c *MempoolAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // Mempool - Get All Mempool Transactions func (c *MempoolAPIController) Mempool(w http.ResponseWriter, r *http.Request) { networkRequest := &types.NetworkRequest{} @@ -95,7 +81,7 @@ func (c *MempoolAPIController) Mempool(w http.ResponseWriter, r *http.Request) { return } - result, serviceErr := c.service.Mempool(c.ContextFromRequest(r), networkRequest) + result, serviceErr := c.service.Mempool(r.Context(), networkRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) @@ -126,7 +112,7 @@ func (c *MempoolAPIController) MempoolTransaction(w http.ResponseWriter, r *http } result, serviceErr := c.service.MempoolTransaction( - c.ContextFromRequest(r), + r.Context(), mempoolTransactionRequest, ) if serviceErr != nil { diff --git a/server/api_network.go b/server/api_network.go index 323c8aa9..75a8f0c1 100644 --- a/server/api_network.go +++ b/server/api_network.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A NetworkAPIController binds http requests to an api service and writes the service results to // the http response type NetworkAPIController struct { - service NetworkAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service NetworkAPIServicer + asserter *asserter.Asserter } // NewNetworkAPIController creates a default api controller func NewNetworkAPIController( s NetworkAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &NetworkAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -71,16 +67,6 @@ func (c *NetworkAPIController) Routes() Routes { } } -func (c *NetworkAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // NetworkList - Get List of Available Networks func (c *NetworkAPIController) NetworkList(w http.ResponseWriter, r *http.Request) { metadataRequest := &types.MetadataRequest{} @@ -101,7 +87,7 @@ func (c *NetworkAPIController) NetworkList(w http.ResponseWriter, r *http.Reques return } - result, serviceErr := c.service.NetworkList(c.ContextFromRequest(r), metadataRequest) + result, serviceErr := c.service.NetworkList(r.Context(), metadataRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) @@ -131,7 +117,7 @@ func (c *NetworkAPIController) NetworkOptions(w http.ResponseWriter, r *http.Req return } - result, serviceErr := c.service.NetworkOptions(c.ContextFromRequest(r), networkRequest) + result, serviceErr := c.service.NetworkOptions(r.Context(), networkRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) @@ -161,7 +147,7 @@ func (c *NetworkAPIController) NetworkStatus(w http.ResponseWriter, r *http.Requ return } - result, serviceErr := c.service.NetworkStatus(c.ContextFromRequest(r), networkRequest) + result, serviceErr := c.service.NetworkStatus(r.Context(), networkRequest) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) diff --git a/server/api_search.go b/server/api_search.go index 70ea7e54..6fb4ddbc 100644 --- a/server/api_search.go +++ b/server/api_search.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" "strings" @@ -29,21 +28,18 @@ import ( // A SearchAPIController binds http requests to an api service and writes the service results to the // http response type SearchAPIController struct { - service SearchAPIServicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service SearchAPIServicer + asserter *asserter.Asserter } // NewSearchAPIController creates a default api controller func NewSearchAPIController( s SearchAPIServicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &SearchAPIController{ - service: s, - asserter: asserter, - contextFromRequest: contextFromRequest, + service: s, + asserter: asserter, } } @@ -59,16 +55,6 @@ func (c *SearchAPIController) Routes() Routes { } } -func (c *SearchAPIController) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -} - // SearchTransactions - [INDEXER] Search for Transactions func (c *SearchAPIController) SearchTransactions(w http.ResponseWriter, r *http.Request) { searchTransactionsRequest := &types.SearchTransactionsRequest{} @@ -90,7 +76,7 @@ func (c *SearchAPIController) SearchTransactions(w http.ResponseWriter, r *http. } result, serviceErr := c.service.SearchTransactions( - c.ContextFromRequest(r), + r.Context(), searchTransactionsRequest, ) if serviceErr != nil { diff --git a/server/routers.go b/server/routers.go index a762117d..27e8b95c 100644 --- a/server/routers.go +++ b/server/routers.go @@ -17,7 +17,6 @@ package server import ( - "context" "encoding/json" "net/http" @@ -38,7 +37,6 @@ type Routes []Route // Router defines the required methods for retrieving api routes type Router interface { Routes() Routes - ContextFromRequest(*http.Request) context.Context } // CorsMiddleware handles CORS and ensures OPTIONS requests are diff --git a/templates/server/controller-api.mustache b/templates/server/controller-api.mustache index 5d0f422a..dbe764f2 100644 --- a/templates/server/controller-api.mustache +++ b/templates/server/controller-api.mustache @@ -13,21 +13,18 @@ import ( // A {{classname}}Controller binds http requests to an api service and writes the service results to the http response type {{classname}}Controller struct { - service {{classname}}Servicer - asserter *asserter.Asserter - contextFromRequest func(*http.Request) context.Context + service {{classname}}Servicer + asserter *asserter.Asserter } // New{{classname}}Controller creates a default api controller func New{{classname}}Controller( s {{classname}}Servicer, asserter *asserter.Asserter, - contextFromRequest func(*http.Request) context.Context, ) Router { return &{{classname}}Controller{ service: s, asserter: asserter, - contextFromRequest: contextFromRequest, } } @@ -43,16 +40,6 @@ func (c *{{classname}}Controller) Routes() Routes { } } -func (c *{{classname}}Controller) ContextFromRequest(r *http.Request) context.Context { - ctx := r.Context() - - if c.contextFromRequest != nil { - ctx = c.contextFromRequest(r) - } - - return ctx -}{{#operations}}{{#operation}} - // {{nickname}} - {{{summary}}} func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Request) { {{#allParams}}{{#isHeaderParam}} {{paramName}} := r.Header.Get("{{paramName}}"){{/isHeaderParam}}{{#isBodyParam}} @@ -75,7 +62,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re } {{/isBodyParam}}{{/allParams}} - result, serviceErr := c.service.{{nickname}}(c.ContextFromRequest(r), {{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + result, serviceErr := c.service.{{nickname}}(r.Context(), {{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) if serviceErr != nil { EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w) diff --git a/templates/server/routers.mustache b/templates/server/routers.mustache index c0046c08..0abad5ca 100644 --- a/templates/server/routers.mustache +++ b/templates/server/routers.mustache @@ -23,7 +23,6 @@ type Routes []Route // Router defines the required methods for retrieving api routes type Router interface { Routes() Routes - ContextFromRequest(*http.Request) context.Context } // CorsMiddleware handles CORS and ensures OPTIONS requests are