Skip to content

Commit 4b5d84d

Browse files
committed
break down response into function
Signed-off-by: Jordan Dubrick <[email protected]>
1 parent 8bc2885 commit 4b5d84d

File tree

1 file changed

+36
-90
lines changed

1 file changed

+36
-90
lines changed

index/server/pkg/server/endpoint.go

Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -57,65 +57,47 @@ func (*Server) ServeRootEndpoint(c *gin.Context) {
5757
}
5858

5959
func (*Server) PostRootEndpoint(c *gin.Context) {
60-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
61-
Message: "Only GET requests are supported.",
62-
})
60+
SetMethodNotAllowedJSONResponse(c)
6361
}
6462

6563
func (*Server) PutRootEndpoint(c *gin.Context) {
66-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
67-
Message: "Only GET requests are supported.",
68-
})
64+
SetMethodNotAllowedJSONResponse(c)
6965
}
7066

7167
func (*Server) DeleteRootEndpoint(c *gin.Context) {
72-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
73-
Message: "Only GET requests are supported.",
74-
})
68+
SetMethodNotAllowedJSONResponse(c)
7569
}
7670

7771
func (*Server) ServeDevfileIndexV1(c *gin.Context, params ServeDevfileIndexV1Params) {
7872
ServeDevfileIndex(c, true, IndexParams(params))
7973
}
8074

8175
func (*Server) PostDevfileIndexV1(c *gin.Context) {
82-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
83-
Message: "Only GET requests are supported.",
84-
})
76+
SetMethodNotAllowedJSONResponse(c)
8577
}
8678

8779
func (*Server) PutDevfileIndexV1(c *gin.Context) {
88-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
89-
Message: "Only GET requests are supported.",
90-
})
80+
SetMethodNotAllowedJSONResponse(c)
9181
}
9282

9383
func (*Server) DeleteDevfileIndexV1(c *gin.Context) {
94-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
95-
Message: "Only GET requests are supported.",
96-
})
84+
SetMethodNotAllowedJSONResponse(c)
9785
}
9886

9987
func (*Server) ServeDevfileIndexV2(c *gin.Context, params ServeDevfileIndexV2Params) {
10088
ServeDevfileIndex(c, false, IndexParams(params))
10189
}
10290

10391
func (*Server) PostDevfileIndexV2(c *gin.Context) {
104-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
105-
Message: "Only GET requests are supported.",
106-
})
92+
SetMethodNotAllowedJSONResponse(c)
10793
}
10894

10995
func (*Server) PutDevfileIndexV2(c *gin.Context) {
110-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
111-
Message: "Only GET requests are supported.",
112-
})
96+
SetMethodNotAllowedJSONResponse(c)
11397
}
11498

11599
func (*Server) DeleteDevfileIndexV2(c *gin.Context) {
116-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
117-
Message: "Only GET requests are supported.",
118-
})
100+
SetMethodNotAllowedJSONResponse(c)
119101
}
120102

121103
// ServeDevfileIndex serves the index.json file located in the container at `ServeDevfileIndex`
@@ -140,21 +122,15 @@ func (*Server) ServeDevfileIndexV1WithType(c *gin.Context, indexType string, par
140122
}
141123

142124
func (*Server) PostDevfileIndexV1WithType(c *gin.Context, indexType string, params PostDevfileIndexV1WithTypeParams){
143-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
144-
Message: "Only GET requests are supported.",
145-
})
125+
SetMethodNotAllowedJSONResponse(c)
146126
}
147127

148128
func (*Server) PutDevfileIndexV1WithType(c *gin.Context, indexType string, params PutDevfileIndexV1WithTypeParams){
149-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
150-
Message: "Only GET requests are supported.",
151-
})
129+
SetMethodNotAllowedJSONResponse(c)
152130
}
153131

154132
func (*Server) DeleteDevfileIndexV1WithType(c *gin.Context, indexType string, params DeleteDevfileIndexV1WithTypeParams){
155-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
156-
Message: "Only GET requests are supported.",
157-
})
133+
SetMethodNotAllowedJSONResponse(c)
158134
}
159135

160136
func (*Server) ServeDevfileIndexV2WithType(c *gin.Context, indexType string, params ServeDevfileIndexV2WithTypeParams) {
@@ -164,21 +140,15 @@ func (*Server) ServeDevfileIndexV2WithType(c *gin.Context, indexType string, par
164140
}
165141

166142
func (*Server) PostDevfileIndexV2WithType(c *gin.Context, indexType string, params PostDevfileIndexV2WithTypeParams) {
167-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
168-
Message: "Only GET requests are supported.",
169-
})
143+
SetMethodNotAllowedJSONResponse(c)
170144
}
171145

172146
func (*Server) PutDevfileIndexV2WithType(c *gin.Context, indexType string, params PutDevfileIndexV2WithTypeParams) {
173-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
174-
Message: "Only GET requests are supported.",
175-
})
147+
SetMethodNotAllowedJSONResponse(c)
176148
}
177149

178150
func (*Server) DeleteDevfileIndexV2WithType(c *gin.Context, indexType string, params DeleteDevfileIndexV2WithTypeParams) {
179-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
180-
Message: "Only GET requests are supported.",
181-
})
151+
SetMethodNotAllowedJSONResponse(c)
182152
}
183153

184154
// ServeHealthCheck serves endpoint `/health` for registry health check with GET request
@@ -189,21 +159,15 @@ func (*Server) ServeHealthCheck(c *gin.Context) {
189159
}
190160
// PostHealthCheck serves endpoint `/health` for registry health check with POST request
191161
func (*Server) PostHealthCheck(c *gin.Context) {
192-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
193-
Message: "Only GET requests are supported.",
194-
})
162+
SetMethodNotAllowedJSONResponse(c)
195163
}
196164
// PutHealthCheck serves endpoint `/health` for registry health check with PUT request
197165
func (*Server) PutHealthCheck(c *gin.Context) {
198-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
199-
Message: "Only GET requests are supported.",
200-
})
166+
SetMethodNotAllowedJSONResponse(c)
201167
}
202168
// DeleteHealthCheck serves endpoint `/health` for registry health check with DELETE request
203169
func (*Server) DeleteHealthCheck(c *gin.Context) {
204-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
205-
Message: "Only GET requests are supported.",
206-
})
170+
SetMethodNotAllowedJSONResponse(c)
207171
}
208172

209173
func (*Server) ServeDevfileWithVersion(c *gin.Context, name string, version string) {
@@ -235,21 +199,15 @@ func (*Server) ServeDevfileWithVersion(c *gin.Context, name string, version stri
235199
}
236200

237201
func (*Server) PostDevfileWithVersion(c *gin.Context, name string, version string){
238-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
239-
Message: "Only GET requests are supported.",
240-
})
202+
SetMethodNotAllowedJSONResponse(c)
241203
}
242204

243205
func (*Server) PutDevfileWithVersion(c *gin.Context, name string, version string){
244-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
245-
Message: "Only GET requests are supported.",
246-
})
206+
SetMethodNotAllowedJSONResponse(c)
247207
}
248208

249209
func (*Server) DeleteDevfileWithVersion(c *gin.Context, name string, version string){
250-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
251-
Message: "Only GET requests are supported.",
252-
})
210+
SetMethodNotAllowedJSONResponse(c)
253211
}
254212

255213
// ServeDevfile returns the devfile content
@@ -259,21 +217,15 @@ func (s *Server) ServeDevfile(c *gin.Context, name string) {
259217
}
260218

261219
func (s *Server) PostDevfile(c *gin.Context, name string) {
262-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
263-
Message: "Only GET requests are supported.",
264-
})
220+
SetMethodNotAllowedJSONResponse(c)
265221
}
266222

267223
func (s *Server) PutDevfile(c *gin.Context, name string) {
268-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
269-
Message: "Only GET requests are supported.",
270-
})
224+
SetMethodNotAllowedJSONResponse(c)
271225
}
272226

273227
func (s *Server) DeleteDevfile(c *gin.Context, name string) {
274-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
275-
Message: "Only GET requests are supported.",
276-
})
228+
SetMethodNotAllowedJSONResponse(c)
277229
}
278230

279231
// ServeDevfileStarterProject returns the starter project content for the devfile using default version
@@ -282,21 +234,15 @@ func (s *Server) ServeDevfileStarterProject(c *gin.Context, name string, starter
282234
}
283235

284236
func (s *Server) PostDevfileStarterProject(c *gin.Context, name string, starterProject string) {
285-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
286-
Message: "Only GET requests are supported.",
287-
})
237+
SetMethodNotAllowedJSONResponse(c)
288238
}
289239

290240
func (s *Server) PutDevfileStarterProject(c *gin.Context, name string, starterProject string) {
291-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
292-
Message: "Only GET requests are supported.",
293-
})
241+
SetMethodNotAllowedJSONResponse(c)
294242
}
295243

296244
func (s *Server) DeleteDevfileStarterProject(c *gin.Context, name string, starterProject string) {
297-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
298-
Message: "Only GET requests are supported.",
299-
})
245+
SetMethodNotAllowedJSONResponse(c)
300246
}
301247

302248
// ServeDevfileStarterProject returns the starter project content for the devfile using specified version
@@ -486,21 +432,15 @@ func (*Server) ServeDevfileStarterProjectWithVersion(c *gin.Context, name string
486432
}
487433

488434
func (*Server) PostDevfileStarterProjectWithVersion(c *gin.Context, name string, version string, starterProject string){
489-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
490-
Message: "Only GET requests are supported.",
491-
})
435+
SetMethodNotAllowedJSONResponse(c)
492436
}
493437

494438
func (*Server) PutDevfileStarterProjectWithVersion(c *gin.Context, name string, version string, starterProject string){
495-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
496-
Message: "Only GET requests are supported.",
497-
})
439+
SetMethodNotAllowedJSONResponse(c)
498440
}
499441

500442
func (*Server) DeleteDevfileStarterProjectWithVersion(c *gin.Context, name string, version string, starterProject string){
501-
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
502-
Message: "Only GET requests are supported.",
503-
})
443+
SetMethodNotAllowedJSONResponse(c)
504444
}
505445

506446
// ServeUI handles registry viewer proxy requests
@@ -862,3 +802,9 @@ func ServeOciProxy(c *gin.Context) {
862802

863803
proxy.ServeHTTP(c.Writer, c.Request)
864804
}
805+
806+
func SetMethodNotAllowedJSONResponse(c *gin.Context){
807+
c.JSON(http.StatusMethodNotAllowed, MethodNotAllowedResponse{
808+
Message: "Only GET requests are supported.",
809+
})
810+
}

0 commit comments

Comments
 (0)