release gate / check (push) Failing after 7m7s
Foundation for the priced plans (PLAN_PRICING_TIERS.md): every user now belongs to exactly one tenant, and single-tenant deployments (self-hosted, core) use an implicit 'tenant-default' row so no code path special-cases 'no tenant'. Plans are per-tenant DATA, never code forks — the open-source promise depends on the free version being the same binary. Schema (migration 012 + schema.sql, converge- and atomicity-verified): Tenant, TenantMember (1:1 per user in phase 1), TenantEntitlement (per-tenant overrides), QuotaUsage (maintained counters), User.tenantId. The backfill adopts every existing user into the implicit tenant (ADMIN -> owner, others -> member). The User.tenantId FK is re-added by name under a guard so converge pass 2 (CASCADE-drop + chain re-run) stays clean. internal/entitlements: the shipped plan catalog (free/standard/premium/ core/selfhosted: storage, brain links, seats, notification channels, connectors, AI mode), tier -> default-plan derivation, override merging that fails CLOSED (unknown plan resolves to free limits; malformed override rows are ignored so a bad row can never widen access), and WithinQuota boundary logic. internal/db/tenants.go: EnsureDefault reconciles the implicit tenant's plan with TIER but only ever touches auto plans (core/selfhosted) — sold plans are billing data, not deployment config. AdoptOrphans is the runtime self-heal twin of the migration backfill (restored dumps); main runs Bootstrap once at startup, best-effort. QuotaAdd moves counters under an advisory lock and refuses growth past the plan limit with ErrQuotaExceeded; counters floor at 0. Wiring: createUserTx attaches tenant + membership atomically for EVERY creation path (signup, OIDC provisioning, seed CLI); the identity projection carries tenantId, so /api/me and the bridge response expose it. models.User gains TenantID. No behavior changes for existing installs: default limits are declarative until gate points land (connectors, brain links, notifications, storage metering are later steps). API.md documents tenantId and the password- reset endpoints; AGENTS.md records the invariant that features gate on PLAN, never on TIER directly. Tests: entitlements unit tests (catalog, fail-closed resolution, override normalization, quota boundaries) and integration tests (attach-on-create across all three paths with owner/member roles, bootstrap + plan reconcile including 'sold plans never overwritten', limits resolution, quota consume/refuse/release/floor).
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/andrew/superbrain/internal/config"
|
|
"github.com/andrew/superbrain/internal/db"
|
|
"github.com/andrew/superbrain/internal/http"
|
|
"github.com/andrew/superbrain/internal/scripts"
|
|
"github.com/andrew/superbrain/internal/signals"
|
|
"log"
|
|
stdhttp "net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 && os.Args[1] == "--healthcheck" {
|
|
client := stdhttp.Client{Timeout: 3 * time.Second}
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
res, err := client.Get("http://127.0.0.1:" + port + "/readyz")
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
res.Body.Close()
|
|
if res.StatusCode != 200 {
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
cfg := config.Load()
|
|
if err := cfg.Validate(); err != nil {
|
|
log.Fatalf("config: %v", err)
|
|
}
|
|
d, err := db.New(cfg)
|
|
if err != nil {
|
|
log.Fatalf("db: %v", err)
|
|
}
|
|
defer d.Close()
|
|
// Tenancy phase 1: make sure the implicit tenant exists with the
|
|
// tier-derived plan and adopt any tenantless users (self-heal twin of
|
|
// migration 012). Best-effort: user creation re-ensures per transaction,
|
|
// so a failure here must not keep the API down.
|
|
if err := db.NewTenantRepo(d).Bootstrap(context.Background()); err != nil {
|
|
log.Printf("tenancy bootstrap: %v", err)
|
|
}
|
|
if len(os.Args) > 1 && os.Args[1] == "--seed" {
|
|
if err := scripts.Seed(context.Background(), d); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
// Shutdown order is owned by main (review OPS-01): drain in-flight HTTP
|
|
// requests first, then await the background loops, and only afterwards let
|
|
// the deferred db close run. Previously Shutdown raced ListenAndServe's
|
|
// return and the process exited mid-drain.
|
|
workersDone := make(chan struct{})
|
|
go func() {
|
|
defer close(workersDone)
|
|
signals.RunLoop(ctx, d)
|
|
}()
|
|
srv := http.NewServer(cfg, d)
|
|
go func() {
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, stdhttp.ErrServerClosed) {
|
|
log.Fatalf("server failed: %v", err)
|
|
}
|
|
}()
|
|
log.Printf("superbrain API listening on %s", srv.Addr)
|
|
<-ctx.Done()
|
|
stop()
|
|
shutdown, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(shutdown); err != nil {
|
|
log.Printf("shutdown: %v", err)
|
|
}
|
|
<-workersDone
|
|
}
|