linstrom/server-old/middlewares/injectStorage.go
2024-08-22 19:57:53 +02:00

22 lines
624 B
Go

package middlewares
import (
"context"
"net/http"
"gitlab.com/mstarongitlab/linstrom/storage"
)
const CONTEXT_KEY_STORAGE = ContextKey("storage")
// Build a middleware for injecting a storage reference into the context
func InjectStorageMiddlewareBuilder(store *storage.Storage) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContext := r.Context()
newContext := context.WithValue(reqContext, CONTEXT_KEY_STORAGE, store)
newRequest := r.WithContext(newContext)
h.ServeHTTP(w, newRequest)
})
}
}