package middleware import ( "context" "errors" "forge.cadoles.com/wpetit/goweb/service" "github.com/go-chi/chi/middleware" ) const ( // KeyServiceContainer is the context key associated with the ServiceContainer value KeyServiceContainer ContextKey = "serviceContainer" ) // ErrInvalidServiceContainer is returned when no service container // could be found on the given context var ErrInvalidServiceContainer = errors.New("invalid service container") // GetServiceContainer retrieves the service container from the given context func GetServiceContainer(ctx context.Context) (*service.Container, error) { container, ok := ctx.Value(KeyServiceContainer).(*service.Container) if !ok { return nil, ErrInvalidServiceContainer } return container, nil } // ServiceContainer expose the given service container as a context value // on the HTTP requests func ServiceContainer(container *service.Container) Middleware { return middleware.WithValue(KeyServiceContainer, container) }