summaryrefslogtreecommitdiff
path: root/admin.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 /admin.go
parent859b5d7ea3b8f660ac68d9aea5a53d25a9a7422c (diff)
Rudimentary start of HTTP servers
Diffstat (limited to 'admin.go')
-rw-r--r--admin.go55
1 files changed, 16 insertions, 39 deletions
diff --git a/admin.go b/admin.go
index 0672745..2841ed8 100644
--- a/admin.go
+++ b/admin.go
@@ -17,8 +17,8 @@ var (
cfgEndptSrvMu sync.Mutex
)
-// Start starts Caddy's administration endpoint.
-func Start(addr string) error {
+// StartAdmin starts Caddy's administration endpoint.
+func StartAdmin(addr string) error {
cfgEndptSrvMu.Lock()
defer cfgEndptSrvMu.Unlock()
@@ -48,14 +48,8 @@ func Start(addr string) error {
return nil
}
-// AdminRoute represents a route for the admin endpoint.
-type AdminRoute struct {
- http.Handler
- Pattern string
-}
-
-// Stop stops the API endpoint.
-func Stop() error {
+// StopAdmin stops the API endpoint.
+func StopAdmin() error {
cfgEndptSrvMu.Lock()
defer cfgEndptSrvMu.Unlock()
@@ -73,6 +67,12 @@ func Stop() error {
return nil
}
+// AdminRoute represents a route for the admin endpoint.
+type AdminRoute struct {
+ http.Handler
+ Pattern string
+}
+
func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
@@ -92,39 +92,16 @@ func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
}
}
-// Load loads a configuration.
+// Load loads and starts a configuration.
func Load(r io.Reader) error {
- gc := globalConfig{modules: make(map[string]interface{})}
- err := json.NewDecoder(r).Decode(&gc)
+ var cfg Config
+ err := json.NewDecoder(r).Decode(&cfg)
if err != nil {
return fmt.Errorf("decoding config: %v", err)
}
-
- for modName, rawMsg := range gc.Modules {
- mod, ok := modules[modName]
- if !ok {
- return fmt.Errorf("unrecognized module: %s", modName)
- }
-
- if mod.New != nil {
- val, err := mod.New()
- if err != nil {
- return fmt.Errorf("initializing module '%s': %v", modName, err)
- }
- err = json.Unmarshal(rawMsg, &val)
- if err != nil {
- return fmt.Errorf("decoding module config: %s: %v", modName, err)
- }
- gc.modules[modName] = val
- }
+ err = Start(cfg)
+ if err != nil {
+ return fmt.Errorf("starting: %v", err)
}
-
return nil
}
-
-type globalConfig struct {
- TestVal string `json:"testval"`
- Modules map[string]json.RawMessage `json:"modules"`
- TestArr []string `json:"test_arr"`
- modules map[string]interface{}
-}