summaryrefslogtreecommitdiff
path: root/caddy.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-03-26 15:45:51 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-03-26 15:45:51 -0600
commit86e2d1b0a48fbd84590291969611f1870471c3e0 (patch)
treed3a8d82ca88f7d7ff60476cd772d8c4314cd6a3f /caddy.go
parent859b5d7ea3b8f660ac68d9aea5a53d25a9a7422c (diff)
Rudimentary start of HTTP servers
Diffstat (limited to 'caddy.go')
-rw-r--r--caddy.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/caddy.go b/caddy.go
new file mode 100644
index 0000000..2f12776
--- /dev/null
+++ b/caddy.go
@@ -0,0 +1,62 @@
+package caddy2
+
+import (
+ "encoding/json"
+ "fmt"
+ "strings"
+ "time"
+)
+
+// Start runs Caddy with the given config.
+func Start(cfg Config) error {
+ cfg.runners = make(map[string]Runner)
+
+ for modName, rawMsg := range cfg.Modules {
+ mod, ok := modules[modName]
+ if !ok {
+ return fmt.Errorf("unrecognized module: %s", modName)
+ }
+ val, err := LoadModule(mod, rawMsg)
+ if err != nil {
+ return fmt.Errorf("loading module '%s': %v", modName, err)
+ }
+ cfg.runners[modName] = val.(Runner)
+ }
+
+ for name, r := range cfg.runners {
+ err := r.Run()
+ if err != nil {
+ return fmt.Errorf("%s module: %v", name, err)
+ }
+ }
+
+ return nil
+}
+
+// Runner is a thing that Caddy runs.
+type Runner interface {
+ Run() error
+}
+
+// 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.
+func (d *Duration) UnmarshalJSON(b []byte) (err error) {
+ dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
+ cd := Duration(dd)
+ d = &cd
+ return
+}
+
+// MarshalJSON satisfies json.Marshaler.
+func (d Duration) MarshalJSON() (b []byte, err error) {
+ return []byte(fmt.Sprintf(`"%s"`, time.Duration(d).String())), nil
+}