/
app_controller.go
192 lines (143 loc) · 3.94 KB
/
app_controller.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
package controller
import (
"fmt"
"net/http"
"os"
"github.com/dtan4/paus-frontend/config"
"github.com/dtan4/paus-frontend/model/app"
"github.com/dtan4/paus-frontend/model/arg"
"github.com/dtan4/paus-frontend/model/env"
"github.com/dtan4/paus-frontend/model/healthcheck"
"github.com/dtan4/paus-frontend/model/user"
"github.com/dtan4/paus-frontend/store"
"github.com/gin-gonic/gin"
)
type AppController struct {
*ApplicationController
}
func NewAppController(config *config.Config, etcd *store.Etcd) *AppController {
return &AppController{NewApplicationController(config, etcd)}
}
func (self *AppController) Index(c *gin.Context) {
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
if !user.Exists(self.etcd, username) {
c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
"error": true,
"message": fmt.Sprintf("User %s does not exist.", username),
})
return
}
apps, err := app.List(self.etcd, username)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "apps.tmpl", gin.H{
"error": true,
"message": "Failed to list apps.",
})
return
}
c.HTML(http.StatusOK, "apps.tmpl", gin.H{
"error": false,
"apps": apps,
"avater_url": user.GetAvaterURL(self.etcd, username),
"logged_in": true,
"username": username,
})
}
func (self *AppController) Get(c *gin.Context) {
var latestURL string
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
if !user.Exists(self.etcd, username) {
c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
"error": true,
"message": fmt.Sprintf("User %s does not exist.", username),
})
return
}
appName := c.Param("appName")
if !app.Exists(self.etcd, username, appName) {
c.HTML(http.StatusNotFound, "apps.tmpl", gin.H{
"error": true,
"message": fmt.Sprintf("Application %s does not exist.", appName),
})
return
}
urls, err := app.URLs(self.etcd, self.config.URIScheme, self.config.BaseDomain, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"error": true,
"message": "Failed to list app URLs.",
})
return
}
envs, err := env.List(self.etcd, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"error": true,
"message": "Failed to list environment variables.",
})
return
}
buildArgs, err := arg.List(self.etcd, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"error": true,
"message": "Failed to list build args.",
})
return
}
hc, err := healthcheck.Get(self.etcd, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"error": true,
"message": "Failed to get healthcheck.",
})
return
}
if len(urls) > 0 {
latestURL = app.LatestAppURLOfUser(self.config.URIScheme, self.config.BaseDomain, username, appName)
}
c.HTML(http.StatusOK, "app.tmpl", gin.H{
"error": false,
"app": appName,
"avater_url": user.GetAvaterURL(self.etcd, username),
"buildArgs": buildArgs,
"envs": envs,
"healthcheck": hc,
"latestURL": latestURL,
"logged_in": true,
"urls": urls,
"username": username,
})
}
func (self *AppController) New(c *gin.Context) {
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
appName := c.PostForm("appName")
err := app.Create(self.etcd, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "apps.tmpl", gin.H{
"alert": true,
"error": true,
"message": "Failed to create app.",
})
return
}
c.Redirect(http.StatusSeeOther, "/apps/"+appName)
}