Posts can be written for a buying signal: the prompt gets its hypothesis and angle, and links carry its campaign tag. Plausible (Stats API v2) is read once a day for visitors on tagged links, recorded per signal as Visited. Stripe (restricted key) is read every hour: $0 checkouts are Signed up, paid invoices and one-time payments are Paid with their value, tied to a signal by the checkout's utm_campaign metadata or the payer's email. The Core's Cal.com has no API, so booking emails from Cal.com in the outreach mailbox count as Booked. New Settings → Website & sales; the signal page lists its posts and tagged links. Schema 11. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
221 lines
6.1 KiB
Go
221 lines
6.1 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/ai"
|
|
"supermarket/internal/content"
|
|
"supermarket/internal/core"
|
|
"supermarket/internal/crm"
|
|
"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)
|
|
}
|
|
|
|
// Hermes' Anthropic key and model, if the user chose to share them
|
|
ai.SharedFile = filepath.Join(dataDir, "hermes-ai.json")
|
|
// What Hermes' CRM job checks before it runs; see internal/crm.
|
|
crm.StateFile = filepath.Join(dataDir, "crm-state")
|
|
if err := crm.WriteState(database, time.Now()); err != nil {
|
|
log.Error("writing the CRM state file", "err", err)
|
|
}
|
|
|
|
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 srv.RunImports(ctx)
|
|
go srv.RunDiscovery(ctx)
|
|
go srv.RunOutreach(ctx)
|
|
go srv.RunTracking(ctx)
|
|
// A sync falls due, and a claim times out, without anything being
|
|
// written: refresh the state file each minute.
|
|
go func() {
|
|
t := time.NewTicker(time.Minute)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-t.C:
|
|
if err := crm.WriteState(database, now); err != nil {
|
|
log.Error("writing the CRM state file", "err", err)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
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
|
|
}
|