1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-24 08:42:17 +02:00

Change loaded modules middleware

It was simply too ugly to have a giant data structure for debugging.
This middleware should also basically never be used outside authboss
views so the allocation means nothing.
This commit is contained in:
Aaron L 2018-05-07 12:33:02 -07:00
parent 3ab0effe9c
commit 1afc6fcb01
3 changed files with 15 additions and 13 deletions

View File

@ -9,9 +9,9 @@ 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 contains a map[string]bool of which modules are loaded
// The bool is largely extraneous and can be ignored, if the module is loaded
// it will be present in the map, if not it will be missing.
DataModules = "modules"
)

View File

@ -87,13 +87,10 @@ func (a *Authboss) loadModule(name string) error {
// 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.
// views to show. The bool is extraneous, as presence in the map is the indication
// of wether or not the module is loaded.
// 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.
// map[modulename] = true
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) {
@ -107,7 +104,12 @@ func ModuleListMiddleware(ab *Authboss) func(http.Handler) http.Handler {
data = HTMLData{}
}
data[DataModules] = ab.loadedModules
loaded := make(map[string]bool, len(ab.loadedModules))
for k := range ab.loadedModules {
loaded[k] = true
}
data[DataModules] = loaded
r = r.WithContext(context.WithValue(ctx, CTXKeyData, data))
next.ServeHTTP(w, r)
})

View File

@ -80,10 +80,10 @@ func TestModuleLoadedMiddleware(t *testing.T) {
"auth": nil,
}
var mods map[string]Moduler
var mods map[string]bool
server := ModuleListMiddleware(ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := r.Context().Value(CTXKeyData).(HTMLData)
mods = data[DataModules].(map[string]Moduler)
mods = data[DataModules].(map[string]bool)
}))
server.ServeHTTP(nil, httptest.NewRequest("GET", "/", nil))
@ -96,6 +96,6 @@ func TestModuleLoadedMiddleware(t *testing.T) {
t.Error("auth should be loaded")
}
if _, ok := mods["recover"]; !ok {
t.Error("auth should be loaded")
t.Error("recover should be loaded")
}
}