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

29 lines
721 B
Go

package server
import (
"fmt"
"net/http"
"github.com/rs/zerolog"
"gitlab.com/mstarongitlab/linstrom/server/middlewares"
"gitlab.com/mstarongitlab/linstrom/storage"
)
func ContextGetStorage(w http.ResponseWriter, r *http.Request) *storage.Storage {
val, _ := r.Context().Value(middlewares.CONTEXT_KEY_STORAGE).(*storage.Storage)
return val
}
func ContextGetLogger(w http.ResponseWriter, r *http.Request) *zerolog.Logger {
val, _ := r.Context().Value(middlewares.CONTEXT_KEY_LOGRUS).(*zerolog.Logger)
return val
}
func contextFail(w http.ResponseWriter, contextValName string) {
http.Error(
w,
fmt.Sprintf("failed to get %s from request context", contextValName),
http.StatusInternalServerError,
)
}