-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
198 lines (157 loc) · 5.06 KB
/
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package registry
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/justinas/alice"
)
// NewDefaultRouter sets up a mux.Router with the registry routes
func NewDefaultRouter(registry Registry) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
middleware := alice.New(recoverHandler, logger)
index := middleware.ThenFunc(registry.Index)
templateIndex := middleware.ThenFunc(registry.TemplateIndex)
templateShow := middleware.ThenFunc(registry.TemplateShow)
authed := middleware.Append(registry.AuthStore.Authorize)
templateCreate := authed.ThenFunc(registry.TemplateCreate)
templateUpdate := authed.ThenFunc(registry.TemplateUpdate)
router.Methods("GET").Path("/").Handler(index)
router.Methods("GET").Path("/templates").Handler(templateIndex)
router.Methods("POST").Path("/templates").Handler(templateCreate) // templateCreate)
router.Methods("GET").Path("/templates/{templateName}").Handler(templateShow)
router.Methods("PUT").Path("/templates/{templateName}").Handler(templateUpdate)
return router
}
var common alice.Chain
func init() {
common = alice.New(contentTypeJSON)
}
// Index lists available resources at this endpoint
func (registry Registry) Index(w http.ResponseWriter, r *http.Request) {
common.ThenFunc(registry.index).ServeHTTP(w, r)
}
func (registry Registry) index(w http.ResponseWriter, r *http.Request) {
// TODO: Navigable API from the root
fmt.Fprintln(w, "{\"status\": \"Binder Registry Live!\"}")
}
// TemplateIndex lists the available templates as well as their configuration
func (registry Registry) TemplateIndex(w http.ResponseWriter, r *http.Request) {
common.ThenFunc(registry.templateIndex).ServeHTTP(w, r)
}
func (registry Registry) templateIndex(w http.ResponseWriter, r *http.Request) {
templates, err := registry.ListTemplates()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode(UnableToListError); err != nil {
panic(err)
}
}
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(templates); err != nil {
panic(err)
}
}
// TemplateShow displays the requested template
func (registry Registry) TemplateShow(w http.ResponseWriter, r *http.Request) {
common.ThenFunc(registry.templateShow).ServeHTTP(w, r)
}
func (registry Registry) templateShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
templateName := vars["templateName"]
tmpl, err := registry.GetTemplate(templateName)
if err != nil {
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(TemplateNotFoundError); err != nil {
panic(err)
}
return
}
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(tmpl); err != nil {
panic(err)
}
}
// TemplateCreate registers a template
func (registry Registry) TemplateCreate(w http.ResponseWriter, r *http.Request) {
common.ThenFunc(registry.templateCreate).ServeHTTP(w, r)
}
func (registry Registry) templateCreate(w http.ResponseWriter, r *http.Request) {
limitedReader := io.LimitReader(r.Body, 1048576)
body, err := ioutil.ReadAll(limitedReader)
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
var tmpl Template
if err := json.Unmarshal(body, &tmpl); err != nil {
w.WriteHeader(http.StatusBadRequest)
userErr := APIErrorResponse{Message: err.Error()}
if err := json.NewEncoder(w).Encode(userErr); err != nil {
panic(err)
}
return
}
if tmpl.Name == "" || tmpl.ImageName == "" {
w.WriteHeader(http.StatusBadRequest)
userErr := APIErrorResponse{Message: "name and image-name must be specified"}
if err := json.NewEncoder(w).Encode(userErr); err != nil {
panic(err)
}
return
}
t, err := registry.RegisterTemplate(tmpl)
if err != nil {
w.WriteHeader(http.StatusConflict)
userErr := APIErrorResponse{Message: err.Error()}
if err := json.NewEncoder(w).Encode(userErr); err != nil {
panic(err)
}
return
}
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(t); err != nil {
panic(err)
}
}
// TemplateUpdate updates an individual template by name
func (registry Registry) TemplateUpdate(w http.ResponseWriter, r *http.Request) {
common.ThenFunc(registry.templateUpdate).ServeHTTP(w, r)
}
func (registry Registry) templateUpdate(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
var tmpl Template
if err := json.Unmarshal(body, &tmpl); err != nil {
w.WriteHeader(http.StatusBadRequest)
userErr := APIErrorResponse{Message: err.Error()}
if err := json.NewEncoder(w).Encode(userErr); err != nil {
panic(err)
}
return
}
vars := mux.Vars(r)
templateName := vars["templateName"]
tmpl.Name = templateName
tmpl, err = registry.UpdateTemplate(tmpl)
if err != nil {
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(TemplateNotFoundError); err != nil {
panic(err)
}
return
}
w.WriteHeader(http.StatusAccepted)
if err := json.NewEncoder(w).Encode(tmpl); err != nil {
panic(err)
}
}