package web import ( "context" "net/http" "path/filepath" "strconv" "strings" "time" "supermarket/internal/api/gitea" "supermarket/internal/hugorepo" "supermarket/internal/profile" "supermarket/internal/sites" ) // siteRepo is a website with a Hugo repository, from the profile. type siteRepo struct { Index int URL string Base string // the URL without a trailing slash, for links Repo string // owner/name Branch string // as set in the profile; "" for the default Err string // why the repository can't be read } // siteSynced is what the last sync of a site's working copy found. type siteSynced struct { At time.Time `json:"at"` Branch string `json:"branch"` Err string `json:"error,omitempty"` } func siteSyncKey(full string) string { return "sites:synced:" + full } func (s *Server) siteRepos() []siteRepo { p, _ := profile.Load(s.DB) if p == nil { return nil } var out []siteRepo for i, w := range p.Websites { sr := siteRepo{Index: i, URL: w.URL, Base: strings.TrimRight(w.URL, "/"), Branch: w.Branch} if w.Repo == "" { sr.Err = "no Hugo repository set" } else if _, full, err := gitea.ParseRepo(w.Repo); err != nil { sr.Err = err.Error() } else { sr.Repo = full } out = append(out, sr) } return out } func (s *Server) siteDir(full string) string { return filepath.Join(s.DataDir, "sites", strings.ReplaceAll(full, "/", "__")) } func (s *Server) currentSite(r *http.Request) ([]siteRepo, *siteRepo) { all := s.siteRepos() want, err := strconv.Atoi(r.URL.Query().Get("site")) if err != nil { want, err = strconv.Atoi(r.PostFormValue("site")) } for i := range all { if err == nil && all[i].Index == want { return all, &all[i] } } for i := range all { if all[i].Repo != "" { return all, &all[i] } } return all, nil } func (s *Server) sitesPage(w http.ResponseWriter, r *http.Request) { s.renderSites(w, r, "") } func (s *Server) renderSites(w http.ResponseWriter, r *http.Request, errMsg string) { all, cur := s.currentSite(r) data := map[string]any{"Sites": all, "Current": cur, "Gitea": s.gitea() != nil, "Synced": siteSynced{}} if cur != nil && cur.Repo != "" { var synced siteSynced if ok, _ := s.DB.GetJSON(siteSyncKey(cur.Repo), &synced); ok { data["Synced"], data["HasSynced"] = synced, true if !synced.At.IsZero() { wc := &hugorepo.Repo{Dir: s.siteDir(cur.Repo), Branch: synced.Branch} wc.Acquire() cat, err := sites.Read(wc.Dir, wc.PathOf) if err == nil { ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second) data["Commits"], _ = wc.Log(ctx, 8) cancel() } wc.Release() if err != nil { errMsg = "Couldn't read the working copy: " + err.Error() } else { data["Catalog"] = cat data["SectionPages"], data["OtherPages"] = splitPages(cat.Pages) } } } } s.render(w, r, "sites", page{Title: "Sites", Nav: "sites/site", Error: errMsg, Data: data}) } func splitPages(ps []sites.Page) (withSections, other []sites.Page) { for _, p := range ps { if p.Sections { withSections = append(withSections, p) } else { other = append(other, p) } } return } // sitesSync brings the site's working copy up to date with its published // branch. func (s *Server) sitesSync(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } _, cur := s.currentSite(r) if cur == nil || cur.Repo == "" { s.renderSites(w, r, "Set this website's Hugo repository in Settings → Profile first.") return } ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute) defer cancel() ap := s.applier(seoSite{Index: cur.Index, URL: cur.URL}) synced := siteSynced{At: time.Now()} wc, _, branch, err := ap.WorkingCopy(ctx) if err != nil { var old siteSynced if ok, _ := s.DB.GetJSON(siteSyncKey(cur.Repo), &old); ok { synced = old // keep showing the last good copy } synced.Err = err.Error() } else { wc.Release() synced.Branch = branch } if err := s.DB.PutJSON(siteSyncKey(cur.Repo), synced); err != nil { s.serverError(w, err) return } http.Redirect(w, r, "/sites?site="+strconv.Itoa(cur.Index), http.StatusSeeOther) }