Move Debug and ServiceContainer middlewares into their own packages
- Implements From/Must accessors to facilitate context values retrieval
This commit is contained in:
parent
a2b0ab6471
commit
38f4c7b735
@ -3,7 +3,7 @@ package goweb
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"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"
|
||||||
"forge.cadoles.com/wpetit/goweb/service/template"
|
"forge.cadoles.com/wpetit/goweb/service/template"
|
||||||
"forge.cadoles.com/wpetit/goweb/template/html"
|
"forge.cadoles.com/wpetit/goweb/template/html"
|
||||||
@ -26,7 +26,7 @@ func Example_usage() {
|
|||||||
|
|
||||||
// On utilise le middleware "ServiceContainer" pour exposer
|
// On utilise le middleware "ServiceContainer" pour exposer
|
||||||
// le conteneur de service dans nos requêtes
|
// 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
|
// On créait un handler pour la page d'accueil
|
||||||
router.Get("/", ServeHomePage)
|
router.Get("/", ServeHomePage)
|
||||||
@ -39,16 +39,13 @@ func Example_usage() {
|
|||||||
|
|
||||||
func ServeHomePage(w http.ResponseWriter, r *http.Request) {
|
func ServeHomePage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// On récupère le conteneur de service depuis le contexte de la requête
|
// On récupère le conteneur de services depuis le contexte de la requête
|
||||||
container, err := middleware.GetServiceContainer(r.Context())
|
container := containerMiddleware.Must(r.Context())
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// On récupère le service "template" depuis le conteneur de service
|
// On récupère le service "template" depuis le conteneur de service
|
||||||
templateService := template.Must(container)
|
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 {
|
if err := templateService.RenderPage(w, "my-template.html.tmpl", nil); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,25 @@
|
|||||||
package middleware
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
goweb "forge.cadoles.com/wpetit/goweb/middleware"
|
||||||
"forge.cadoles.com/wpetit/goweb/service"
|
"forge.cadoles.com/wpetit/goweb/service"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// KeyServiceContainer is the context key associated with the ServiceContainer value
|
// KeyServiceContainer is the context key associated with the ServiceContainer value
|
||||||
KeyServiceContainer ContextKey = "serviceContainer"
|
KeyServiceContainer goweb.ContextKey = "serviceContainer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrInvalidServiceContainer is returned when no service container
|
// ErrInvalidServiceContainer is returned when no service container
|
||||||
// could be found on the given context
|
// could be found on the given context
|
||||||
var ErrInvalidServiceContainer = errors.New("invalid service container")
|
var ErrInvalidServiceContainer = errors.New("invalid service container")
|
||||||
|
|
||||||
// GetServiceContainer retrieves the service container from the given context
|
// From retrieves the service container from the given context
|
||||||
func GetServiceContainer(ctx context.Context) (*service.Container, error) {
|
func From(ctx context.Context) (*service.Container, error) {
|
||||||
container, ok := ctx.Value(KeyServiceContainer).(*service.Container)
|
container, ok := ctx.Value(KeyServiceContainer).(*service.Container)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrInvalidServiceContainer
|
return nil, ErrInvalidServiceContainer
|
||||||
@ -26,8 +27,17 @@ func GetServiceContainer(ctx context.Context) (*service.Container, error) {
|
|||||||
return container, nil
|
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
|
// ServiceContainer expose the given service container as a context value
|
||||||
// on the HTTP requests
|
// on the HTTP requests
|
||||||
func ServiceContainer(container *service.Container) Middleware {
|
func ServiceContainer(container *service.Container) goweb.Middleware {
|
||||||
return middleware.WithValue(KeyServiceContainer, container)
|
return middleware.WithValue(KeyServiceContainer, container)
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package middleware
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -12,7 +12,7 @@ func TestContextServiceContainer(t *testing.T) {
|
|||||||
container := service.NewContainer()
|
container := service.NewContainer()
|
||||||
ctx := context.WithValue(context.Background(), KeyServiceContainer, container)
|
ctx := context.WithValue(context.Background(), KeyServiceContainer, container)
|
||||||
|
|
||||||
ctn, err := GetServiceContainer(ctx)
|
ctn, err := From(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@ func TestContextInvalidServiceContainer(t *testing.T) {
|
|||||||
invalidContainer := struct{}{}
|
invalidContainer := struct{}{}
|
||||||
ctx := context.WithValue(context.Background(), KeyServiceContainer, invalidContainer)
|
ctx := context.WithValue(context.Background(), KeyServiceContainer, invalidContainer)
|
||||||
|
|
||||||
container, err := GetServiceContainer(ctx)
|
container, err := From(ctx)
|
||||||
|
|
||||||
if g, e := err, ErrInvalidServiceContainer; g != e {
|
if g, e := err, ErrInvalidServiceContainer; g != e {
|
||||||
t.Errorf("err: got '%v', expected '%v'", g, e)
|
t.Errorf("err: got '%v', expected '%v'", g, e)
|
@ -1,23 +1,24 @@
|
|||||||
package middleware
|
package debug
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
goweb "forge.cadoles.com/wpetit/goweb/middleware"
|
||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// KeyDebug is the context key associated with the debug value
|
// KeyDebug is the context key associated with the debug value
|
||||||
KeyDebug ContextKey = "debug"
|
KeyDebug goweb.ContextKey = "debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrInvalidDebug is returned when no debug value
|
// ErrInvalidDebug is returned when no debug value
|
||||||
// could be found on the given context
|
// could be found on the given context
|
||||||
var ErrInvalidDebug = errors.New("invalid debug")
|
var ErrInvalidDebug = errors.New("invalid debug")
|
||||||
|
|
||||||
// GetDebug retrieves the debug value from the given context
|
// From retrieves the debug value from the given context
|
||||||
func GetDebug(ctx context.Context) (bool, error) {
|
func From(ctx context.Context) (bool, error) {
|
||||||
debug, ok := ctx.Value(KeyDebug).(bool)
|
debug, ok := ctx.Value(KeyDebug).(bool)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, ErrInvalidDebug
|
return false, ErrInvalidDebug
|
||||||
@ -25,8 +26,17 @@ func GetDebug(ctx context.Context) (bool, error) {
|
|||||||
return debug, nil
|
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
|
// Debug expose the given debug flag as a context value
|
||||||
// on the HTTP requests
|
// on the HTTP requests
|
||||||
func Debug(debug bool) Middleware {
|
func Debug(debug bool) goweb.Middleware {
|
||||||
return middleware.WithValue(KeyDebug, debug)
|
return middleware.WithValue(KeyDebug, debug)
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package middleware
|
package debug
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -10,7 +10,7 @@ func TestContextDebug(t *testing.T) {
|
|||||||
debug := false
|
debug := false
|
||||||
ctx := context.WithValue(context.Background(), KeyDebug, debug)
|
ctx := context.WithValue(context.Background(), KeyDebug, debug)
|
||||||
|
|
||||||
dbg, err := GetDebug(ctx)
|
dbg, err := From(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user