This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knotmirror / adminpage.go
5.2 kB 198 lines
1package knotmirror 2 3import ( 4 "database/sql" 5 "embed" 6 "encoding/json" 7 "fmt" 8 "html" 9 "html/template" 10 "log/slog" 11 "net/http" 12 "strconv" 13 "time" 14 15 "github.com/bluesky-social/indigo/atproto/syntax" 16 "github.com/go-chi/chi/v5" 17 "tangled.org/core/appview/pagination" 18 "tangled.org/core/knotmirror/db" 19 "tangled.org/core/knotmirror/models" 20 "tangled.org/core/knotmirror/xrpc" 21) 22 23//go:embed templates/*.html 24var templateFS embed.FS 25 26const repoPageSize = 20 27 28type AdminServer struct { 29 db *sql.DB 30 resyncer *Resyncer 31 xrpc *xrpc.Xrpc 32 logger *slog.Logger 33} 34 35func NewAdminServer(l *slog.Logger, database *sql.DB, resyncer *Resyncer, x *xrpc.Xrpc) *AdminServer { 36 return &AdminServer{ 37 db: database, 38 resyncer: resyncer, 39 xrpc: x, 40 logger: l, 41 } 42} 43 44func (s *AdminServer) Router() http.Handler { 45 r := chi.NewRouter() 46 r.Get("/repos", s.handleRepos()) 47 r.Get("/hosts", s.handleHosts()) 48 49 r.Post("/api/triggerRepoResync", s.handleRepoResyncTrigger()) 50 r.Post("/api/cancelRepoResync", s.handleRepoResyncCancel()) 51 r.Get("/api/inflight", s.handleInflight()) 52 return r 53} 54 55func (s *AdminServer) handleInflight() http.HandlerFunc { 56 return func(w http.ResponseWriter, r *http.Request) { 57 entries := s.xrpc.Inflight() 58 w.Header().Set("Content-Type", "application/json") 59 _ = json.NewEncoder(w).Encode(entries) 60 } 61} 62 63func funcmap() template.FuncMap { 64 return template.FuncMap{ 65 "add": func(a, b int) int { return a + b }, 66 "sub": func(a, b int) int { return a - b }, 67 "readt": func(ts int64) string { 68 if ts <= 0 { 69 return "n/a" 70 } 71 return time.Unix(ts, 0).Format("2006-01-02 15:04") 72 }, 73 "const": func() map[string]any { 74 return map[string]any{ 75 "AllRepoStates": models.AllRepoStates, 76 "AllHostStatuses": models.AllHostStatuses, 77 } 78 }, 79 } 80} 81 82func (s *AdminServer) handleRepos() http.HandlerFunc { 83 tpl := template.Must(template.New("").Funcs(funcmap()).ParseFS(templateFS, "templates/base.html", "templates/repos.html")) 84 return func(w http.ResponseWriter, r *http.Request) { 85 pageNum, _ := strconv.Atoi(r.URL.Query().Get("page")) 86 if pageNum < 1 { 87 pageNum = 1 88 } 89 page := pagination.Page{ 90 Offset: (pageNum - 1) * repoPageSize, 91 Limit: repoPageSize, 92 } 93 94 var ( 95 did = r.URL.Query().Get("did") 96 knot = r.URL.Query().Get("knot") 97 state = r.URL.Query().Get("state") 98 ) 99 100 repos, err := db.ListRepos(r.Context(), s.db, page, did, knot, state) 101 if err != nil { 102 http.Error(w, err.Error(), http.StatusInternalServerError) 103 return 104 } 105 counts, err := db.GetRepoCountsByState(r.Context(), s.db) 106 if err != nil { 107 http.Error(w, err.Error(), http.StatusInternalServerError) 108 return 109 } 110 err = tpl.ExecuteTemplate(w, "base", map[string]any{ 111 "Repos": repos, 112 "RepoCounts": counts, 113 "Page": pageNum, 114 "FilterByDid": did, 115 "FilterByKnot": knot, 116 "FilterByState": models.RepoState(state), 117 }) 118 if err != nil { 119 slog.Error("failed to render", "err", err) 120 } 121 } 122} 123 124func (s *AdminServer) handleHosts() http.HandlerFunc { 125 tpl := template.Must(template.New("").Funcs(funcmap()).ParseFS(templateFS, "templates/base.html", "templates/hosts.html")) 126 return func(w http.ResponseWriter, r *http.Request) { 127 var status = models.HostStatus(r.URL.Query().Get("status")) 128 if status == "" { 129 status = models.HostStatusActive 130 } 131 132 hosts, err := db.ListHosts(r.Context(), s.db, status) 133 if err != nil { 134 http.Error(w, err.Error(), http.StatusInternalServerError) 135 return 136 } 137 err = tpl.ExecuteTemplate(w, "base", map[string]any{ 138 "Hosts": hosts, 139 "FilterByStatus": models.HostStatus(status), 140 }) 141 if err != nil { 142 slog.Error("failed to render", "err", err) 143 } 144 } 145} 146 147func (s *AdminServer) handleRepoResyncTrigger() http.HandlerFunc { 148 return func(w http.ResponseWriter, r *http.Request) { 149 var repoQuery = r.FormValue("repo") 150 151 repo, err := syntax.ParseATURI(repoQuery) 152 if err != nil || repo.RecordKey() == "" { 153 writeNotif(w, http.StatusBadRequest, fmt.Sprintf("repo parameter invalid: %s", repoQuery)) 154 return 155 } 156 157 if err := s.resyncer.TriggerResyncJob(r.Context(), repo); err != nil { 158 s.logger.Error("failed to trigger resync job", "err", err) 159 writeNotif(w, http.StatusInternalServerError, fmt.Sprintf("repo parameter invalid: %s", repoQuery)) 160 return 161 } 162 writeNotif(w, http.StatusOK, "success") 163 } 164} 165 166func (s *AdminServer) handleRepoResyncCancel() http.HandlerFunc { 167 return func(w http.ResponseWriter, r *http.Request) { 168 var repoQuery = r.FormValue("repo") 169 170 repo, err := syntax.ParseATURI(repoQuery) 171 if err != nil || repo.RecordKey() == "" { 172 writeNotif(w, http.StatusBadRequest, fmt.Sprintf("repo parameter invalid: %s", repoQuery)) 173 return 174 } 175 176 s.resyncer.CancelResyncJob(repo) 177 writeNotif(w, http.StatusOK, "success") 178 } 179} 180 181func writeNotif(w http.ResponseWriter, status int, msg string) { 182 w.Header().Set("Content-Type", "text/html") 183 w.WriteHeader(status) 184 185 class := "info" 186 switch { 187 case status >= 500: 188 class = "error" 189 case status >= 400: 190 class = "warn" 191 } 192 193 fmt.Fprintf(w, 194 `<div hx-swap-oob="beforeend:#notifications"><div class="notif %s">%s</div></div>`, 195 class, 196 html.EscapeString(msg), 197 ) 198}