Settings -> WebDAV: a user connects an external WebDAV share (any service; on Core the deployment's NEXTCLOUD_URL offers Nextcloud as a one-click prefill - DATA, never a TIER code fork). The share materializes as a Collection and every file becomes a Bookmark carrying the file's DAV URL, reconciled by the WebDavItem path ledger (migration 020 + schema.sql, db-converge proven): - idempotent syncs; etag changes refresh the ledger only, never user-edited title/notes/tags; same-URL bookmarks are reused (URL-identity invariant); - vanished files detach membership but keep the bookmark (sync never destroys user data); a user-deleted bookmark is a tombstone that is never resurrected; a user-removed membership stays removed - unless the whole collection was deleted, when the next sync re-links every live row; - bounded walks (200 dirs / 1000 files / depth 12) and a truncated listing never reconciles deletions; - netguard-pinned dials (80/443, no trusted exemption for user hosts), Basic auth in a header only, redirects refused outright; - the webdav connector entitlement gates creation AND every sync; a downgrade suppresses instead of deleting; - engine tick per notify/agent discipline (jitter, advisory lease, budget, log-not-fatal); /test proves PROPFIND, /sync reconciles on demand; passwords write-only + masked, PATCH carries version (409 on stale), cap 10 per user. internal/webdav (client+engine), handlers/webdav.go + routes, main.go 4th loop, config.NextcloudSuggestion, Settings tab + webdav-settings.tsx, API.md contract section, env docs, Core installer writes NEXTCLOUD_URL. Client unit tests on an httptest fake DAV server; four integration tests cover lifecycle, recreation, plan gate and suggestion; regressions drive the Settings UI end-to-end (masked rows, prefill, connect/sync/test, no secret echo). All suites green.
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/andrew/superbrain/internal/agent"
|
|
"github.com/andrew/superbrain/internal/config"
|
|
"github.com/andrew/superbrain/internal/db"
|
|
"github.com/andrew/superbrain/internal/http"
|
|
"github.com/andrew/superbrain/internal/notify"
|
|
"github.com/andrew/superbrain/internal/scripts"
|
|
"github.com/andrew/superbrain/internal/signals"
|
|
"github.com/andrew/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(4)
|
|
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) }()
|
|
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
|
|
}
|