diff --git a/caddy/caddymain/run.go b/caddy/caddymain/run.go index 0d8e4de1..a38c95d3 100644 --- a/caddy/caddymain/run.go +++ b/caddy/caddymain/run.go @@ -98,6 +98,12 @@ func Run() { mustLogFatalf("%v", err.Error()) } + // Execute plugins that are registered to run as the process starts + err = caddy.StartupHooks(serverType) + if err != nil { + mustLogFatalf("%v", err) + } + // Get Caddyfile input caddyfileinput, err := caddy.LoadCaddyfile(serverType) if err != nil { diff --git a/plugins.go b/plugins.go index 92526ad8..885b621a 100644 --- a/plugins.go +++ b/plugins.go @@ -69,6 +69,30 @@ func DescribePlugins() string { return str } +// StartupHooks executes the startup hooks defined when the +// plugins were registered and returns the first error +// it encounters. +func StartupHooks(serverType string) error { + for stype, stypePlugins := range plugins { + if stype != "" && stype != serverType { + continue + } + + for name := range stypePlugins { + if stypePlugins[name].StartupHook == nil { + continue + } + + err := stypePlugins[name].StartupHook() + if err != nil { + return err + } + } + } + + return nil +} + // ValidDirectives returns the list of all directives that are // recognized for the server type serverType. However, not all // directives may be installed. This makes it possible to give @@ -176,6 +200,10 @@ type Plugin struct { // Action is the plugin's setup function, if associated // with a directive in the Caddyfile. Action SetupFunc + + // StartupHook is the plugin's function that is executed + // immediately after the flag parsing. + StartupHook func() error } // RegisterPlugin plugs in plugin. All plugins should register