Commit 0155b0c5 authored by Henrique Dias's avatar Henrique Dias Committed by Matt Holt

Add StartupHooks to Plugins (#1330)

* Update run.go

* Update plugins.go

* Update plugins.go

* Update run.go

* typo

* Update plugins.go

* Update plugins.go

* Requested changes by @mholt
parent 21d92d68
...@@ -98,6 +98,12 @@ func Run() { ...@@ -98,6 +98,12 @@ func Run() {
mustLogFatalf("%v", err.Error()) 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 // Get Caddyfile input
caddyfileinput, err := caddy.LoadCaddyfile(serverType) caddyfileinput, err := caddy.LoadCaddyfile(serverType)
if err != nil { if err != nil {
......
...@@ -69,6 +69,30 @@ func DescribePlugins() string { ...@@ -69,6 +69,30 @@ func DescribePlugins() string {
return str 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 // ValidDirectives returns the list of all directives that are
// recognized for the server type serverType. However, not all // recognized for the server type serverType. However, not all
// directives may be installed. This makes it possible to give // directives may be installed. This makes it possible to give
...@@ -176,6 +200,10 @@ type Plugin struct { ...@@ -176,6 +200,10 @@ type Plugin struct {
// Action is the plugin's setup function, if associated // Action is the plugin's setup function, if associated
// with a directive in the Caddyfile. // with a directive in the Caddyfile.
Action SetupFunc 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 // RegisterPlugin plugs in plugin. All plugins should register
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment