Phase 10, part 9: the server package

compose.server.yaml runs Supermarket behind Caddy (HTTPS from Let's
Encrypt, Caddy's own CA or the team's), with Postgres on an internal
network and a nightly pg_dump kept two weeks. install-server.sh asks for
the domain, certificate and mail server, makes the secret key and database
password, starts it all and prints the setup link. Settings → Backups lists
the dumps on a server, and how the operator restores one. Checked end to
end in Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 09:37:34 -07:00
co-authored by Claude Opus 5.5
parent c8964cec12
commit da16736fb4
15 changed files with 532 additions and 17 deletions
+50
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"
@@ -59,6 +60,10 @@ func (s *Server) backupsPage(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) renderBackups(w http.ResponseWriter, r *http.Request, errMsg, flash string) {
if s.DB.Dialect != db.SQLite {
s.renderServerBackups(w, r)
return
}
bs, err := backup.List(s.backupDir())
if err != nil {
s.serverError(w, err)
@@ -82,6 +87,10 @@ func (s *Server) renderBackups(w http.ResponseWriter, r *http.Request, errMsg, f
}
func (s *Server) backupsAction(w http.ResponseWriter, r *http.Request) {
if s.DB.Dialect != db.SQLite {
http.NotFound(w, r) // the operator backs Postgres up, and restores it
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
@@ -124,6 +133,10 @@ func (s *Server) backupsAction(w http.ResponseWriter, r *http.Request) {
// data (not its secrets), so it's served to this machine only, like
// everything else here.
func (s *Server) backupDownload(w http.ResponseWriter, r *http.Request) {
if s.DB.Dialect != db.SQLite {
http.NotFound(w, r)
return
}
b, err := backup.Find(s.backupDir(), r.PathValue("name"))
if err != nil {
http.NotFound(w, r)
@@ -208,3 +221,40 @@ func (s *Server) backupUpload(w http.ResponseWriter, r *http.Request) {
}
s.renderBackups(w, r, "", "Uploaded as "+b.Name+". Restore it from the list when you're ready.")
}
// dump is one of the server's nightly database dumps.
type dump struct {
Name string
Size int64
At time.Time
}
// renderServerBackups lists the nightly dumps of Postgres (compose.server.yaml's
// backup service), newest first, and how the operator restores one.
func (s *Server) renderServerBackups(w http.ResponseWriter, r *http.Request) {
data := map[string]any{"Dir": ""}
if s.Config != nil && s.Config.PGBackups != "" {
data["Dir"] = s.Config.PGBackups
entries, err := os.ReadDir(s.Config.PGBackups)
if err != nil {
data["Err"] = err.Error()
}
var dumps []dump
var total int64
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".dump") {
continue
}
if fi, err := e.Info(); err == nil {
dumps = append(dumps, dump{Name: e.Name(), Size: fi.Size(), At: fi.ModTime()})
total += fi.Size()
}
}
sort.Slice(dumps, func(i, j int) bool { return dumps[i].At.After(dumps[j].At) })
data["Dumps"], data["Total"] = dumps, total
if len(dumps) > 0 && time.Since(dumps[0].At) > 36*time.Hour {
data["Stale"] = dumps[0].At
}
}
s.render(w, r, "backups-server", page{Title: "Backups", Nav: "settings/backups", Data: data})
}