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.
130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
)
|
|
|
|
// App manages application lifecycle and graceful shutdown
|
|
type App struct {
|
|
echo *echo.Echo
|
|
server *http.Server
|
|
shutdownTimeout time.Duration
|
|
shutdownMutex sync.Mutex
|
|
shutdownDone chan struct{}
|
|
shutdownHooks []func()
|
|
}
|
|
|
|
// New creates a new App instance
|
|
func New(echo *echo.Echo) *App {
|
|
return &App{
|
|
echo: echo,
|
|
server: nil,
|
|
shutdownTimeout: 30 * time.Second,
|
|
shutdownDone: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
func (a *App) StartServer(addr string) error {
|
|
a.server = &http.Server{
|
|
Addr: addr,
|
|
Handler: a.echo,
|
|
}
|
|
|
|
// Start HTTP server in background
|
|
go func() {
|
|
if err := a.server.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatalf("Server failed to start: %v", err)
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
// Start begins all background services and blocks until shutdown signal
|
|
func (a *App) Start() error {
|
|
log.Println("Starting application lifecycle management...")
|
|
|
|
// Setup signal handling for graceful shutdown
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan,
|
|
syscall.SIGINT, // Ctrl+C
|
|
syscall.SIGTERM, // kill
|
|
syscall.SIGQUIT, // quit
|
|
)
|
|
|
|
// Wait for shutdown signal
|
|
sig := <-sigChan
|
|
log.Printf("Received signal: %v. Initiating graceful shutdown...", sig)
|
|
|
|
// Perform graceful shutdown
|
|
if err := a.Shutdown(); err != nil {
|
|
log.Printf("Error during shutdown: %v", err)
|
|
return err
|
|
}
|
|
|
|
log.Println("Application shutdown complete")
|
|
return nil
|
|
}
|
|
|
|
// Shutdown performs graceful shutdown of all services
|
|
func (a *App) Shutdown() error {
|
|
a.shutdownMutex.Lock()
|
|
defer a.shutdownMutex.Unlock()
|
|
|
|
select {
|
|
case <-a.shutdownDone:
|
|
log.Println("Shutdown already in progress")
|
|
return nil
|
|
default:
|
|
// Continue with shutdown
|
|
}
|
|
|
|
log.Println("Beginning graceful shutdown...")
|
|
|
|
// Create context with timeout
|
|
ctx, cancel := context.WithTimeout(context.Background(), a.shutdownTimeout)
|
|
defer cancel()
|
|
log.Println("Stopping HTTP Server...")
|
|
if a.server != nil {
|
|
if err := a.server.Shutdown(ctx); err != nil {
|
|
log.Printf("Error stopping HTTP server: %v", err)
|
|
}
|
|
}
|
|
|
|
// Run shutdown hooks: drain background workers, stop tickers, close files
|
|
for _, hook := range a.shutdownHooks {
|
|
hook()
|
|
}
|
|
|
|
close(a.shutdownDone)
|
|
log.Println("Graceful shutdown completed successfully")
|
|
return nil
|
|
}
|
|
|
|
// OnShutdown registers a hook to run during graceful shutdown, after the HTTP
|
|
// server has drained. Hooks run in registration order; register background
|
|
// workers, tickers, and file handles here.
|
|
func (a *App) OnShutdown(fn func()) {
|
|
a.shutdownHooks = append(a.shutdownHooks, fn)
|
|
}
|
|
|
|
// SetShutdownTimeout sets the maximum time to wait for graceful shutdown
|
|
func (a *App) SetShutdownTimeout(timeout time.Duration) {
|
|
a.shutdownTimeout = timeout
|
|
}
|
|
|
|
// ShutdownDone returns a channel that closes when shutdown is complete
|
|
func (a *App) ShutdownDone() <-chan struct{} {
|
|
return a.shutdownDone
|
|
}
|