fix: wire security headers, graceful shutdown, and settings invalidation

Lifecycle (internal/app + main.go):
- App.OnShutdown hooks run after the HTTP server drains, in
  registration order: Worker.Shutdown (background jobs now drain
  instead of dying on SIGTERM), DeviceAuthMiddleware.Stop (releases
  the rate-limiter ticker), Handler.CloseScanner (fsnotify watcher
  and scan log files)

Security headers:
- SecurityHeadersMiddleware (survivor of middleware/security.go,
  renamed security_headers.go) is now on the echo stack: nosniff,
  frame-deny, HSTS, CSP (unsafe-inline required by the sanctioned
  theme flash guard + Alpine), referrer and permissions policy

Scanner settings cache:
- settings saves (UpdateScanSettings + ApplySetting for the three
  scan keys) now invalidate MediaScanner's 30s settings cache via
  SystemSettingsHandler.SetScanSettingsChanged, so changes apply on
  the next poll instead of after the TTL
- MediaScanner.Close closes the scan logger, fixing a file-descriptor
  leak on every per-item rescan (fresh scanners never closed logs)

Client fix:
- custom-section-builder's add/remove-selection buttons used inline
  onclick attributes referencing module-scoped functions, which are
  not global in a bundled ES module: the buttons threw ReferenceError.
  Functions restored and exposed on window via declare global

Also fixes a pre-existing vet error (saveCoverImage copied
echo.Context by value, mutex included) and drops the per-key
SettingsCache.InvalidateKey variant left callerless by the
full-flush invalidation wiring.
This commit is contained in:
John O'Keefe committed 2026-10-25 11:06:26 -04:00
1 parent 8631aa00bf
commit a716b386a1
10 files changed
+98 -195

No files matched your search

+24 -2
View File
@@ -15,8 +15,9 @@ import (
)
type SystemSettingsHandler struct {
db *database.Queries
settings *database.SettingsRegistry
db *database.Queries
settings *database.SettingsRegistry
onScanSettingsChanged func()
}
func NewSystemSettingsHandler(db *database.Queries) *SystemSettingsHandler {
@@ -25,6 +26,19 @@ func NewSystemSettingsHandler(db *database.Queries) *SystemSettingsHandler {
}
}
// SetScanSettingsChanged registers a callback fired after scan settings are
// successfully saved on any write path, so listeners (the scanner) can drop
// their cached values immediately instead of waiting out the cache TTL.
func (h *SystemSettingsHandler) SetScanSettingsChanged(fn func()) {
h.onScanSettingsChanged = fn
}
func (h *SystemSettingsHandler) notifyScanSettingsChanged() {
if h.onScanSettingsChanged != nil {
h.onScanSettingsChanged()
}
}
// SetSettings wires the tunable settings registry. Required for the unified
// /api/system/settings endpoints and for cache invalidation after writes.
func (h *SystemSettingsHandler) SetSettings(s *database.SettingsRegistry) {
@@ -121,6 +135,12 @@ func (h *SystemSettingsHandler) ApplySetting(ctx context.Context, key, value str
h.settings.Reload(ctx)
// Scan keys are also cached inside MediaScanner (30s TTL); let it drop
// its cache so the change takes effect on the next poll.
if key == "scan_poll_interval_seconds" || key == "auto_scan_enabled" || key == "scan_extractor_version" {
h.notifyScanSettingsChanged()
}
resp := UpdateSettingResponse{ReloadRequired: def.RequiresRestart}
for _, e := range h.settings.All() {
if e.Key == key {
@@ -218,6 +238,8 @@ func (h *SystemSettingsHandler) UpdateScanSettings(c *echo.Context) error {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
h.notifyScanSettingsChanged()
h.reload(c)
return c.JSON(http.StatusOK, ScanSettingsResponse{