forked from PearsonEducation/Thalassa-Conduit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend_handlers.go
126 lines (112 loc) · 3.03 KB
/
frontend_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
// GetFrontends returns a list of HAProxy frontends.
func GetFrontends(w http.ResponseWriter, enc Encoder, svc DataSvc) {
f, err := svc.GetAllFrontends()
if err != nil {
panic(err)
}
util{}.writeResponse(w, http.StatusOK, enc.EncodeMulti(f.ToInterfaces()...))
}
// GetFrontend returns the requested HAProxy frontend.
func GetFrontend(w http.ResponseWriter, enc Encoder, svc DataSvc, params Params) {
data, err := svc.GetFrontend(params["name"])
if err != nil {
panic(err)
}
if data == nil {
util{}.notFound(w, enc, fmt.Sprintf("the frontend with name %s does not exist", params["name"]))
return
}
util{}.writeResponse(w, http.StatusOK, enc.Encode(data))
}
// PutFrontend creates or updates an HAProxy frontend.
func PutFrontend(w http.ResponseWriter, r *http.Request, enc Encoder, svc DataSvc, params Params) {
f := &Frontend{}
e := loadFrontendFromRequest(r, enc, f)
if e != nil {
util{}.badRequest(w, enc, "the frontend data is invalid")
return
}
// always use the name identified in the resource URL
f.Name = params["name"]
existing, err := svc.GetFrontend(f.Name)
if err != nil {
panic(err)
}
status := http.StatusOK
if existing == nil {
status = http.StatusCreated
}
err = svc.SaveFrontend(f)
if err != nil {
switch err.Type {
case ErrBadData:
util{}.badRequest(w, enc, err.Error())
return
default:
panic(err)
}
}
util{}.writeResponse(w, status, enc.Encode(f))
}
// PostFrontend performs a partial update of an existing HAProxy frontend.
func PostFrontend(w http.ResponseWriter, r *http.Request, enc Encoder, svc DataSvc, params Params) {
name := params["name"]
f, err := svc.GetFrontend(name)
if err != nil {
panic(err)
}
if f == nil {
util{}.notFound(w, enc, fmt.Sprintf("the frontend with name %s does not exist", name))
return
}
e := loadFrontendFromRequest(r, enc, f)
if e != nil {
util{}.badRequest(w, enc, "the frontend data is invalid")
return
}
err = svc.SaveFrontend(f)
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(f))
}
// DeleteFrontend removes an HAProxy frontend.
func DeleteFrontend(w http.ResponseWriter, enc Encoder, svc DataSvc, params Params) {
key := params["name"]
err := svc.DeleteFrontend(key)
if err != nil {
switch err.Type {
case ErrNotFound:
util{}.notFound(w, enc, fmt.Sprintf("the frontend with name %s does not exist", key))
return
default:
panic(err)
}
}
util{}.writeResponse(w, http.StatusNoContent, "")
}
// parse request body into a Frontend instance
func loadFrontendFromRequest(r *http.Request, enc Encoder, f *Frontend) *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, f)
if err != nil {
return NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("the frontend data is not valid"))
}
return nil
}