package server import ( "context" "errors" "fmt" "io/fs" "log" "net/http" "os" "os/signal" "runtime" "strings" "syscall" "time" "glimmer/internal/config" "glimmer/internal/db" ) type Server struct { cfg *config.Config db *db.DB sessions *sessionStore limiter *rateLimiter authLimiter *rateLimiter bcryptSem chan struct{} } func New(cfg *config.Config, database *db.DB) *Server { ttl := time.Duration(cfg.Admin.SessionHours) * time.Hour // Allow at most NumCPU concurrent bcrypt operations so password checks // cannot peg the CPU on small hardware. Always at least 1. bcryptWorkers := runtime.NumCPU() if bcryptWorkers < 1 { bcryptWorkers = 1 } return &Server{ cfg: cfg, db: database, sessions: newSessionStore(database, ttl), limiter: newRateLimiter(1 * time.Second), authLimiter: newRateLimiter(1 * time.Second), bcryptSem: make(chan struct{}, bcryptWorkers), } } func securityHeaders(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // SAMEORIGIN (not DENY) so the admin Pastes page can preview a paste in // an in-page iframe; cross-site framing is still blocked. w.Header().Set("X-Frame-Options", "SAMEORIGIN") w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin") // All assets are served from the same origin (CSS and JS are embedded), // so the app works fully offline and we can lock the origin down. // img-src allows data: and https: so uploaded/remote images and // favicons still render, and blob: so the client-side image compressor // (canvas) can load a selected file before upload. script-src is // 'self' only — all JS lives in /static/js/ files; do not add inline //