- Inbox: a top-level page, with a count in the menu, for everything waiting for approval; Content's drafts are reviewed there. /drafts redirects to it. - Scheduling: publish a draft now or at a time (migration 3 adds publish_at). A scheduler in the app publishes due drafts every 30 seconds. Posts more than 12 hours overdue, or refused before posting, go back to the Inbox with the reason. Content gets Scheduled and Published tabs. - UTM tags on links to the company's own websites and product pages, added when publishing; drafts stay untagged. - The container runs in the computer's time zone, so times are shown and read correctly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
191 lines
5.2 KiB
Go
191 lines
5.2 KiB
Go
// 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)
|
|
// SUPERMARKET_MCP_SOCKET Unix socket for agents' MCP (default <data>/mcp.sock)
|
|
// SUPERMARKET_HOST_DATA the data folder's path on the host, to show in
|
|
// setup instructions (default: same as the data folder)
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"supermarket/internal/content"
|
|
"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"))
|
|
mcpSocket := env("SUPERMARKET_MCP_SOCKET", filepath.Join(dataDir, "mcp.sock"))
|
|
hostData := env("SUPERMARKET_HOST_DATA", dataDir)
|
|
|
|
// 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()
|
|
|
|
if n, err := content.RecoverInterrupted(database); err != nil {
|
|
return err
|
|
} else if n > 0 {
|
|
log.Warn("drafts were being published when Supermarket stopped; marked failed for review", "count", n)
|
|
}
|
|
|
|
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,
|
|
MCPSocket: filepath.Join(hostData, filepath.Base(mcpSocket)),
|
|
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, 2)
|
|
go func() {
|
|
if err := mcpserver.ServeSocket(ctx, database, version, mcpSocket, log); err != nil {
|
|
errCh <- fmt.Errorf("MCP socket %s: %w", mcpSocket, err)
|
|
}
|
|
}()
|
|
go srv.RunScheduler(ctx)
|
|
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
|
|
}
|