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

Revert a change to removing some code

- This code is useful and this commit adds a comment to say why
This commit is contained in:
Aaron L 2018-02-01 17:10:26 -08:00
parent ad5230a303
commit 8f546fdd7c
2 changed files with 39 additions and 10 deletions

View File

@ -35,16 +35,8 @@ func (a *Authboss) Init(modulesToLoad ...string) error {
}
for _, name := range modulesToLoad {
mod, ok := registeredModules[name]
if !ok {
return errors.Errorf("module %s was supposed to be loaded but is not registered", name)
}
a.loadedModules[name] = mod
// Initialize the module
if err := mod.Init(a); err != nil {
return errors.Wrapf(err, "failed to init module: %s", name)
if err := a.loadModule(name); err != nil {
return errors.Errorf("module %s failed to load", name)
}
}

View File

@ -1,5 +1,7 @@
package authboss
import "reflect"
var registeredModules = make(map[string]Moduler)
// Moduler should be implemented by all the authboss modules.
@ -43,3 +45,38 @@ func (a *Authboss) IsLoaded(mod string) bool {
_, ok := a.loadedModules[mod]
return ok
}
// loadModule loads a particular module. It uses reflection to create a new
// instance of the module type. The original value is copied, but not deep copied
// so care should be taken to make sure most initialization happens inside the Initialize()
// method of the module.
//
// This method exists so many copies of authboss can be loaded and initialized at the same time
// if we didn't use this, then the registeredModules instances of the modules would end up used
// by the first instance of authboss.
func (a *Authboss) loadModule(name string) error {
module, ok := registeredModules[name]
if !ok {
panic("could not find module: " + name)
}
var wasPtr bool
modVal := reflect.ValueOf(module)
if modVal.Kind() == reflect.Ptr {
wasPtr = true
modVal = modVal.Elem()
}
modType := modVal.Type()
value := reflect.New(modType)
if !wasPtr {
value = value.Elem()
value.Set(modVal)
} else {
value.Elem().Set(modVal)
}
mod, ok := value.Interface().(Moduler)
a.loadedModules[name] = mod
return mod.Init(a)
}