server

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 60 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 {
	// 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) InitForBatchLoad added in v0.3.0

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

InitForBatchLoad performs minimal initialization for cache batch load CLI. Sets root dir, opens DB pools, loads config, initializes HTTP cache, builds handler chain. No server start, no discovery, no worker pool.

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

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 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. The actual restart is triggered via the restart channel (restartCh) in server.go's event loop, not through this package.

func (*App) Run

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

func (*App) RunCacheBatchLoad added in v0.3.0

func (app *App) RunCacheBatchLoad() int

RunCacheBatchLoad runs the batch load and returns the exit code: 0 success, 1 error, 2 blocked.

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 drains the WriteBatcher first, then 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
	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) GobDecode added in v0.3.0

func (bw *BatchedWrite) GobDecode(data []byte) error

GobDecode deserializes BatchedWrite from the gob-safe wire format, reconstructing the files.File (including its Thumbnail) and cache entry from their separately-encoded blobs.

func (BatchedWrite) GobEncode added in v0.3.0

func (bw BatchedWrite) GobEncode() ([]byte, error)

GobEncode serializes BatchedWrite into a gob-safe wire format. Since files.File now has its own GobEncode/GobDecode handling the *bytes.Buffer Thumbnail, we can encode it directly without copying or mutating the caller's object.

func (BatchedWrite) Size

func (bw BatchedWrite) Size() int64

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

type Config deprecated

type Config = config.Config

Deprecated: Config is a type alias for config.Config. Use config.Config directly in new code.

func DefaultConfig deprecated

func DefaultConfig() *Config

Deprecated: DefaultConfig returns a Config with all default values. Use config.DefaultConfig directly in new code.

type ConfigDiff deprecated

type ConfigDiff = config.ConfigDiff

Deprecated: ConfigDiff is a type alias for config.ConfigDiff. Use config.ConfigDiff directly in new code.

type ConfigQueries deprecated

type ConfigQueries = config.ConfigQueries

Deprecated: ConfigQueries is a type alias for config.ConfigQueries. Use config.ConfigQueries directly in new code.

type ConfigSaver deprecated

type ConfigSaver = config.ConfigSaver

Deprecated: ConfigSaver is a type alias for config.ConfigSaver. Use config.ConfigSaver directly in new code.

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 cachebatch provides batch cache loading with bounded concurrency.
Package cachebatch provides batch cache loading with bounded concurrency.
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 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