-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
115 lines (90 loc) · 2.62 KB
/
app.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
package controllers
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/unrolled/render"
"github.com/spesnova/iruka/registry"
"github.com/spesnova/iruka/schema"
)
type AppController struct {
*registry.Registry
*render.Render
}
func NewAppController(reg *registry.Registry, ren *render.Render) AppController {
return AppController{reg, ren}
}
func (c *AppController) Create(rw http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var opts schema.AppCreateOpts
err := json.NewDecoder(r.Body).Decode(&opts)
if err != nil {
// TODO (spesnova): response better error
c.JSON(rw, http.StatusInternalServerError, "error")
return
}
app, err := c.CreateApp(opts)
if err != nil {
// TODO (spesnova): if the reqeust is invalid, server should returns 400 instead of 500
//c.JSON(rw, http.StatusBadRequest, "error")
// TODO (spesnova): response better error
c.JSON(rw, http.StatusInternalServerError, "error")
return
}
c.JSON(rw, http.StatusCreated, app)
}
func (c *AppController) Delete(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
identity := vars["identity"]
app, err := c.DestroyApp(identity)
if err != nil {
// TODO (spesnova): separate 404 and 500 error
c.JSON(rw, http.StatusInternalServerError, err.Error())
return
}
c.JSON(rw, http.StatusAccepted, app)
}
func (c *AppController) Info(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
identity := vars["identity"]
app, err := c.App(identity)
if err != nil {
// TODO (spesnova): separate 404 and 500 error
c.JSON(rw, http.StatusInternalServerError, err.Error())
return
}
c.JSON(rw, http.StatusOK, app)
}
func (c *AppController) List(rw http.ResponseWriter, r *http.Request) {
apps, err := c.Apps()
if err != nil {
c.JSON(rw, http.StatusInternalServerError, "error")
return
}
if len(apps) == 0 {
c.JSON(rw, http.StatusOK, []schema.App{})
return
}
c.JSON(rw, http.StatusOK, apps)
}
func (c *AppController) Update(rw http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
vars := mux.Vars(r)
identity := vars["identity"]
var opts schema.AppUpdateOpts
err := json.NewDecoder(r.Body).Decode(&opts)
if err != nil {
// TODO (spesnova): response better error
c.JSON(rw, http.StatusInternalServerError, "error")
return
}
app, err := c.UpdateApp(identity, opts)
if err != nil {
// TODO (spesnova): if the reqeust is invalid, server should returns 400 instead of 500
//c.JSON(rw, http.StatusBadRequest, "error")
// TODO (spesnova): response better error
c.JSON(rw, http.StatusInternalServerError, err.Error())
return
}
c.JSON(rw, http.StatusAccepted, app)
}