-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhttp.go
More file actions
114 lines (110 loc) · 2.71 KB
/
http.go
File metadata and controls
114 lines (110 loc) · 2.71 KB
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
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"strings"
)
func runHttp(config *Config) {
http.HandleFunc("/v1/render", func(w http.ResponseWriter, req *http.Request) {
log.Println(req.Method, req.RequestURI)
switch req.Method {
case "GET":
w.Write(config.LastRender())
case "POST":
body, err := ioutil.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad request: "+err.Error())
return
}
newconfig := config.Copy()
err = newconfig.Load(body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad request: "+err.Error())
return
}
err = newconfig.Validate()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
if e, ok := err.(*ExecError); ok {
io.WriteString(w, e.Output)
io.WriteString(w, e.Input)
} else {
io.WriteString(w, err.Error())
}
return
}
w.Write(newconfig.LastRender())
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
http.HandleFunc("/v1/config/", func(w http.ResponseWriter, req *http.Request) {
log.Println(req.Method, req.RequestURI)
path := strings.TrimPrefix(req.RequestURI, "/v1/config")
handleMutateError := func(err error) {
if err != nil {
log.Println("mutate:", err)
w.WriteHeader(http.StatusBadRequest)
if e, ok := err.(*ExecError); ok {
io.WriteString(w, e.Output)
} else {
io.WriteString(w, err.Error())
}
}
}
readJsonBody := func() interface{} {
var json interface{}
if err := unmarshal(req.Body, &json); err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad request: "+err.Error())
return nil
}
return json
}
switch req.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.Write(append(marshal(config.Get(path)), '\n'))
case "POST":
if !config.Tree().IsComposite(path) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
json := readJsonBody()
if json == nil {
return
}
obj, isObj := json.(map[string]interface{})
err := config.Mutate(func(c *JsonTree) bool {
if c.IsObject(path) && isObj {
return c.Merge(path, obj)
} else {
return c.Append(path, json)
}
})
handleMutateError(err)
case "PUT":
json := readJsonBody()
if json == nil {
return
}
err := config.Mutate(func(c *JsonTree) bool {
return c.Replace(path, json)
})
handleMutateError(err)
case "DELETE":
err := config.Mutate(func(c *JsonTree) bool {
return c.Delete(path)
})
handleMutateError(err)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
log.Println("Listening on port " + *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}