SPB Git

Repository Public

main.go 2.0 KB 84 lines go

1// control-api is the public entry point of MacLustr Command Center: REST API,2// event stream, WebAuthn ceremonies, and reverse proxy to the web application.3package main45import (6	"log/slog"7	"net"8	"net/http"9	"os"10	"time"1112	"clustr.spboucher.ai/mcc/internal/api"13	"clustr.spboucher.ai/mcc/internal/config"14	"clustr.spboucher.ai/mcc/internal/db"15	"clustr.spboucher.ai/mcc/internal/events"16	"clustr.spboucher.ai/mcc/internal/identity"17	"clustr.spboucher.ai/mcc/internal/svc"18)1920func main() {21	if err := run(); err != nil {22		slog.Error("fatal", "err", err)23		os.Exit(1)24	}25}2627func run() error {28	ctx, stop := svc.Init("control-api")29	defer stop()30	cfg, err := config.Load()31	if err != nil {32		return err33	}34	pool, err := db.Open(ctx, cfg.DatabaseURL)35	if err != nil {36		return err37	}38	defer pool.Close()39	applied, err := db.Migrate(ctx, pool)40	if err != nil {41		return err42	}43	if len(applied) > 0 {44		slog.Info("migrations applied", "files", applied)45	}46	ident, err := identity.New(pool, cfg)47	if err != nil {48		return err49	}50	hub := events.NewHub(pool)51	go hub.Run(ctx)52	go svc.Heartbeat(ctx, pool, "control-api", api.Version, nil)53	go func() { // housekeeping54		t := time.NewTicker(10 * time.Minute)55		defer t.Stop()56		for {57			if err := ident.CleanupExpired(ctx); err != nil && ctx.Err() == nil {58				slog.Warn("cleanup failed", "err", err)59			}60			select {61			case <-ctx.Done():62				return63			case <-t.C:64			}65		}66	}()6768	server := api.NewServer(api.Deps{Pool: pool, Cfg: cfg, Identity: ident, Hub: hub})69	srv := &http.Server{70		Handler:           server.Handler(),71		ReadHeaderTimeout: 10 * time.Second,72		ReadTimeout:       30 * time.Second,73		IdleTimeout:       120 * time.Second,74		MaxHeaderBytes:    64 << 10,75		// No WriteTimeout: the event stream is long-lived; each handler bounds its own work.76	}77	ln, err := net.Listen("tcp", cfg.APIListen)78	if err != nil {79		return err80	}81	slog.Info("listening", "addr", cfg.APIListen, "origin", cfg.PublicOrigin, "rp_id", cfg.RPID, "env", cfg.Env, "version", api.Version)82	return svc.ServeHTTP(ctx, srv, ln)83}84
Files main
Menu

Theme