Phase 9, part 5: file storage, a folder or an S3 bucket

internal/storage keeps files that must outlast the app's disk (exports,
uploads) behind one interface: a folder (<data>/files) or an S3-compatible
bucket (minio-go, path-style, for Garage). Names that would reach outside
the store are refused. An S3 store is checked at start; supermarket config
checks the storage and Postgres for real. The same tests run on a folder
and on Garage (SUPERMARKET_TEST_S3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 07:42:11 -07:00
co-authored by Claude Opus 5.5
parent c76a7791ee
commit d2f1658708
13 changed files with 718 additions and 8 deletions
+58
View File
@@ -45,6 +45,7 @@ import (
"supermarket/internal/legacy"
"supermarket/internal/mcpserver"
"supermarket/internal/secrets"
"supermarket/internal/storage"
"supermarket/internal/web"
)
@@ -96,6 +97,10 @@ func configCommand() int {
for _, it := range cfg.Summary() {
fmt.Printf("%-9s %s\n", it.Name+":", it.Value)
}
if err == nil {
fmt.Println()
err = liveChecks(cfg)
}
if err == nil {
err = cfg.Ready()
}
@@ -111,6 +116,43 @@ func configCommand() int {
return 0
}
// liveChecks reaches what the settings name: Postgres, and file storage
// (writing, reading and deleting a small file).
func liveChecks(cfg *config.Config) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var problems []string
if cfg.DatabaseURL != "" {
fmt.Print("Checking the database… ")
d, err := sql.Open("pgx", cfg.DatabaseURL)
if err == nil {
err = d.PingContext(ctx)
d.Close()
}
if err != nil {
fmt.Println("no")
problems = append(problems, "Postgres can't be reached: "+err.Error())
} else {
fmt.Println("reachable")
}
}
fmt.Print("Checking the file storage… ")
st, err := storage.Open(cfg.Storage)
if err == nil {
err = storage.Check(ctx, st)
}
if err != nil {
fmt.Println("no")
problems = append(problems, "The file storage doesn't work: "+err.Error())
} else {
fmt.Println("writable")
}
if len(problems) > 0 {
return errors.New(strings.Join(problems, "\n"))
}
return nil
}
func run(log *slog.Logger) error {
cfg, err := config.Load(os.Getenv)
if err != nil {
@@ -193,6 +235,21 @@ func run(log *slog.Logger) error {
if err != nil {
return err
}
// Files that must outlast this copy of the app: a folder, or S3,
// checked now so a wrong key is found at the start.
files, err := storage.Open(cfg.Storage)
if err != nil {
return err
}
if cfg.Storage.S3() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
err = storage.Check(ctx, files)
cancel()
if err != nil {
return err
}
}
token, err := mcpToken(filepath.Join(dataDir, "mcp-token"))
if err != nil {
return err
@@ -221,6 +278,7 @@ func run(log *slog.Logger) error {
Secrets: sec,
Core: c,
DataDir: dataDir,
Files: files,
PublicURL: publicURL,
Version: version,
MCPToken: token,