Move Debug and ServiceContainer middlewares into their own packages
- Implements From/Must accessors to facilitate context values retrieval
This commit is contained in:
43
middleware/container/service_container.go
Normal file
43
middleware/container/service_container.go
Normal file
@ -0,0 +1,43 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
goweb "forge.cadoles.com/wpetit/goweb/middleware"
|
||||
"forge.cadoles.com/wpetit/goweb/service"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
// KeyServiceContainer is the context key associated with the ServiceContainer value
|
||||
KeyServiceContainer goweb.ContextKey = "serviceContainer"
|
||||
)
|
||||
|
||||
// ErrInvalidServiceContainer is returned when no service container
|
||||
// could be found on the given context
|
||||
var ErrInvalidServiceContainer = errors.New("invalid service container")
|
||||
|
||||
// From retrieves the service container from the given context
|
||||
func From(ctx context.Context) (*service.Container, error) {
|
||||
container, ok := ctx.Value(KeyServiceContainer).(*service.Container)
|
||||
if !ok {
|
||||
return nil, ErrInvalidServiceContainer
|
||||
}
|
||||
return container, nil
|
||||
}
|
||||
|
||||
// Must retrieves the service container from the given context or panics otherwise
|
||||
func Must(ctx context.Context) *service.Container {
|
||||
container, err := From(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
// ServiceContainer expose the given service container as a context value
|
||||
// on the HTTP requests
|
||||
func ServiceContainer(container *service.Container) goweb.Middleware {
|
||||
return middleware.WithValue(KeyServiceContainer, container)
|
||||
}
|
41
middleware/container/service_container_test.go
Normal file
41
middleware/container/service_container_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"forge.cadoles.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
func TestContextServiceContainer(t *testing.T) {
|
||||
|
||||
container := service.NewContainer()
|
||||
ctx := context.WithValue(context.Background(), KeyServiceContainer, container)
|
||||
|
||||
ctn, err := From(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ctn == nil {
|
||||
t.Fatal("container should not be nil")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestContextInvalidServiceContainer(t *testing.T) {
|
||||
|
||||
invalidContainer := struct{}{}
|
||||
ctx := context.WithValue(context.Background(), KeyServiceContainer, invalidContainer)
|
||||
|
||||
container, err := From(ctx)
|
||||
|
||||
if g, e := err, ErrInvalidServiceContainer; g != e {
|
||||
t.Errorf("err: got '%v', expected '%v'", g, e)
|
||||
}
|
||||
|
||||
if container != nil {
|
||||
t.Errorf("container: got '%v', expected '%v'", container, nil)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user