summaryrefslogtreecommitdiffhomepage
path: root/modules.go
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2019-04-26 12:35:39 -0600
committerMatthew Holt <[email protected]>2019-04-26 12:35:39 -0600
commit43961b542b077f99f78d64629348b9300d3cd4a3 (patch)
treef4d2b9d8478064912daf1e5de49803afcd7de242 /modules.go
parent2d056fbe66849f041a233a0d961639fae3835cbb (diff)
downloadcaddy-43961b542b077f99f78d64629348b9300d3cd4a3.tar.gz
caddy-43961b542b077f99f78d64629348b9300d3cd4a3.zip
General cleanup and more godocs
Diffstat (limited to 'modules.go')
-rw-r--r--modules.go39
1 files changed, 34 insertions, 5 deletions
diff --git a/modules.go b/modules.go
index 0fd898e9d..5a4e69fc0 100644
--- a/modules.go
+++ b/modules.go
@@ -11,15 +11,44 @@ import (
// Module represents a Caddy module.
type Module struct {
- Name string
- New func() (interface{}, error)
- OnLoad func(instances []interface{}, priorState interface{}) (newState interface{}, err error)
+ // Name is the full name of the module. It
+ // must be unique and properly namespaced.
+ Name string
+
+ // New returns a new, empty instance of
+ // the module's type. The host module
+ // which loads this module will likely
+ // invoke methods on the returned value.
+ // It must return a pointer; if not, it
+ // is converted into one.
+ New func() (interface{}, error)
+
+ // OnLoad is invoked after all module
+ // instances ave been loaded. It receives
+ // pointers to each instance of this
+ // module, and any state from a previous
+ // running configuration, which may be
+ // nil.
+ //
+ // If this module is to carry "global"
+ // state between all instances through
+ // reloads, you might find it helpful
+ // to return it.
+ // TODO: Is this really better/safer than a global variable?
+ OnLoad func(instances []interface{}, priorState interface{}) (newState interface{}, err error)
+
+ // OnUnload is called after all module
+ // instances have been stopped, possibly
+ // in favor of a new configuration. It
+ // receives the state given by OnLoad (if
+ // any).
OnUnload func(state interface{}) error
}
func (m Module) String() string { return m.Name }
-// RegisterModule registers a module.
+// RegisterModule registers a module. Modules must call
+// this function in the init phase of runtime.
func RegisterModule(mod Module) error {
if mod.Name == "caddy" {
return fmt.Errorf("modules cannot be named 'caddy'")
@@ -35,7 +64,7 @@ func RegisterModule(mod Module) error {
return nil
}
-// GetModule returns a module by name.
+// GetModule returns a module by its full name.
func GetModule(name string) (Module, error) {
modulesMu.Lock()
defer modulesMu.Unlock()