mirror of
https://github.com/volatiletech/authboss.git
synced 2025-07-13 01:20: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:
12
authboss.go
12
authboss.go
@ -35,16 +35,8 @@ func (a *Authboss) Init(modulesToLoad ...string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range modulesToLoad {
|
for _, name := range modulesToLoad {
|
||||||
mod, ok := registeredModules[name]
|
if err := a.loadModule(name); err != nil {
|
||||||
if !ok {
|
return errors.Errorf("module %s failed to load", name)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
37
module.go
37
module.go
@ -1,5 +1,7 @@
|
|||||||
package authboss
|
package authboss
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
var registeredModules = make(map[string]Moduler)
|
var registeredModules = make(map[string]Moduler)
|
||||||
|
|
||||||
// Moduler should be implemented by all the authboss modules.
|
// Moduler should be implemented by all the authboss modules.
|
||||||
@ -43,3 +45,38 @@ func (a *Authboss) IsLoaded(mod string) bool {
|
|||||||
_, ok := a.loadedModules[mod]
|
_, ok := a.loadedModules[mod]
|
||||||
return ok
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user