server

package
v0.1.149 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 58 Imported by: 0

Documentation

Overview

Package server provides the core HTTP server, routing, middleware, and handlers for the web application. This file holds route registration.

Package server provides the core HTTP server, routing, middleware, and handlers for the web application. It integrates all the sub-packages like database, caching, and background workers to serve the photo gallery.

Index

Constants

View Source
const (
	// SQLiteDriverName is the name of the SQLite driver to use
	SQLiteDriverName = "sqlite3"
)

Variables

This section is empty.

Functions

func MakeAuthCookie

func MakeAuthCookie(t *testing.T, app *App) *http.Cookie

MakeAuthCookie creates an authenticated session cookie for testing. Includes a stable CSRF token to ensure consistent HTML output across requests.

Types

type App

type App struct {

	// ImporterFactory allows tests or callers to override how Importer instances are constructed.
	ImporterFactory func(conn *sql.Conn, q *gallerydb.CustomQueries) files.Importer
	// contains filtered or unexported fields
}

App holds the shared state and resources for the entire application. It manages database connections, worker pools, queues, caching, application context, and a task scheduler for recurring and one-time tasks.

Lock Ordering: To prevent deadlocks, always acquire locks in this order when holding multiple locks: 1. ctxMu 2. configMu 3. restartMu Never acquire a lower-ordered lock while holding a higher-ordered one.

func CreateApp

func CreateApp(t *testing.T, startPool bool) *App

CreateApp sets up a full, isolated application instance for testing.

func CreateAppWithOpt

func CreateAppWithOpt(tb testing.TB, startPool bool, opt getopt.Opt) *App

CreateAppWithOpt sets up a full, isolated application instance for testing with custom options. It creates and wires services in order: ConfigService (setDB), FileProcessor, session/store/restartCh (ensureSessionAndRestart), Handlers (buildHandlers). All services are non-nil when CreateApp returns.

func CreateAppWithRoot

func CreateAppWithRoot(t *testing.T, startPool bool, rootDir string) *App

CreateAppWithRoot sets up a full application instance using an existing root directory. Useful for simulating restarts while preserving the same database.

func CreateAppWithRootAndOpt

func CreateAppWithRootAndOpt(t *testing.T, startPool bool, rootDir string, opt getopt.Opt) *App

CreateAppWithRootAndOpt sets up a full application instance using an existing root directory and options.

func CreateAppWithTB

func CreateAppWithTB(tb testing.TB, startPool bool) *App

CreateAppWithTB sets up a full, isolated application instance for benchmarks or tests. Use CreateApp for regular tests; use CreateAppWithTB(b, false) in benchmarks.

func New

func New(opt getopt.Opt, version string) *App

New creates and initializes a new App instance. It sets up the application context, session secret, importer factory, and other core components.

func (*App) GetETagVersion

func (app *App) GetETagVersion() string

GetETagVersion returns the current ETag version for cache-busting URLs. Thread-safe: acquires configMu.RLock to read app.config.ETagVersion.

func (*App) IncrementETag

func (app *App) IncrementETag() (string, error)

IncrementETag loads current ETag, increments it, saves to database, and returns new value.

func (*App) InitForIncrementETag

func (app *App) InitForIncrementETag(opt getopt.Opt) error

InitForIncrementETag initializes minimal app state for --increment-etag command. Similar to InitForUnlock, this sets up only what's needed for ETag operations.

func (*App) InitForUnlock

func (app *App) InitForUnlock() error

InitForUnlock performs minimal initialization needed for unlock operations. It sets up root directory and database using the database package. This is a minimal initialization that does not require config to be loaded.

func (*App) LogProfileLocation

func (app *App) LogProfileLocation()

LogProfileLocation logs the profile directory and stops the profiler if active. This should be called before shutdown to ensure profile location is logged to both console and file.

func (*App) RestartRequired

func (app *App) RestartRequired() bool

RestartRequired returns true if a server restart is required due to configuration changes that require restart.

func (*App) RestartServer

func (app *App) RestartServer(server *http.Server) error

RestartServer gracefully shuts down the HTTP server and restarts it. If only listener settings (address/port) changed, it performs an HTTP-only restart. Otherwise, it performs a full restart.

func (*App) Run

func (app *App) Run(minPoolWorkers, maxPoolWorkers int) error

Run orchestrates the application startup sequence. It initializes the root directory, logging, database, configuration, and command-line parsing. It then starts the background worker pool and file discovery process before starting the HTTP server.

func (*App) Serve

func (app *App) Serve() error

Serve initializes the session store and starts the HTTP server on the configured port. It supports graceful restart via the restart channel.

func (*App) Shutdown

func (app *App) Shutdown()

Shutdown gracefully shuts down the application. It cancels the main context, waits for background goroutines and the worker pool to finish, closes database connections, and closes the log file.

func (*App) UnlockAccount

func (app *App) UnlockAccount(username string) error

UnlockAccount unlocks a locked account by clearing failed attempts and removing the lockout.

type BatchedWrite

type BatchedWrite struct {
	File        *files.File                        // File metadata + EXIF + thumbnails
	InvalidFile *gallerydb.UpsertInvalidFileParams // Invalid file tracking
	CacheEntry  *cachelite.HTTPCacheEntry          // HTTP cache entries
}

BatchedWrite is a union type for all high-volume database writes. Exactly one field should be non-nil per instance.

func (BatchedWrite) Size

func (bw BatchedWrite) Size() int64

Size returns estimated memory cost in bytes for batch size limiting.

type Config

type Config = config.Config

Config is the main application configuration type. It is defined in the config package and re-exported here for backward compatibility.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with all default values.

type ConfigDiff

type ConfigDiff = config.ConfigDiff

ConfigDiff is an alias for config.ConfigDiff to maintain backward compatibility.

type ConfigQueries

type ConfigQueries = config.ConfigQueries

ConfigQueries is an alias for config.ConfigQueries to maintain backward compatibility.

type ConfigSaver

type ConfigSaver = config.ConfigSaver

ConfigSaver is an alias for config.ConfigSaver to maintain backward compatibility.

type GalleryStats

type GalleryStats struct {
	Folders        string
	Images         string
	ImagesSize     int64
	FirstDiscovery string
	LastDiscovery  string
}

GalleryStats holds statistics about the gallery for display in the about modal.

type MemoryReclaimerConfig

type MemoryReclaimerConfig struct {
	InitialDelay  time.Duration
	CheckInterval time.Duration
	IdleThreshold time.Duration
	FreeMemFunc   func()
}

MemoryReclaimerConfig holds the configuration for the memory reclaimer.

Directories

Path Synopsis
Package auth provides authentication services for the application.
Package auth provides authentication services for the application.
Package cachepreload provides cache preloading when folders are opened.
Package cachepreload provides cache preloading when folders are opened.
Package compress provides pure functions for content encoding negotiation and compression decision making.
Package compress provides pure functions for content encoding negotiation and compression decision making.
Package conditional provides pure functions for HTTP conditional request matching.
Package conditional provides pure functions for HTTP conditional request matching.
Package files provides file discovery, metadata extraction, and thumbnail generation for the photo gallery.
Package files provides file discovery, metadata extraction, and thumbnail generation for the photo gallery.
Package handlers provides HTTP request handlers for the web application.
Package handlers provides HTTP request handlers for the web application.
Package interfaces holds shared contracts consumed by both the server orchestrator (App) and the handlers package.
Package interfaces holds shared contracts consumed by both the server orchestrator (App) and the handlers package.
Package metrics provides centralized metrics collection for the dashboard.
Package metrics provides centralized metrics collection for the dashboard.
Package pathutil provides path manipulation utilities for the server package.
Package pathutil provides path manipulation utilities for the server package.
Package restart provides pure functions for determining server restart requirements.
Package restart provides pure functions for determining server restart requirements.
Package security provides pure functions for security and lockout calculations.
Package security provides pure functions for security and lockout calculations.
Package session provides session store, CSRF token handling, and session cookie options for the web application.
Package session provides session store, CSRF token handling, and session cookie options for the web application.
Package template provides pure functions for building template data maps.
Package template provides pure functions for building template data maps.
Package ui provides HTML template rendering and cache version management for the application's user interface.
Package ui provides HTML template rendering and cache version management for the application's user interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL