blob: a4430b85296acd8dcbb0cc28868799ccecf2feb1 [file] [log] [blame]
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04001package welcome
2
3import (
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +04004 "context"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04005 "embed"
6 "encoding/json"
7 "fmt"
8 "html/template"
9 "io/ioutil"
10 "log"
11 "net/http"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040012 "time"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040013
14 "github.com/Masterminds/sprig/v3"
gioaa0fcdb2024-06-10 22:19:25 +040015 "github.com/gorilla/mux"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040016
17 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040018 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040019)
20
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040021//go:embed appmanager-tmpl/*
22var appTmpls embed.FS
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040023
24type AppManagerServer struct {
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040025 port int
26 m *installer.AppManager
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040027 r installer.AppRepository
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040028 reconciler tasks.Reconciler
gio778577f2024-04-29 09:44:38 +040029 h installer.HelmReleaseMonitor
30 tasks map[string]tasks.Task
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040031 tmpl tmplts
32}
33
34type tmplts struct {
35 index *template.Template
36 app *template.Template
37}
38
39func parseTemplatesAppManager(fs embed.FS) (tmplts, error) {
40 base, err := template.New("base.html").Funcs(template.FuncMap(sprig.FuncMap())).ParseFS(fs, "appmanager-tmpl/base.html")
41 if err != nil {
42 return tmplts{}, err
43 }
44 parse := func(path string) (*template.Template, error) {
45 if b, err := base.Clone(); err != nil {
46 return nil, err
47 } else {
48 return b.ParseFS(fs, path)
49 }
50 }
51 index, err := parse("appmanager-tmpl/index.html")
52 if err != nil {
53 return tmplts{}, err
54 }
55 app, err := parse("appmanager-tmpl/app.html")
56 if err != nil {
57 return tmplts{}, err
58 }
59 return tmplts{index, app}, nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040060}
61
62func NewAppManagerServer(
63 port int,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040064 m *installer.AppManager,
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +040065 r installer.AppRepository,
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +040066 reconciler tasks.Reconciler,
gio778577f2024-04-29 09:44:38 +040067 h installer.HelmReleaseMonitor,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040068) (*AppManagerServer, error) {
69 tmpl, err := parseTemplatesAppManager(appTmpls)
70 if err != nil {
71 return nil, err
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040072 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +040073 return &AppManagerServer{
74 port: port,
75 m: m,
76 r: r,
77 reconciler: reconciler,
78 h: h,
79 tasks: make(map[string]tasks.Task),
80 tmpl: tmpl,
81 }, nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040082}
83
gio09f8efa2024-06-10 22:35:24 +040084type cachingHandler struct {
85 h http.Handler
86}
87
88func (h cachingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
89 w.Header().Set("Cache-Control", "max-age=604800")
90 h.h.ServeHTTP(w, r)
91}
92
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +040093func (s *AppManagerServer) Start() error {
gioaa0fcdb2024-06-10 22:19:25 +040094 r := mux.NewRouter()
gio09f8efa2024-06-10 22:35:24 +040095 r.PathPrefix("/static/").Handler(cachingHandler{http.FileServer(http.FS(staticAssets))})
gioaa0fcdb2024-06-10 22:19:25 +040096 r.HandleFunc("/api/app-repo", s.handleAppRepo)
97 r.HandleFunc("/api/app/{slug}/install", s.handleAppInstall).Methods(http.MethodPost)
98 r.HandleFunc("/api/app/{slug}", s.handleApp).Methods(http.MethodGet)
99 r.HandleFunc("/api/instance/{slug}", s.handleInstance).Methods(http.MethodGet)
100 r.HandleFunc("/api/instance/{slug}/update", s.handleAppUpdate).Methods(http.MethodPost)
101 r.HandleFunc("/api/instance/{slug}/remove", s.handleAppRemove).Methods(http.MethodPost)
102 r.HandleFunc("/", s.handleIndex).Methods(http.MethodGet)
103 r.HandleFunc("/not-installed", s.handleNotInstalledApps).Methods(http.MethodGet)
104 r.HandleFunc("/installed", s.handleInstalledApps).Methods(http.MethodGet)
105 r.HandleFunc("/app/{slug}", s.handleAppUI).Methods(http.MethodGet)
106 r.HandleFunc("/instance/{slug}", s.handleInstanceUI).Methods(http.MethodGet)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400107 fmt.Printf("Starting HTTP server on port: %d\n", s.port)
gioaa0fcdb2024-06-10 22:19:25 +0400108 return http.ListenAndServe(fmt.Sprintf(":%d", s.port), r)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400109}
110
111type app struct {
gio3cdee592024-04-17 10:15:56 +0400112 Name string `json:"name"`
113 Icon template.HTML `json:"icon"`
114 ShortDescription string `json:"shortDescription"`
115 Slug string `json:"slug"`
116 Instances []installer.AppInstanceConfig `json:"instances,omitempty"`
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400117}
118
gioaa0fcdb2024-06-10 22:19:25 +0400119func (s *AppManagerServer) handleAppRepo(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400120 all, err := s.r.GetAll()
121 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400122 http.Error(w, err.Error(), http.StatusInternalServerError)
123 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400124 }
125 resp := make([]app, len(all))
126 for i, a := range all {
gio44f621b2024-04-29 09:44:38 +0400127 resp[i] = app{a.Name(), a.Icon(), a.Description(), a.Slug(), nil}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400128 }
gioaa0fcdb2024-06-10 22:19:25 +0400129 w.Header().Set("Content-Type", "application/json")
130 if err := json.NewEncoder(w).Encode(resp); err != nil {
131 http.Error(w, err.Error(), http.StatusInternalServerError)
132 return
133 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400134}
135
gioaa0fcdb2024-06-10 22:19:25 +0400136func (s *AppManagerServer) handleApp(w http.ResponseWriter, r *http.Request) {
137 slug, ok := mux.Vars(r)["slug"]
138 if !ok {
139 http.Error(w, "empty slug", http.StatusBadRequest)
140 return
141 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400142 a, err := s.r.Find(slug)
143 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400144 http.Error(w, err.Error(), http.StatusInternalServerError)
145 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400146 }
gio308105e2024-04-19 13:12:13 +0400147 instances, err := s.m.FindAllAppInstances(slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400148 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400149 http.Error(w, err.Error(), http.StatusInternalServerError)
150 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400151 }
gioaa0fcdb2024-06-10 22:19:25 +0400152 resp := app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances}
153 w.Header().Set("Content-Type", "application/json")
154 if err := json.NewEncoder(w).Encode(resp); err != nil {
155 http.Error(w, err.Error(), http.StatusInternalServerError)
156 return
157 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400158}
159
gioaa0fcdb2024-06-10 22:19:25 +0400160func (s *AppManagerServer) handleInstance(w http.ResponseWriter, r *http.Request) {
161 slug, ok := mux.Vars(r)["slug"]
162 if !ok {
163 http.Error(w, "empty slug", http.StatusBadRequest)
164 return
165 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400166 instance, err := s.m.FindInstance(slug)
167 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400168 http.Error(w, err.Error(), http.StatusInternalServerError)
169 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400170 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400171 a, err := s.r.Find(instance.AppId)
172 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400173 http.Error(w, err.Error(), http.StatusInternalServerError)
174 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400175 }
gioaa0fcdb2024-06-10 22:19:25 +0400176 resp := app{a.Name(), a.Icon(), a.Description(), a.Slug(), []installer.AppInstanceConfig{*instance}}
177 w.Header().Set("Content-Type", "application/json")
178 if err := json.NewEncoder(w).Encode(resp); err != nil {
179 http.Error(w, err.Error(), http.StatusInternalServerError)
180 return
181 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400182}
183
gioaa0fcdb2024-06-10 22:19:25 +0400184func (s *AppManagerServer) handleAppInstall(w http.ResponseWriter, r *http.Request) {
185 slug, ok := mux.Vars(r)["slug"]
186 if !ok {
187 http.Error(w, "empty slug", http.StatusBadRequest)
188 return
189 }
190 contents, err := ioutil.ReadAll(r.Body)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400191 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400192 http.Error(w, err.Error(), http.StatusInternalServerError)
193 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400194 }
195 var values map[string]any
196 if err := json.Unmarshal(contents, &values); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400197 http.Error(w, err.Error(), http.StatusInternalServerError)
198 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400199 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400200 log.Printf("Values: %+v\n", values)
gio3cdee592024-04-17 10:15:56 +0400201 a, err := installer.FindEnvApp(s.r, slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400202 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400203 http.Error(w, err.Error(), http.StatusInternalServerError)
204 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400205 }
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400206 log.Printf("Found application: %s\n", slug)
gio3cdee592024-04-17 10:15:56 +0400207 env, err := s.m.Config()
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400208 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400209 http.Error(w, err.Error(), http.StatusInternalServerError)
210 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400211 }
gio3cdee592024-04-17 10:15:56 +0400212 log.Printf("Configuration: %+v\n", env)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400213 suffixGen := installer.NewFixedLengthRandomSuffixGenerator(3)
gio3af43942024-04-16 08:13:50 +0400214 suffix, err := suffixGen.Generate()
215 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400216 http.Error(w, err.Error(), http.StatusInternalServerError)
217 return
gio3af43942024-04-16 08:13:50 +0400218 }
gio44f621b2024-04-29 09:44:38 +0400219 instanceId := a.Slug() + suffix
gio3cdee592024-04-17 10:15:56 +0400220 appDir := fmt.Sprintf("/apps/%s", instanceId)
221 namespace := fmt.Sprintf("%s%s%s", env.NamespacePrefix, a.Namespace(), suffix)
gio778577f2024-04-29 09:44:38 +0400222 rr, err := s.m.Install(a, instanceId, appDir, namespace, values)
223 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400224 http.Error(w, err.Error(), http.StatusInternalServerError)
225 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400226 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400227 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
228 go s.reconciler.Reconcile(ctx)
gio778577f2024-04-29 09:44:38 +0400229 if _, ok := s.tasks[instanceId]; ok {
230 panic("MUST NOT REACH!")
231 }
232 t := tasks.NewMonitorRelease(s.h, rr)
233 t.OnDone(func(err error) {
234 delete(s.tasks, instanceId)
235 })
236 s.tasks[instanceId] = t
237 go t.Start()
gioaa0fcdb2024-06-10 22:19:25 +0400238 if _, err := fmt.Fprintf(w, "/instance/%s", instanceId); err != nil {
239 http.Error(w, err.Error(), http.StatusInternalServerError)
240 return
241 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400242}
243
gioaa0fcdb2024-06-10 22:19:25 +0400244func (s *AppManagerServer) handleAppUpdate(w http.ResponseWriter, r *http.Request) {
245 slug, ok := mux.Vars(r)["slug"]
246 if !ok {
247 http.Error(w, "empty slug", http.StatusBadRequest)
248 return
249 }
gioaa0fcdb2024-06-10 22:19:25 +0400250 contents, err := ioutil.ReadAll(r.Body)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400251 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400252 http.Error(w, err.Error(), http.StatusInternalServerError)
253 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400254 }
255 var values map[string]any
256 if err := json.Unmarshal(contents, &values); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400257 http.Error(w, err.Error(), http.StatusInternalServerError)
258 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400259 }
gio778577f2024-04-29 09:44:38 +0400260 if _, ok := s.tasks[slug]; ok {
gioaa0fcdb2024-06-10 22:19:25 +0400261 http.Error(w, "Update already in progress", http.StatusBadRequest)
262 return
gio778577f2024-04-29 09:44:38 +0400263 }
giof8843412024-05-22 16:38:05 +0400264 rr, err := s.m.Update(slug, values)
gio778577f2024-04-29 09:44:38 +0400265 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400266 http.Error(w, err.Error(), http.StatusInternalServerError)
267 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400268 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400269 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
270 go s.reconciler.Reconcile(ctx)
gio778577f2024-04-29 09:44:38 +0400271 t := tasks.NewMonitorRelease(s.h, rr)
272 t.OnDone(func(err error) {
273 delete(s.tasks, slug)
274 })
275 s.tasks[slug] = t
276 go t.Start()
gioaa0fcdb2024-06-10 22:19:25 +0400277 if _, err := fmt.Fprintf(w, "/instance/%s", slug); err != nil {
278 http.Error(w, err.Error(), http.StatusInternalServerError)
279 return
280 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400281}
282
gioaa0fcdb2024-06-10 22:19:25 +0400283func (s *AppManagerServer) handleAppRemove(w http.ResponseWriter, r *http.Request) {
284 slug, ok := mux.Vars(r)["slug"]
285 if !ok {
286 http.Error(w, "empty slug", http.StatusBadRequest)
287 return
288 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400289 if err := s.m.Remove(slug); err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400290 http.Error(w, err.Error(), http.StatusInternalServerError)
291 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400292 }
Giorgi Lekveishvilid2f3dca2023-12-20 09:31:30 +0400293 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
294 go s.reconciler.Reconcile(ctx)
gioaa0fcdb2024-06-10 22:19:25 +0400295 if _, err := fmt.Fprint(w, "/"); err != nil {
296 http.Error(w, err.Error(), http.StatusInternalServerError)
297 return
298 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400299}
300
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400301type PageData struct {
302 Apps []app
303 CurrentPage string
304}
305
gioaa0fcdb2024-06-10 22:19:25 +0400306func (s *AppManagerServer) handleIndex(w http.ResponseWriter, r *http.Request) {
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400307 all, err := s.r.GetAll()
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400308 if err != nil {
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400309 log.Printf("all apps: %v", err)
gioaa0fcdb2024-06-10 22:19:25 +0400310 http.Error(w, err.Error(), http.StatusInternalServerError)
311 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400312 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400313 resp := make([]app, 0)
314 for _, a := range all {
315 instances, err := s.m.FindAllAppInstances(a.Slug())
316 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400317 http.Error(w, err.Error(), http.StatusInternalServerError)
318 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400319 }
320 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances})
321 }
322 data := PageData{
323 Apps: resp,
324 CurrentPage: "ALL",
325 }
gioaa0fcdb2024-06-10 22:19:25 +0400326 if err := s.tmpl.index.Execute(w, data); err != nil {
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400327 log.Printf("executing template: %v", err)
gioaa0fcdb2024-06-10 22:19:25 +0400328 http.Error(w, err.Error(), http.StatusInternalServerError)
329 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400330 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400331}
332
gioaa0fcdb2024-06-10 22:19:25 +0400333func (s *AppManagerServer) handleNotInstalledApps(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400334 all, err := s.r.GetAll()
335 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400336 http.Error(w, err.Error(), http.StatusInternalServerError)
337 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400338 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400339 resp := make([]app, 0)
340 for _, a := range all {
341 instances, err := s.m.FindAllAppInstances(a.Slug())
342 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400343 http.Error(w, err.Error(), http.StatusInternalServerError)
344 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400345 }
346 if len(instances) == 0 {
347 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), nil})
348 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400349 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400350 data := PageData{
351 Apps: resp,
352 CurrentPage: "NOT_INSTALLED",
353 }
gioaa0fcdb2024-06-10 22:19:25 +0400354 if err := s.tmpl.index.Execute(w, data); err != nil {
355 http.Error(w, err.Error(), http.StatusInternalServerError)
356 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400357 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400358}
359
gioaa0fcdb2024-06-10 22:19:25 +0400360func (s *AppManagerServer) handleInstalledApps(w http.ResponseWriter, r *http.Request) {
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400361 all, err := s.r.GetAll()
362 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400363 http.Error(w, err.Error(), http.StatusInternalServerError)
364 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400365 }
366 resp := make([]app, 0)
367 for _, a := range all {
368 instances, err := s.m.FindAllAppInstances(a.Slug())
369 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400370 http.Error(w, err.Error(), http.StatusInternalServerError)
371 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400372 }
373 if len(instances) != 0 {
374 resp = append(resp, app{a.Name(), a.Icon(), a.Description(), a.Slug(), instances})
375 }
376 }
377 data := PageData{
378 Apps: resp,
379 CurrentPage: "INSTALLED",
380 }
gioaa0fcdb2024-06-10 22:19:25 +0400381 if err := s.tmpl.index.Execute(w, data); err != nil {
382 http.Error(w, err.Error(), http.StatusInternalServerError)
383 return
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400384 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400385}
386
387type appPageData struct {
gio3cdee592024-04-17 10:15:56 +0400388 App installer.EnvApp
389 Instance *installer.AppInstanceConfig
390 Instances []installer.AppInstanceConfig
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400391 AvailableNetworks []installer.Network
gio778577f2024-04-29 09:44:38 +0400392 Task tasks.Task
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400393 CurrentPage string
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400394}
395
gioaa0fcdb2024-06-10 22:19:25 +0400396func (s *AppManagerServer) handleAppUI(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400397 global, err := s.m.Config()
398 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400399 http.Error(w, err.Error(), http.StatusInternalServerError)
400 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400401 }
gioaa0fcdb2024-06-10 22:19:25 +0400402 slug, ok := mux.Vars(r)["slug"]
403 if !ok {
404 http.Error(w, "empty slug", http.StatusBadRequest)
405 return
406 }
gio3cdee592024-04-17 10:15:56 +0400407 a, err := installer.FindEnvApp(s.r, slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400408 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400409 http.Error(w, err.Error(), http.StatusInternalServerError)
410 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400411 }
gio308105e2024-04-19 13:12:13 +0400412 instances, err := s.m.FindAllAppInstances(slug)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400413 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400414 http.Error(w, err.Error(), http.StatusInternalServerError)
415 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400416 }
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400417 data := appPageData{
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400418 App: a,
419 Instances: instances,
420 AvailableNetworks: installer.CreateNetworks(global),
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400421 CurrentPage: a.Name(),
422 }
gioaa0fcdb2024-06-10 22:19:25 +0400423 if err := s.tmpl.app.Execute(w, data); err != nil {
424 http.Error(w, err.Error(), http.StatusInternalServerError)
425 return
426 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400427}
428
gioaa0fcdb2024-06-10 22:19:25 +0400429func (s *AppManagerServer) handleInstanceUI(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400430 global, err := s.m.Config()
431 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400432 http.Error(w, err.Error(), http.StatusInternalServerError)
433 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400434 }
gioaa0fcdb2024-06-10 22:19:25 +0400435 slug, ok := mux.Vars(r)["slug"]
436 if !ok {
437 http.Error(w, "empty slug", http.StatusBadRequest)
438 return
439 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400440 instance, err := s.m.FindInstance(slug)
441 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400442 http.Error(w, err.Error(), http.StatusInternalServerError)
443 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400444 }
giof8843412024-05-22 16:38:05 +0400445 a, err := s.m.GetInstanceApp(instance.Id)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400446 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400447 http.Error(w, err.Error(), http.StatusInternalServerError)
448 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400449 }
gio44f621b2024-04-29 09:44:38 +0400450 instances, err := s.m.FindAllAppInstances(a.Slug())
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400451 if err != nil {
gioaa0fcdb2024-06-10 22:19:25 +0400452 http.Error(w, err.Error(), http.StatusInternalServerError)
453 return
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400454 }
gio778577f2024-04-29 09:44:38 +0400455 t := s.tasks[slug]
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400456 data := appPageData{
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400457 App: a,
gio778577f2024-04-29 09:44:38 +0400458 Instance: instance,
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400459 Instances: instances,
460 AvailableNetworks: installer.CreateNetworks(global),
gio778577f2024-04-29 09:44:38 +0400461 Task: t,
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400462 CurrentPage: instance.Id,
463 }
gioaa0fcdb2024-06-10 22:19:25 +0400464 if err := s.tmpl.app.Execute(w, data); err != nil {
465 http.Error(w, err.Error(), http.StatusInternalServerError)
466 return
467 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400468}