summaryrefslogtreecommitdiff
path: root/admin.go
blob: 06727458f8ae1b9480d6a479e4490d2402bf467b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package caddy2

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net"
	"net/http"
	"strings"
	"sync"
)

var (
	cfgEndptSrv   *http.Server
	cfgEndptSrvMu sync.Mutex
)

// Start starts Caddy's administration endpoint.
func Start(addr string) error {
	cfgEndptSrvMu.Lock()
	defer cfgEndptSrvMu.Unlock()

	ln, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/load", handleLoadConfig)

	for _, m := range GetModules("admin") {
		moduleValue, err := m.New()
		if err != nil {
			return fmt.Errorf("initializing module '%s': %v", m.Name, err)
		}
		route := moduleValue.(AdminRoute)
		mux.Handle(route.Pattern, route)
	}

	cfgEndptSrv = &http.Server{
		Handler: mux,
	}

	go cfgEndptSrv.Serve(ln)

	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 {
	cfgEndptSrvMu.Lock()
	defer cfgEndptSrvMu.Unlock()

	if cfgEndptSrv == nil {
		return fmt.Errorf("no server")
	}

	err := cfgEndptSrv.Shutdown(context.Background()) // TODO
	if err != nil {
		return fmt.Errorf("shutting down server: %v", err)
	}

	cfgEndptSrv = nil

	return nil
}

func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
		return
	}

	if !strings.Contains(r.Header.Get("Content-Type"), "/json") {
		http.Error(w, "unacceptable Content-Type", http.StatusBadRequest)
		return
	}

	err := Load(r.Body)
	if err != nil {
		log.Printf("[ADMIN][ERROR] loading config: %v", err)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
}

// Load loads a configuration.
func Load(r io.Reader) error {
	gc := globalConfig{modules: make(map[string]interface{})}
	err := json.NewDecoder(r).Decode(&gc)
	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
		}
	}

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