-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
93 lines (88 loc) · 2.81 KB
/
config.go
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
package api
import (
"context"
"encoding/json"
"errors"
"net/http"
"github.com/micromdm/nanodep/client"
"github.com/micromdm/nanodep/log"
"github.com/micromdm/nanodep/log/ctxlog"
)
// RetrieveConfigHandler returns the DEP server config for the DEP
// name in the path.
//
// Note the whole URL path is used as the DEP name. This necessitates
// stripping the URL prefix before using this handler. Also note we expose Go
// errors to the output as this is meant for "API" users.
func RetrieveConfigHandler(store client.ConfigRetriever, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
if r.URL.Path == "" {
logger.Info("msg", "DEP name check", "err", "missing DEP name")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
logger = logger.With("name", r.URL.Path)
config, err := store.RetrieveConfig(r.Context(), r.URL.Path)
if err != nil {
logger.Info("msg", "retrieving config", "err", err)
jsonError(w, err)
return
}
w.Header().Set("Content-type", "application/json")
err = json.NewEncoder(w).Encode(config)
if err != nil {
logger.Info("msg", "encoding response body", "err", err)
return
}
}
}
type ConfigStorer interface {
StoreConfig(ctx context.Context, name string, config *client.Config) error
}
// StoreConfigHandler stores the DEP server config for the DEP
// name in the path.
//
// Note the whole URL path is used as the DEP name. This necessitates
// stripping the URL prefix before using this handler. Also note we expose Go
// errors to the output as this is meant for "API" users.
func StoreConfigHandler(store ConfigStorer, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
if r.URL.Path == "" {
logger.Info("msg", "DEP name check", "err", "missing DEP name")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
logger = logger.With("name", r.URL.Path)
config := new(client.Config)
err := json.NewDecoder(r.Body).Decode(config)
if err != nil {
logger.Info("msg", "decoding request body", "err", err)
jsonError(w, err)
return
}
defer r.Body.Close()
if config.BaseURL == "" {
err = errors.New("empty base URL")
}
if err != nil {
logger.Info("msg", "decoded config", "err", err)
jsonError(w, err)
return
}
err = store.StoreConfig(r.Context(), r.URL.Path, config)
if err != nil {
logger.Info("msg", "storing config", "err", err)
jsonError(w, err)
return
}
logger.Debug("msg", "stored config")
w.Header().Set("Content-type", "application/json")
err = json.NewEncoder(w).Encode(config)
if err != nil {
logger.Info("msg", "encoding response body", "err", err)
return
}
}
}