Files
sandrodach 48fe61fbce
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 39s
feat(logging): integrate slog logging with env control and request/sync tracing
- Configure structured slog logger in cmd/server/main.go via LOG_LEVEL and DEBUG env vars
- Add HTTP API request logging middleware in internal/api/ with status and duration_ms
- Add WebSocket join/leave, event reception, and debounced SQLite flush tracing in docroom
- Document LOG_LEVEL and DEBUG settings in .env.example, compose.yaml, and docker-compose.yml
2026-10-26 22:12:51 +01:00

86 lines
2.3 KiB
Go

package main
import (
"context"
"embed"
"io/fs"
"log/slog"
"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()
level := cfg.SlogLevel()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level}))
slog.SetDefault(logger)
slog.Info("MindSift Server starting", "port", cfg.Port, "log_level", level.String())
store, err := storage.NewStore(cfg.DatabasePath)
if err != nil {
slog.Error("Failed to initialize storage", "error", err)
os.Exit(1)
}
defer store.Close()
// Seed SMTP defaults from environment variables into DB if not yet configured in DB
store.SeedSMTPDefaultsFromEnv(cfg.SMTPHost, cfg.SMTPPort, cfg.SMTPUser, cfg.SMTPPass, cfg.SMTPFrom, cfg.SMTPTLS)
if cfg.DemoMode {
slog.Info("🧪 Demo-Modus aktiv (MINDA_DEMO=true): Richte Demo-Benutzer admin/admin und user/user ein...")
if err := store.EnsureDemoAccounts(); err != nil {
slog.Warn("⚠️ Fehler beim Anlegen der Demo-Accounts", "error", err)
} else {
slog.Info("✅ Demo-Accounts bereit: admin / admin (Admin-Rolle) und user / user (Standard-Rolle)")
}
}
wsHub := ws.NewHub()
staticFS, err := fs.Sub(embeddedDist, "dist")
if err != nil {
slog.Error("Failed to mount embedded dist filesystem", "error", err)
os.Exit(1)
}
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() {
slog.Info("✨ MindSift ready", "url", "http://0.0.0.0:"+cfg.Port)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("HTTP server error", "error", err)
os.Exit(1)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
slog.Info("Shutting down MindSift server gracefully...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = httpServer.Shutdown(ctx)
}