| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "html/template" |
| 8 | "net/http" |
| 9 | |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | |
| 12 | "amadan.net/oficina/base/internal/catalog" |
| 13 | "amadan.net/oficina/base/internal/install" |
| 14 | "amadan.net/oficina/base/internal/store" |
| 15 | ) |
| 16 | |
| 17 | type storeRow struct { |
| 18 | catalog.Entry |
| 19 | State string // "", "installing", "installed" or "failed" |
| 20 | Error string |
| 21 | } |
| 22 | |
| 23 | // IconHTML is the catalogue's own SVG, which is trusted markup. |
| 24 | func (s storeRow) IconHTML() template.HTML { return template.HTML(s.Icon) } |
| 25 | |
| 26 | type storePage struct { |
| 27 | Name string |
| 28 | storeContent |
| 29 | Busy bool |
| 30 | } |
| 31 | |
| 32 | // storeContent is what the Store shows a member: a row per catalogue |
| 33 | // entry, in the catalogue's order, with its install's state and error. The |
| 34 | // Store page and the JSON (GET /api/v1/store) both draw from it. |
| 35 | type storeContent struct { |
| 36 | IsOwner bool |
| 37 | Installing bool |
| 38 | Rows []storeRow |
| 39 | } |
| 40 | |
| 41 | // row is the entry id's row, and false when the catalogue has no such app. |
| 42 | func (c storeContent) row(id string) (storeRow, bool) { |
| 43 | for _, r := range c.Rows { |
| 44 | if r.ID == id { |
| 45 | return r, true |
| 46 | } |
| 47 | } |
| 48 | return storeRow{}, false |
| 49 | } |
| 50 | |
| 51 | func (a *app) storeContent(ctx context.Context, me store.Member) (storeContent, error) { |
| 52 | installs, err := store.Installs(ctx, a.db) |
| 53 | if err != nil { |
| 54 | return storeContent{}, fmt.Errorf("installs: %w", err) |
| 55 | } |
| 56 | state := map[string]store.Install{} |
| 57 | for _, in := range installs { |
| 58 | state[in.App] = in |
| 59 | } |
| 60 | c := storeContent{IsOwner: me.Role == "owner"} |
| 61 | for _, e := range catalog.All() { |
| 62 | in := state[e.ID] |
| 63 | c.Rows = append(c.Rows, storeRow{Entry: e, State: in.State, Error: in.Error}) |
| 64 | if in.State == "installing" { |
| 65 | c.Installing = true |
| 66 | } |
| 67 | } |
| 68 | return c, nil |
| 69 | } |
| 70 | |
| 71 | func (a *app) storePage(w http.ResponseWriter, r *http.Request) { |
| 72 | me, _ := CurrentMember(r) |
| 73 | c, err := a.storeContent(r.Context(), me) |
| 74 | if err != nil { |
| 75 | a.logger.Error("store", "err", err) |
| 76 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 77 | return |
| 78 | } |
| 79 | w.Header().Set("Cache-Control", "no-store") |
| 80 | a.render(w, r, "store", storePage{Name: a.cfg.Name, storeContent: c, Busy: r.URL.Query().Get("problem") == "busy"}) |
| 81 | } |
| 82 | |
| 83 | // owner returns the catalogue entry named in the URL when the signed-in |
| 84 | // member is the owner, and otherwise answers 403 or 404 and reports false. |
| 85 | func (a *app) owner(w http.ResponseWriter, r *http.Request) (catalog.Entry, bool) { |
| 86 | me, _ := CurrentMember(r) |
| 87 | if me.Role != "owner" { |
| 88 | http.Error(w, "forbidden", http.StatusForbidden) |
| 89 | return catalog.Entry{}, false |
| 90 | } |
| 91 | e, ok := catalog.Get(chi.URLParam(r, "app")) |
| 92 | if !ok { |
| 93 | http.NotFound(w, r) |
| 94 | return catalog.Entry{}, false |
| 95 | } |
| 96 | return e, true |
| 97 | } |
| 98 | |
| 99 | // storeInstall starts an install and answers at once; the Store page shows |
| 100 | // progress. Start has claimed the install before the redirect, so the page |
| 101 | // the browser loads next shows it installing. The install outlives the |
| 102 | // request. |
| 103 | func (a *app) storeInstall(w http.ResponseWriter, r *http.Request) { |
| 104 | e, ok := a.owner(w, r) |
| 105 | if !ok { |
| 106 | return |
| 107 | } |
| 108 | if err := a.inst.Start(r.Context(), e.ID); err != nil { |
| 109 | a.logger.Error("install", "app", e.ID, "err", err) |
| 110 | } |
| 111 | http.Redirect(w, r, "/store", http.StatusSeeOther) |
| 112 | } |
| 113 | |
| 114 | func (a *app) uninstallPage(w http.ResponseWriter, r *http.Request) { |
| 115 | e, ok := a.owner(w, r) |
| 116 | if !ok { |
| 117 | return |
| 118 | } |
| 119 | w.Header().Set("Cache-Control", "no-store") |
| 120 | a.render(w, r, "uninstall", struct { |
| 121 | Name string |
| 122 | App catalog.Entry |
| 123 | }{a.cfg.Name, e}) |
| 124 | } |
| 125 | |
| 126 | func (a *app) storeUninstall(w http.ResponseWriter, r *http.Request) { |
| 127 | e, ok := a.owner(w, r) |
| 128 | if !ok { |
| 129 | return |
| 130 | } |
| 131 | switch err := a.inst.Uninstall(r.Context(), e.ID); { |
| 132 | case errors.Is(err, install.ErrBusy): |
| 133 | http.Redirect(w, r, "/store?problem=busy", http.StatusSeeOther) |
| 134 | return |
| 135 | case errors.Is(err, store.ErrNoInstall): |
| 136 | // Already uninstalled. |
| 137 | case err != nil: |
| 138 | a.logger.Error("uninstall", "app", e.ID, "err", err) |
| 139 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 140 | return |
| 141 | } |
| 142 | http.Redirect(w, r, "/store", http.StatusSeeOther) |
| 143 | } |
| 144 | |