0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

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
This commit is contained in:
Henrique Dias 2017-01-14 14:25:57 +00:00 committed by Matt Holt
parent 21d92d6873
commit 0155b0c5fb
2 changed files with 34 additions and 0 deletions

View file

@ -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 {

View file

@ -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 {
// 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