2019-03-27 05:45:51 +08:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-03-27 09:42:52 +08:00
|
|
|
"log"
|
2019-03-27 05:45:51 +08:00
|
|
|
"strings"
|
2019-03-27 09:42:52 +08:00
|
|
|
"sync"
|
2019-04-03 05:31:02 +08:00
|
|
|
"sync/atomic"
|
2019-03-27 05:45:51 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Start runs Caddy with the given config.
|
|
|
|
func Start(cfg Config) error {
|
2019-04-08 14:00:14 +08:00
|
|
|
// allow only one call to Start at a time,
|
|
|
|
// since various calls to LoadModule()
|
|
|
|
// access shared map moduleInstances
|
|
|
|
startMu.Lock()
|
|
|
|
defer startMu.Unlock()
|
|
|
|
|
|
|
|
// prepare the config for use
|
2019-03-27 05:45:51 +08:00
|
|
|
cfg.runners = make(map[string]Runner)
|
2019-04-08 14:00:14 +08:00
|
|
|
cfg.moduleStates = make(map[string]interface{})
|
|
|
|
|
|
|
|
// reset the shared moduleInstances map; but
|
|
|
|
// keep a temporary reference to the current
|
|
|
|
// one so we can transfer over any necessary
|
|
|
|
// state to the new modules; or in case this
|
|
|
|
// function returns an error, we need to put
|
|
|
|
// the "old" one back where we found it
|
|
|
|
var err error
|
|
|
|
oldModuleInstances := moduleInstances
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
moduleInstances = oldModuleInstances
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
moduleInstances = make(map[string][]interface{})
|
2019-03-27 05:45:51 +08:00
|
|
|
|
2019-04-08 14:00:14 +08:00
|
|
|
// load (decode) each runner module
|
2019-03-27 05:45:51 +08:00
|
|
|
for modName, rawMsg := range cfg.Modules {
|
2019-04-01 10:41:29 +08:00
|
|
|
val, err := LoadModule(modName, rawMsg)
|
2019-03-27 05:45:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading module '%s': %v", modName, err)
|
|
|
|
}
|
|
|
|
cfg.runners[modName] = val.(Runner)
|
|
|
|
}
|
|
|
|
|
2019-04-03 05:31:02 +08:00
|
|
|
// start the new runners
|
2019-03-27 05:45:51 +08:00
|
|
|
for name, r := range cfg.runners {
|
|
|
|
err := r.Run()
|
|
|
|
if err != nil {
|
2019-03-27 09:42:52 +08:00
|
|
|
// TODO: If any one has an error, stop the others
|
2019-03-27 05:45:51 +08:00
|
|
|
return fmt.Errorf("%s module: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 14:00:14 +08:00
|
|
|
// shut down down the old runners
|
2019-03-27 09:42:52 +08:00
|
|
|
currentCfgMu.Lock()
|
|
|
|
if currentCfg != nil {
|
2019-04-08 14:00:14 +08:00
|
|
|
for name, r := range currentCfg.runners {
|
2019-03-27 09:42:52 +08:00
|
|
|
err := r.Cancel()
|
|
|
|
if err != nil {
|
2019-04-08 14:00:14 +08:00
|
|
|
log.Printf("[ERROR] cancel %s: %v", name, err)
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 14:00:14 +08:00
|
|
|
oldCfg := currentCfg
|
2019-03-27 09:42:52 +08:00
|
|
|
currentCfg = &cfg
|
|
|
|
currentCfgMu.Unlock()
|
|
|
|
|
2019-04-08 14:00:14 +08:00
|
|
|
// invoke unload callbacks on old configuration
|
|
|
|
for modName := range oldModuleInstances {
|
|
|
|
mod, err := GetModule(modName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mod.OnUnload != nil {
|
|
|
|
var unloadingState interface{}
|
|
|
|
if oldCfg != nil {
|
|
|
|
unloadingState = oldCfg.moduleStates[modName]
|
|
|
|
}
|
|
|
|
err := mod.OnUnload(unloadingState)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] module OnUnload: %s: %v", modName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoke load callbacks on new configuration
|
|
|
|
for modName, instances := range moduleInstances {
|
|
|
|
mod, err := GetModule(modName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mod.OnLoad != nil {
|
|
|
|
var priorState interface{}
|
|
|
|
if oldCfg != nil {
|
|
|
|
priorState = oldCfg.moduleStates[modName]
|
|
|
|
}
|
|
|
|
modState, err := mod.OnLoad(instances, priorState)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("module OnLoad: %s: %v", modName, err)
|
|
|
|
}
|
|
|
|
if modState != nil {
|
|
|
|
cfg.moduleStates[modName] = modState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 05:31:02 +08:00
|
|
|
// shut down listeners that are no longer being used
|
|
|
|
listenersMu.Lock()
|
|
|
|
for key, info := range listeners {
|
|
|
|
if atomic.LoadInt32(&info.usage) == 0 {
|
|
|
|
err := info.ln.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] closing listener %s: %v", info.ln.Addr(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(listeners, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listenersMu.Unlock()
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runner is a thing that Caddy runs.
|
|
|
|
type Runner interface {
|
|
|
|
Run() error
|
2019-03-27 09:42:52 +08:00
|
|
|
Cancel() error
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config represents a Caddy configuration.
|
|
|
|
type Config struct {
|
|
|
|
TestVal string `json:"testval"`
|
|
|
|
Modules map[string]json.RawMessage `json:"modules"`
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// runners stores the decoded Modules values,
|
|
|
|
// keyed by module name.
|
2019-03-27 05:45:51 +08:00
|
|
|
runners map[string]Runner
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// moduleStates stores the optional "global" state
|
|
|
|
// values of every module used by this configuration,
|
|
|
|
// keyed by module name.
|
|
|
|
moduleStates map[string]interface{}
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Duration is a JSON-string-unmarshable duration type.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2019-03-27 05:45:51 +08:00
|
|
|
dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
|
2019-04-01 10:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-27 05:45:51 +08:00
|
|
|
cd := Duration(dd)
|
|
|
|
d = &cd
|
2019-04-01 10:41:29 +08:00
|
|
|
return nil
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:42:55 +08:00
|
|
|
// CtxKey is a value type for use with context.WithValue.
|
|
|
|
type CtxKey string
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// currentCfg is the currently-loaded configuration.
|
|
|
|
var (
|
|
|
|
currentCfg *Config
|
|
|
|
currentCfgMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// moduleInstances stores the individual instantiated
|
|
|
|
// values of modules, keyed by module name. The list
|
|
|
|
// of instances of each module get passed into the
|
|
|
|
// respective module's OnLoad callback, so they can
|
|
|
|
// set up any global state and/or make sure their
|
|
|
|
// configuration, when viewed as a whole, is valid.
|
|
|
|
// Since this list is shared, only one Start() routine
|
|
|
|
// must be allowed to happen at any given time.
|
|
|
|
var moduleInstances = make(map[string][]interface{})
|
|
|
|
|
|
|
|
// startMu ensures that only one Start() happens at a time.
|
|
|
|
// This is important since
|
|
|
|
var startMu sync.Mutex
|