mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-24 08:42:17 +02:00
Add module list middleware
This commit is contained in:
parent
563fd622be
commit
a9c161e940
@ -9,6 +9,10 @@ const (
|
||||
DataValidation = "errors"
|
||||
// DataPreserve preserves fields
|
||||
DataPreserve = "preserve"
|
||||
// DataModules contains a map[string]Moduler of which modules are loaded
|
||||
// the Init() method should NEVER be called in a template. This structure
|
||||
// is only returned to avoid allocations.
|
||||
DataModules = "modules"
|
||||
)
|
||||
|
||||
// HTMLData is used to render templates with.
|
||||
|
35
module.go
35
module.go
@ -1,6 +1,10 @@
|
||||
package authboss
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var registeredModules = make(map[string]Moduler)
|
||||
|
||||
@ -80,3 +84,32 @@ func (a *Authboss) loadModule(name string) error {
|
||||
a.loadedModules[name] = mod
|
||||
return mod.Init(a)
|
||||
}
|
||||
|
||||
// ModuleListMiddleware puts a map in the data that can be used
|
||||
// to provide the renderer with information about which pieces of the
|
||||
// views to show.
|
||||
// Data looks like:
|
||||
// map[modulename] = Moduler
|
||||
//
|
||||
// The Moduler interface type should be ignored, and the key being present
|
||||
// or not is the only important point. Copying this data structure ignores
|
||||
// allocations.
|
||||
func ModuleListMiddleware(ab *Authboss) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var data HTMLData
|
||||
|
||||
ctx := r.Context()
|
||||
dataIntf := ctx.Value(CTXKeyData)
|
||||
if dataIntf != nil {
|
||||
data = dataIntf.(HTMLData)
|
||||
} else {
|
||||
data = HTMLData{}
|
||||
}
|
||||
|
||||
data[DataModules] = ab.loadedModules
|
||||
r = r.WithContext(context.WithValue(ctx, CTXKeyData, data))
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package authboss
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -68,3 +69,33 @@ func TestIsLoaded(t *testing.T) {
|
||||
t.Error("Loaded modules wrong:", loaded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModuleLoadedMiddleware(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ab := New()
|
||||
|
||||
ab.loadedModules = map[string]Moduler{
|
||||
"recover": nil,
|
||||
"auth": nil,
|
||||
}
|
||||
|
||||
var mods map[string]Moduler
|
||||
server := ModuleListMiddleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
data := r.Context().Value(CTXKeyData).(HTMLData)
|
||||
mods = data[DataModules].(map[string]Moduler)
|
||||
}))
|
||||
|
||||
server.ServeHTTP(nil, httptest.NewRequest("GET", "/", nil))
|
||||
|
||||
if len(mods) != 2 {
|
||||
t.Error("want two modules, got:", len(mods))
|
||||
}
|
||||
|
||||
if _, ok := mods["auth"]; !ok {
|
||||
t.Error("auth should be loaded")
|
||||
}
|
||||
if _, ok := mods["recover"]; !ok {
|
||||
t.Error("auth should be loaded")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user