-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend_handlers.go
139 lines (124 loc) · 3.43 KB
/
backend_handlers.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
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
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
// GetBackends returns a list of HAProxy backends.
func GetBackends(w http.ResponseWriter, enc Encoder, svc DataSvc) {
b, err := svc.GetAllBackends()
if err != nil {
panic(err)
}
util{}.writeResponse(w, http.StatusOK, enc.EncodeMulti(b.ToInterfaces()...))
}
// GetBackend returns the requested HAProxy backend.
func GetBackend(w http.ResponseWriter, enc Encoder, svc DataSvc, params Params) {
data, err := svc.GetBackend(params["name"])
if err != nil {
panic(err)
}
if data == nil {
util{}.notFound(w, enc, fmt.Sprintf("the backend with name %s does not exist", params["name"]))
return
}
util{}.writeResponse(w, http.StatusOK, enc.Encode(data))
}
// PutBackend creates or updates an HAProxy backend.
func PutBackend(w http.ResponseWriter, r *http.Request, enc Encoder, svc DataSvc, params Params) {
b := &Backend{}
e := loadBackendFromRequest(r, enc, b)
if e != nil {
util{}.badRequest(w, enc, "the backend data is invalid")
return
}
// always use the name identified in the resource
b.Name = params["name"]
existing, err := svc.GetBackend(b.Name)
if err != nil {
panic(err)
}
status := http.StatusOK
if existing == nil {
status = http.StatusCreated
}
err = svc.SaveBackend(b)
if err != nil {
switch err.Type {
case ErrBadData:
util{}.badRequest(w, enc, err.Error())
return
default:
panic(err)
}
}
util{}.writeResponse(w, status, enc.Encode(b))
}
// PostBackend performs a partial update of an existing HAProxy backend.
func PostBackend(w http.ResponseWriter, r *http.Request, enc Encoder, svc DataSvc, params Params) {
name := params["name"]
b, err := svc.GetBackend(name)
if err != nil {
panic(err)
}
if b == nil {
util{}.notFound(w, enc, fmt.Sprintf("the backend with name %s does not exist", name))
return
}
e := loadBackendFromRequest(r, enc, b)
if e != nil {
util{}.badRequest(w, enc, "the backend data is invalid")
return
}
err = svc.SaveBackend(b)
if err != nil {
switch err.Type {
case ErrBadData:
util{}.badRequest(w, enc, err.Error())
return
default:
panic(err)
}
}
util{}.writeResponse(w, http.StatusOK, enc.Encode(b))
}
// DeleteBackend removes an HAProxy backend.
func DeleteBackend(w http.ResponseWriter, enc Encoder, svc DataSvc, params Params) {
key := params["name"]
err := svc.DeleteBackend(key)
if err != nil {
switch err.Type {
case ErrNotFound:
util{}.notFound(w, enc, fmt.Sprintf("the backend with name %s does not exist", key))
return
default:
panic(err)
}
}
util{}.writeResponse(w, http.StatusNoContent, "")
}
// GetBackendMembers returns a list of all members in a backend.
func GetBackendMembers(w http.ResponseWriter, enc Encoder, svc DataSvc, params Params) {
b, err := svc.GetBackend(params["name"])
if err != nil {
panic(err)
}
if b == nil {
util{}.notFound(w, enc, fmt.Sprintf("the backend with name %s does not exist", params["name"]))
return
}
util{}.writeResponse(w, http.StatusOK, enc.EncodeMulti(b.Members.ToInterfaces()...))
}
// parse request body into a Backend instance
func loadBackendFromRequest(r *http.Request, enc Encoder, b *Backend) *ErrorResponse {
//TODO: Don't use ReadAll()... reading a terabyte of data in one go would be bad
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
err = enc.Decode(body, b)
if err != nil {
return NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("the backend data is not valid"))
}
return nil
}