From 43ade30f374990fb6ab7f06361772d03bf2a6bcc Mon Sep 17 00:00:00 2001 From: Shahzad Lone Date: Thu, 17 Oct 2024 20:45:07 +0400 Subject: [PATCH] PR(ADHERE): Adhere to code review - move to utils --- http/middleware.go | 57 ------------------------------------------- http/utils.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 57 deletions(-) diff --git a/http/middleware.go b/http/middleware.go index aada0d76b0..cc98473711 100644 --- a/http/middleware.go +++ b/http/middleware.go @@ -26,63 +26,6 @@ import ( "github.com/sourcenetwork/defradb/internal/db" ) -const ( - // txHeaderName is the name of the transaction header. - // This header should contain a valid transaction id. - txHeaderName = "x-defradb-tx" -) - -type contextKey string - -var ( - // txsContextKey is the context key for the transaction *sync.Map - txsContextKey = contextKey("txs") - // dbContextKey is the context key for the client.DB - dbContextKey = contextKey("db") - // colContextKey is the context key for the client.Collection - // - // If a transaction exists, all operations will be executed - // in the current transaction context. - colContextKey = contextKey("col") -) - -// mustGetContextClientCollection returns the client collection from the http request context or panics. -// -// This should only be called from functions within the http package. -func mustGetContextClientCollection(req *http.Request) client.Collection { - return req.Context().Value(colContextKey).(client.Collection) //nolint:forcetypeassert -} - -// mustGetContextSyncMap returns the sync map from the http request context or panics. -// -// This should only be called from functions within the http package. -func mustGetContextSyncMap(req *http.Request) *sync.Map { - return req.Context().Value(txsContextKey).(*sync.Map) //nolint:forcetypeassert -} - -// mustGetContextClientDB returns the client DB from the http request context or panics. -// -// This should only be called from functions within the http package. -func mustGetContextClientDB(req *http.Request) client.DB { - return req.Context().Value(dbContextKey).(client.DB) //nolint:forcetypeassert -} - -// mustGetContextClientStore returns the client store from the http request context or panics. -// -// This should only be called from functions within the http package. -func mustGetContextClientStore(req *http.Request) client.Store { - return req.Context().Value(dbContextKey).(client.Store) //nolint:forcetypeassert -} - -// tryGetContextClientP2P returns the P2P client from the http request context and a boolean -// indicating if p2p was enabled. -// -// This should only be called from functions within the http package. -func tryGetContextClientP2P(req *http.Request) (client.P2P, bool) { - p2p, ok := req.Context().Value(dbContextKey).(client.P2P) - return p2p, ok -} - // CorsMiddleware handles cross origin request func CorsMiddleware(allowedOrigins []string) func(http.Handler) http.Handler { return cors.Handler(cors.Options{ diff --git a/http/utils.go b/http/utils.go index 176fe3d035..9734058748 100644 --- a/http/utils.go +++ b/http/utils.go @@ -14,8 +14,68 @@ import ( "encoding/json" "io" "net/http" + "sync" + + "github.com/sourcenetwork/defradb/client" +) + +const ( + // txHeaderName is the name of the transaction header. + // This header should contain a valid transaction id. + txHeaderName = "x-defradb-tx" +) + +type contextKey string + +var ( + // txsContextKey is the context key for the transaction *sync.Map + txsContextKey = contextKey("txs") + // dbContextKey is the context key for the client.DB + dbContextKey = contextKey("db") + // colContextKey is the context key for the client.Collection + // + // If a transaction exists, all operations will be executed + // in the current transaction context. + colContextKey = contextKey("col") ) +// mustGetContextClientCollection returns the client collection from the http request context or panics. +// +// This should only be called from functions within the http package. +func mustGetContextClientCollection(req *http.Request) client.Collection { + return req.Context().Value(colContextKey).(client.Collection) //nolint:forcetypeassert +} + +// mustGetContextSyncMap returns the sync map from the http request context or panics. +// +// This should only be called from functions within the http package. +func mustGetContextSyncMap(req *http.Request) *sync.Map { + return req.Context().Value(txsContextKey).(*sync.Map) //nolint:forcetypeassert +} + +// mustGetContextClientDB returns the client DB from the http request context or panics. +// +// This should only be called from functions within the http package. +func mustGetContextClientDB(req *http.Request) client.DB { + return req.Context().Value(dbContextKey).(client.DB) //nolint:forcetypeassert +} + +// mustGetContextClientStore returns the client store from the http request context or panics. +// +// This should only be called from functions within the http package. +func mustGetContextClientStore(req *http.Request) client.Store { + return req.Context().Value(dbContextKey).(client.Store) //nolint:forcetypeassert +} + +// tryGetContextClientP2P returns the P2P client from the http request context and a boolean +// indicating if p2p was enabled. +// +// This should only be called from functions within the http package. +func tryGetContextClientP2P(req *http.Request) (client.P2P, bool) { + p2p, ok := req.Context().Value(dbContextKey).(client.P2P) + return p2p, ok +} + func requestJSON(req *http.Request, out any) error { data, err := io.ReadAll(req.Body) if err != nil {