2019-03-26 16:45:51 -05:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-03-26 20:42:52 -05:00
|
|
|
"log"
|
2019-03-26 16:45:51 -05:00
|
|
|
"strings"
|
2019-03-26 20:42:52 -05:00
|
|
|
"sync"
|
2019-04-02 16:31:02 -05:00
|
|
|
"sync/atomic"
|
2019-03-26 16:45:51 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-03-26 20:42:52 -05:00
|
|
|
var currentCfg *Config
|
|
|
|
var currentCfgMu sync.Mutex
|
|
|
|
|
2019-03-26 16:45:51 -05:00
|
|
|
// Start runs Caddy with the given config.
|
|
|
|
func Start(cfg Config) error {
|
|
|
|
cfg.runners = make(map[string]Runner)
|
|
|
|
|
|
|
|
for modName, rawMsg := range cfg.Modules {
|
2019-03-31 21:41:29 -05:00
|
|
|
val, err := LoadModule(modName, rawMsg)
|
2019-03-26 16:45:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading module '%s': %v", modName, err)
|
|
|
|
}
|
|
|
|
cfg.runners[modName] = val.(Runner)
|
|
|
|
}
|
|
|
|
|
2019-04-02 16:31:02 -05:00
|
|
|
// start the new runners
|
2019-03-26 16:45:51 -05:00
|
|
|
for name, r := range cfg.runners {
|
|
|
|
err := r.Run()
|
|
|
|
if err != nil {
|
2019-03-26 20:42:52 -05:00
|
|
|
// TODO: If any one has an error, stop the others
|
2019-03-26 16:45:51 -05:00
|
|
|
return fmt.Errorf("%s module: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 16:31:02 -05:00
|
|
|
// shut down down the old ones
|
2019-03-26 20:42:52 -05:00
|
|
|
currentCfgMu.Lock()
|
|
|
|
if currentCfg != nil {
|
2019-04-02 15:58:24 -05:00
|
|
|
for _, r := range currentCfg.runners {
|
2019-03-26 20:42:52 -05:00
|
|
|
err := r.Cancel()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentCfg = &cfg
|
|
|
|
currentCfgMu.Unlock()
|
|
|
|
|
2019-04-02 16:31:02 -05: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-26 16:45:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runner is a thing that Caddy runs.
|
|
|
|
type Runner interface {
|
|
|
|
Run() error
|
2019-03-26 20:42:52 -05:00
|
|
|
Cancel() error
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config represents a Caddy configuration.
|
|
|
|
type Config struct {
|
|
|
|
TestVal string `json:"testval"`
|
|
|
|
Modules map[string]json.RawMessage `json:"modules"`
|
|
|
|
runners map[string]Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration is a JSON-string-unmarshable duration type.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-03-31 21:41:29 -05:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2019-03-26 16:45:51 -05:00
|
|
|
dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
|
2019-03-31 21:41:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-26 16:45:51 -05:00
|
|
|
cd := Duration(dd)
|
|
|
|
d = &cd
|
2019-03-31 21:41:29 -05:00
|
|
|
return nil
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON satisfies json.Marshaler.
|
2019-03-31 21:41:29 -05:00
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
2019-03-26 16:45:51 -05:00
|
|
|
return []byte(fmt.Sprintf(`"%s"`, time.Duration(d).String())), nil
|
|
|
|
}
|