package main import ( "context" "embed" "io/fs" "log" "net/http" "os" "os/signal" "syscall" "time" "github.com/sandrodach/mindsift/internal/api" "github.com/sandrodach/mindsift/internal/config" "github.com/sandrodach/mindsift/internal/storage" "github.com/sandrodach/mindsift/internal/ws" ) //go:embed all:dist var embeddedDist embed.FS func main() { cfg := config.Load() log.Printf("🧠 Starting MindSift Server on port %s (DB: %s)", cfg.Port, cfg.DatabasePath) store, err := storage.NewStore(cfg.DatabasePath) if err != nil { log.Fatalf("Failed to initialize storage: %v", err) } defer store.Close() // Ensure initial default workspace exists on fresh self-hosted boot if _, err := store.EnsureDefaultUser(); err != nil { log.Printf("Warning: could not seed default user: %v", err) } wsHub := ws.NewHub() staticFS, err := fs.Sub(embeddedDist, "dist") if err != nil { log.Fatalf("Failed to mount embedded dist filesystem: %v", err) } srv := api.NewServer(cfg, store, wsHub, staticFS) httpServer := &http.Server{ Addr: ":" + cfg.Port, Handler: srv.Router(), ReadTimeout: 15 * time.Second, WriteTimeout: 30 * time.Second, IdleTimeout: 60 * time.Second, } go func() { log.Printf("✨ MindSift ready at http://0.0.0.0:%s", cfg.Port) if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatalf("HTTP server error: %v", err) } }() stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGTERM) <-stop log.Println("Shutting down MindSift server gracefully...") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _ = httpServer.Shutdown(ctx) }