Server & Registry¶
Server¶
The Server is the main entry point for the framework. It holds the app registry and orchestrates the boot sequence.
Creating a Server¶
Apps are automatically sorted by their HasDependencies declarations — you can list them in any order.
Methods¶
NewServer¶
Creates a server and registers all given apps in order.
SetLayout¶
Configures the app layout function. Call before Run().
Registry¶
Returns the server's app registry for direct access.
Flags¶
Returns all CLI flags: core framework flags merged with flags from all Configurable apps. Pass a config source function to enable TOML file sourcing, or nil for CLI+ENV only.
Run¶
Boots and starts the server. This is a cli.ActionFunc — pass it directly to cli.Command.Action.
Boot Sequence¶
When Run() is called, the following happens in order:
- Parse config — reads CLI flags, env vars, and TOML into a
Configstruct - Open database — connects to SQLite with WAL mode, foreign keys, and connection pool
- Run migrations — calls
RunAppMigrationsfor everyMigratableapp - Register apps — calls
Register()on each app with the sharedAppConfig - Seed database — calls
Seed()on eachSeedableapp - Configure apps — calls
Configure()on eachConfigurableapp - Build templates — collects
.htmlfiles from allHasTemplatesapps and template functions from allHasFuncMapapps, parses them into a single global*template.Template - Create router — sets up Chi with core middleware (request logger, request ID, gzip, body limit)
- Inject context — injects nav items (from
HasNavItems), layout, and template executor into the request context via middleware - Register middleware — applies middleware from all
HasMiddlewareapps (including request-scopedHasRequestFuncMapinjection) - Register routes — calls
Routes()on allHasRoutesapps - Start HTTP server — listens on the configured address with graceful shutdown and zero-downtime restart via SIGHUP (see Deployment Guide)
Logging
The framework uses slog.Default() for all logging. Configure your preferred logger (text, JSON, tint, etc.) by calling slog.SetDefault() before starting the server.
Registry¶
The Registry manages registered apps and provides access to their capabilities.
Methods¶
Add¶
Registers an app. Panics on duplicate names or missing dependencies.
Get¶
Returns the app with the given name, or false if not found. Use with type assertions to access app-specific methods.
Apps¶
Returns all registered apps in registration order.
RunMigrations¶
Runs migrations for all Migratable apps.
AllNavItems¶
Collects and sorts nav items from all HasNavItems apps by position.
RegisterMiddleware¶
Applies middleware from all HasMiddleware apps to the router.
RegisterRoutes¶
Calls Routes() on all HasRoutes apps.
AllFlags¶
Collects CLI flags from all Configurable apps. Pass nil for CLI+ENV only.
Configure¶
Calls Configure() on each Configurable app.
AllCLICommands¶
Collects CLI subcommands from all HasCLICommands apps.
Seed¶
Calls Seed() on each Seedable app in order.
RenderTemplate¶
func RenderTemplate(w http.ResponseWriter, r *http.Request, statusCode int, name string, data map[string]any) error
Renders a named template into the HTTP response. If the request has an HX-Request header (htmx), the fragment is returned directly. Otherwise, it is wrapped in the layout function from context (if set).