Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
// Supermarket serves the marketing suite's web app. It's meant to run in
|
||||
// its Docker container (see compose.yaml), with its data folder at /data.
|
||||
//
|
||||
// Environment:
|
||||
//
|
||||
// SUPERMARKET_DATA data folder (default /data)
|
||||
// SUPERMARKET_ADDR listen address (default :8080)
|
||||
// SUPERMARKET_PUBLIC_URL the address the browser uses (default http://localhost:8470)
|
||||
// SUPERMARKET_CORE_CONF CoreDesktop's core.conf (default <data>/core.conf)
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/core"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/fileutil"
|
||||
"supermarket/internal/legacy"
|
||||
"supermarket/internal/mcpserver"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/web"
|
||||
)
|
||||
|
||||
// version is set at build time (-ldflags "-X main.version=...").
|
||||
var version = "dev"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "version" || os.Args[1] == "--version") {
|
||||
fmt.Println(version)
|
||||
return
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
if err := run(log); err != nil {
|
||||
log.Error("stopped", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func env(key, def string) string {
|
||||
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func run(log *slog.Logger) error {
|
||||
dataDir := env("SUPERMARKET_DATA", "/data")
|
||||
addr := env("SUPERMARKET_ADDR", ":8080")
|
||||
publicURL := strings.TrimRight(env("SUPERMARKET_PUBLIC_URL", "http://localhost:8470"), "/")
|
||||
coreConf := env("SUPERMARKET_CORE_CONF", filepath.Join(dataDir, "core.conf"))
|
||||
|
||||
// Everything the app writes (database, secrets, clones) is for this user
|
||||
// only: it will hold contacts and credentials.
|
||||
syscall.Umask(0o077)
|
||||
|
||||
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
database, err := db.Open(filepath.Join(dataDir, "app.db"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
sec, err := secrets.Open(filepath.Join(dataDir, "secrets.json"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token, err := mcpToken(filepath.Join(dataDir, "mcp-token"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := core.Load(coreConf)
|
||||
if err != nil {
|
||||
log.Warn("couldn't read the Core's settings", "file", coreConf, "err", err)
|
||||
}
|
||||
|
||||
// The import never stops the app: a bad old file is reported on the
|
||||
// overview page instead.
|
||||
status, importErr := legacy.Import(database, sec, filepath.Join(dataDir, "import"))
|
||||
if importErr != nil {
|
||||
log.Error("importing the old app's data", "err", importErr)
|
||||
} else if status != nil {
|
||||
log.Info("imported the old app's data", "summary", strings.Join(status.Summary, "; "))
|
||||
}
|
||||
if status == nil && importErr == nil {
|
||||
status, _ = legacy.LoadStatus(database)
|
||||
}
|
||||
|
||||
srv := &web.Server{
|
||||
DB: database,
|
||||
Secrets: sec,
|
||||
Core: c,
|
||||
DataDir: dataDir,
|
||||
PublicURL: publicURL,
|
||||
Version: version,
|
||||
MCPToken: token,
|
||||
MCP: mcpserver.Handler(database, token, version),
|
||||
Log: log,
|
||||
ImportStatus: status,
|
||||
ImportErr: importErr,
|
||||
}
|
||||
handler, err := srv.Handler()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
log.Info("Supermarket is running", "version", version, "listen", addr, "open", publicURL)
|
||||
errCh <- httpServer.ListenAndServe()
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
case <-ctx.Done():
|
||||
log.Info("shutting down")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mcpToken reads the bearer token agents use for /mcp, creating it if there
|
||||
// is none. install.sh may create it first, to give it to Hermes.
|
||||
func mcpToken(path string) (string, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err == nil {
|
||||
if t := strings.TrimSpace(string(raw)); len(t) >= 32 {
|
||||
return t, nil
|
||||
}
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return "", err
|
||||
}
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
t := hex.EncodeToString(b)
|
||||
if err := fileutil.WriteFileAtomic(path, []byte(t+"\n"), 0600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
Reference in New Issue
Block a user