From 38f4c7b7359cc77994c1db414aac72fd3cf12616 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 7 Dec 2018 10:13:53 +0100 Subject: [PATCH] Move Debug and ServiceContainer middlewares into their own packages - Implements From/Must accessors to facilitate context values retrieval --- goweb_test.go | 13 +++++------- .../{ => container}/service_container.go | 20 ++++++++++++++----- .../{ => container}/service_container_test.go | 6 +++--- middleware/{ => debug}/debug.go | 20 ++++++++++++++----- middleware/{ => debug}/debug_test.go | 4 ++-- 5 files changed, 40 insertions(+), 23 deletions(-) rename middleware/{ => container}/service_container.go (56%) rename middleware/{ => container}/service_container_test.go (87%) rename middleware/{ => debug}/debug.go (54%) rename middleware/{ => debug}/debug_test.go (84%) diff --git a/goweb_test.go b/goweb_test.go index 83bb385..31d69a1 100644 --- a/goweb_test.go +++ b/goweb_test.go @@ -3,7 +3,7 @@ package goweb import ( "net/http" - "forge.cadoles.com/wpetit/goweb/middleware" + containerMiddleware "forge.cadoles.com/wpetit/goweb/middleware/container" "forge.cadoles.com/wpetit/goweb/service" "forge.cadoles.com/wpetit/goweb/service/template" "forge.cadoles.com/wpetit/goweb/template/html" @@ -26,7 +26,7 @@ func Example_usage() { // On utilise le middleware "ServiceContainer" pour exposer // le conteneur de service dans nos requêtes - router.Use(middleware.ServiceContainer(container)) + router.Use(containerMiddleware.ServiceContainer(container)) // On créait un handler pour la page d'accueil router.Get("/", ServeHomePage) @@ -39,16 +39,13 @@ func Example_usage() { func ServeHomePage(w http.ResponseWriter, r *http.Request) { - // On récupère le conteneur de service depuis le contexte de la requête - container, err := middleware.GetServiceContainer(r.Context()) - if err != nil { - panic(err) - } + // On récupère le conteneur de services depuis le contexte de la requête + container := containerMiddleware.Must(r.Context()) // On récupère le service "template" depuis le conteneur de service templateService := template.Must(container) - // On utilise notre + // On utilise notre service de template if err := templateService.RenderPage(w, "my-template.html.tmpl", nil); err != nil { panic(err) } diff --git a/middleware/service_container.go b/middleware/container/service_container.go similarity index 56% rename from middleware/service_container.go rename to middleware/container/service_container.go index 07623b7..d3f9e9f 100644 --- a/middleware/service_container.go +++ b/middleware/container/service_container.go @@ -1,24 +1,25 @@ -package middleware +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 ContextKey = "serviceContainer" + 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") -// GetServiceContainer retrieves the service container from the given context -func GetServiceContainer(ctx context.Context) (*service.Container, error) { +// 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 @@ -26,8 +27,17 @@ func GetServiceContainer(ctx context.Context) (*service.Container, error) { 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) Middleware { +func ServiceContainer(container *service.Container) goweb.Middleware { return middleware.WithValue(KeyServiceContainer, container) } diff --git a/middleware/service_container_test.go b/middleware/container/service_container_test.go similarity index 87% rename from middleware/service_container_test.go rename to middleware/container/service_container_test.go index 9c92cf3..39f4736 100644 --- a/middleware/service_container_test.go +++ b/middleware/container/service_container_test.go @@ -1,4 +1,4 @@ -package middleware +package container import ( "context" @@ -12,7 +12,7 @@ func TestContextServiceContainer(t *testing.T) { container := service.NewContainer() ctx := context.WithValue(context.Background(), KeyServiceContainer, container) - ctn, err := GetServiceContainer(ctx) + ctn, err := From(ctx) if err != nil { t.Fatal(err) } @@ -28,7 +28,7 @@ func TestContextInvalidServiceContainer(t *testing.T) { invalidContainer := struct{}{} ctx := context.WithValue(context.Background(), KeyServiceContainer, invalidContainer) - container, err := GetServiceContainer(ctx) + container, err := From(ctx) if g, e := err, ErrInvalidServiceContainer; g != e { t.Errorf("err: got '%v', expected '%v'", g, e) diff --git a/middleware/debug.go b/middleware/debug/debug.go similarity index 54% rename from middleware/debug.go rename to middleware/debug/debug.go index 7e89bfc..5273a2f 100644 --- a/middleware/debug.go +++ b/middleware/debug/debug.go @@ -1,23 +1,24 @@ -package middleware +package debug import ( "context" "errors" + goweb "forge.cadoles.com/wpetit/goweb/middleware" "github.com/go-chi/chi/middleware" ) const ( // KeyDebug is the context key associated with the debug value - KeyDebug ContextKey = "debug" + KeyDebug goweb.ContextKey = "debug" ) // ErrInvalidDebug is returned when no debug value // could be found on the given context var ErrInvalidDebug = errors.New("invalid debug") -// GetDebug retrieves the debug value from the given context -func GetDebug(ctx context.Context) (bool, error) { +// From retrieves the debug value from the given context +func From(ctx context.Context) (bool, error) { debug, ok := ctx.Value(KeyDebug).(bool) if !ok { return false, ErrInvalidDebug @@ -25,8 +26,17 @@ func GetDebug(ctx context.Context) (bool, error) { return debug, nil } +// Must retrieves the debug value from the given context or panics otherwise +func Must(ctx context.Context) bool { + debug, err := From(ctx) + if err != nil { + panic(err) + } + return debug +} + // Debug expose the given debug flag as a context value // on the HTTP requests -func Debug(debug bool) Middleware { +func Debug(debug bool) goweb.Middleware { return middleware.WithValue(KeyDebug, debug) } diff --git a/middleware/debug_test.go b/middleware/debug/debug_test.go similarity index 84% rename from middleware/debug_test.go rename to middleware/debug/debug_test.go index ae09718..ff92741 100644 --- a/middleware/debug_test.go +++ b/middleware/debug/debug_test.go @@ -1,4 +1,4 @@ -package middleware +package debug import ( "context" @@ -10,7 +10,7 @@ func TestContextDebug(t *testing.T) { debug := false ctx := context.WithValue(context.Background(), KeyDebug, debug) - dbg, err := GetDebug(ctx) + dbg, err := From(ctx) if err != nil { t.Fatal(err) }