Skip to content

Commit

Permalink
PR(ADHERE): Adhere to code review - move to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Oct 17, 2024
1 parent f96e88b commit 77bdcdb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 57 deletions.
57 changes: 0 additions & 57 deletions http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
60 changes: 60 additions & 0 deletions http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 77bdcdb

Please sign in to comment.