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 (
|
||||
"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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue