Files
superbrain/main.go
T
Andrew S Erwin 4b6a58118e Rename Go module to gitea.federated.computer/Federated_Computer/superbrain
The Gitea repository was renamed brain -> superbrain (server-side redirect
already in place; local origin updated at 0.3.5). Carry the rename through
for accurate identity, the 0.3.2 precedent: go.mod + every internal import,
the agent's outbound User-Agent contact URL, README/RUNNING clone URLs and
local notes. CHANGELOG history entries stay as written. Pure identifier
rename, no runtime behavior change; verified with build + vet + unit tests
and the full database integration suite under -race.
2026-11-24 16:02:01 -06:00

113 lines
4.0 KiB
Go

package main
import (
"context"
"errors"
"gitea.federated.computer/Federated_Computer/superbrain/internal/agent"
"gitea.federated.computer/Federated_Computer/superbrain/internal/caldav"
"gitea.federated.computer/Federated_Computer/superbrain/internal/carddav"
"gitea.federated.computer/Federated_Computer/superbrain/internal/config"
"gitea.federated.computer/Federated_Computer/superbrain/internal/db"
"gitea.federated.computer/Federated_Computer/superbrain/internal/http"
"gitea.federated.computer/Federated_Computer/superbrain/internal/notify"
"gitea.federated.computer/Federated_Computer/superbrain/internal/scripts"
"gitea.federated.computer/Federated_Computer/superbrain/internal/signals"
"gitea.federated.computer/Federated_Computer/superbrain/internal/webdav"
"log"
stdhttp "net/http"
"os"
"os/signal"
"sync"
"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)
var wg sync.WaitGroup
wg.Add(6)
go func() { defer wg.Done(); signals.RunLoop(ctx, d) }()
// Notification engine (DESIGN_NOTIFICATIONS.md): scans rules and
// delivers through channels; shares the shutdown contract with
// signals — drain HTTP first, then the loops, then close the db.
go func() { defer wg.Done(); notify.RunLoop(ctx, d) }()
// Agent automations ("hey superbrain"): evaluates compiled tasks on
// their own poll schedules under a global advisory lease.
go func() { defer wg.Done(); agent.RunLoop(ctx, d) }()
// WebDAV shares (Settings → WebDAV): reconciles connected file shares
// into Collections of per-file bookmark URLs under the same lease and
// shutdown discipline as the other loops.
go func() { defer wg.Done(); webdav.RunLoop(ctx, d) }()
// CalDAV calendars (Settings → CalDAV): reconciles each server
// calendar into a Collection of per-event bookmark URLs, on its own
// advisory lease so the two DAV loops never block each other.
go func() { defer wg.Done(); caldav.RunLoop(ctx, d) }()
// CardDAV address books (Settings -> CardDAV): each server book becomes a
// Collection of per-contact bookmark URLs, on its own advisory lease.
go func() { defer wg.Done(); carddav.RunLoop(ctx, d) }()
wg.Wait()
}()
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
}