Server entry point
Read datapages first for the build loop, hard rules and naming conventions.
On its first run, datapages gen writes the server command's main.go. It does not regenerate that file on later runs.
NewServer
datapages gen reads the type arguments to find the app package and output directory.
s, err := datapages.NewServer[
app.App, // App
app.SessionData, // or datapages.DisableSessions
datapages.DisablePrometheus, // or datapages.EnablePrometheus
datapagesgen.Server, // the datapagesgen of that app package
](&a, broker, opts...) // the app is passed by pointer
Keep the call inside the module. Import datapages with a package name because the scanner rejects dot imports. Generated code is always in datapagesgen directly below its app package. 1 module can contain several apps. Run one with datapages watch --app app/frontend, the directory of its app package.
datapages.EnablePrometheus requires WithPrometheus, DisablePrometheus rejects it.
Broker
A broker is always required. It sends events between server instances and to SSE subscribers. Use modules/messaging/natscore for multiple instances. Use modules/messaging/inmem only for 1 instance. The generated server uses NATS. Start it with make up before starting the server.
Options
var opts []datapages.ServerOption
opts = append(opts,
datapages.WithLogger(slog.Default()),
datapages.WithMiddleware(mw),
datapages.WithSessions(datapages.SessionsConfig{}),
datapages.WithSessionManager[app.SessionData](mgr),
datapages.WithAssets(app.StaticFS, false),
datapages.WithHTTPServer(&http.Server{ReadHeaderTimeout: 10 * time.Second}),
datapages.WithDatastarJS("https://cdn.example.com/datastar@1.0.5/datastar.js"),
datapages.WithShutdownTimeout(30*time.Second),
datapages.WithFileSendTimeout(time.Minute),
datapages.WithAssetsCache(datapages.AssetsCacheConfig{}),
datapages.WithBodySizeLimit(1<<20),
datapages.WithLogSampling(datapages.LogSamplingConfig{Interval: time.Minute}),
datapages.WithSSECompression(datapages.SSECompressionConfig{}),
datapages.WithResponseCompression(datapages.ResponseCompressionConfig{}),
datapages.WithPrometheus(datapages.PrometheusConfig{Host: ":9091"}),
)
WithBodySizeLimit limits the request body of an action that declares Signals. The default is 1 MiB. An over-limit request returns 400 while reading signals. A handler that reads r.Body itself gets no limit: wrap the body in http.MaxBytesReader. WithLogSampling limits repeated framework warnings, not application logs. WithHTTPServer uses every supplied field except Addr and Handler. Keep WriteTimeout at 0 because a nonzero value ends long-lived SSE streams. WithFileSendTimeout ends a blocked file response after 1 minute by default. WithSSECompression sets the codings, order, levels and windows; the default order is zstd, brotli and gzip. CPU and memory scale with stream count. Use the presets datapages.SSECompressionLowMemory() for many streams with fragments below about 100 KB, datapages.SSECompressionLowBandwidth() for few streams with slow or metered clients, and datapages.SSECompressionLargeFragments() for fragments above about 1 MB. WithNoSSECompression turns compression off. A page's StreamConfig overrides it for the page's streams, and a StreamConfig method named after an action, such as StreamConfigPOSTSave, for that action's response. Use WithNoSSECompression in an entry point that only loopback clients reach, such as a desktop app whose webview loads from a server on 127.0.0.1. Compression there costs CPU and saves no transfer.
WithResponseCompression sets the codings, order, levels and minimum size for page and action bodies. Bodies of at least 256 bytes use zstd, brotli or gzip by default. ResponseCompressionLowCPU reduces CPU and memory. ResponseCompressionLowBandwidth prefers brotli. Static assets use WithAssetsCompression. datapages.File and SSE responses pass through, as do responses with Content-Encoding or Cache-Control: no-transform. A HEAD response gets the headers of its GET response. Compression runs after every middleware and is disabled in dev mode. For loopback-only clients, add WithNoResponseCompression, WithNoSSECompression and WithNoAssetsCompression. Before a proxy body filter such as nginx sub_filter, add the ones for the responses it edits. Compressing proxies pass encoded bodies through. Set Cache-Control: private, no-transform on pages that combine secrets with attacker-controlled input, and on their _$/ streams, or give such a page a StreamConfig that returns Compression: &datapages.SSECompressionConfig{Disabled: true}.
WithPrometheus starts a second HTTP server that serves /metrics on the configured host. WithShutdownTimeout limits how long ListenAndServe waits after context cancellation for requests, SSE streams and StreamClose hooks. When the timeout expires, Datapages logs the shutdown error and returns. Cancel the context on SIGTERM as well as SIGINT with signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM). Docker, Kubernetes and systemd stop a process with SIGTERM, which otherwise ends it without a graceful shutdown.
Deployments need no application code. Each page sends its build ID with Datastar requests. A server with a different ID returns 205 Reset Content without running the handler. The page then reloads. Shutdown resets open SSE streams. Datastar then reconnects to the next process. The ID defaults to a hash of the executable. Set WithBuildID when replicas use different binaries for 1 release. During a rolling deployment, route each client to 1 build.
The session cookie uses the Secure attribute. Set DisableSecureCookie only when the complete deployment uses plain HTTP, where the browser would reject the secure cookie. datapages.IsDevMode() reports whether the dev server is active. DATAPAGES_DEV_MODE and TEMPL_DEV_MODE enable development behavior. Log at slog.LevelDebug when you need to inspect that behavior.
Declaring PageOffline generates datapagesgen.WithOffline(offline.Config{...}). The option serves the service worker. See datapages-offline.
If WithMiddleware adds a Content-Security-Policy, it must allow script-src 'unsafe-inline' 'unsafe-eval'. Datapages writes the build script, the CSRF script and the instance ID script inline. Datastar compiles every data-* expression at run time.
WithCSPNonce(func(r *http.Request) string) replaces both allowances with a nonce. The application mints the nonce and puts it in its own policy header. Datapages reads it back, writes it on the html element as data-nonce and on every script it writes. Mint the nonce in middleware and store it in the request context: Datapages calls the function several times per response and every call with the same request must return the same value. data-nonce turns on Datastar's CSP mode, which compiles expressions through a nonced script element instead of Function. It needs Datastar 1.0.3 or later. The nonce must differ per response.
WithOffline passes the nonce to the offline module regardless of option order. The nonce cannot reach a page served from the service worker's cache; see datapages-offline.
s.ListenAndServe(ctx, "localhost:8080")
s.ListenAndServeTLS(ctx, "localhost:8443", certPath, keyPath)
Static assets
An embed.FS in the app package enables file serving. Its doc comment defines the URL prefix. Its directive defines the source directory.
// StaticFS is /static/
//go:embed static/*
var StaticFS embed.FS
Declare at most 1 such variable in an app package. The URL prefix must start and end with / and must not be /. It may contain only ASCII letters, digits, -, ., _, ~ and /: the server matches it against the decoded request path, which a percent-encoded prefix never matches. The directive must name exactly 1 directory inside the app package. No page or action route may start with the URL prefix: every path below it belongs to the files.
datapages.WithAssets(app.StaticFS, false) supplies the filesystem and the directory-browsing setting. Generated code supplies the URL prefix, source directory and development disk path. In development mode, Datapages reads files from disk and disables caching. Asset changes then need no rebuild. The server rejects WithAssets when the app declares no assets.
WithAssetsFS accepts an http.FileSystem and overrides WithAssets. Datapages uses that filesystem as-is: it does not extract the declared source directory or replace it in development mode. The app's declared URL prefix still determines where files are served. Without an asset declaration, the option is accepted but no asset route is registered.
Without WithAssetsCache, assets carry Cache-Control: public, max-age=0 and an ETag, the headers of a zero AssetsCacheConfig. The browser asks before every reuse, and an unchanged file returns 304 with an empty body.
Set MaxAge only when an asset URL changes with its content, such as when the file name contains a build hash or the version of a vendored library, as in datastar-1.0.5.js. Otherwise a browser can use stale content until the age expires. Immutable prevents reloads from revalidating a fresh response and requires a positive MaxAge. CacheControl sets the header directly and cannot be combined with MaxAge or Immutable. DisableETag omits the ETag. Disabled leaves the headers of a server without WithAssetsCache.
The server computes an ETag on a file's first request and keeps it until the process exits, since assets must not change while it runs. A file that changes anyway keeps its old ETag, and browsers may get its old compressed content, until a restart. Serve files that change through a GET action that returns datapages.File. Development mode ignores this option and always sends Cache-Control: no-cache without Last-Modified.
Assets of at least 256 bytes use brotli 6 or gzip 9 by default when their type is text, HTML included, JSON, JavaScript, XML, YAML, TOML, SVG, WebAssembly, a TrueType or OpenType font, an icon or a BMP image. The first request compresses and caches its copy. Per 200 KB, brotli 6 takes a few milliseconds and brotli 11 takes 140 to 215 ms. To avoid this delay, create the compressor with assetcompress.New, pass it to WithAssetsCompression, and run assets.Precompress(ctx, app.StaticFS) in a goroutine that logs its error, or before ListenAndServe. With Levels.BrotliQuality 11, run Precompress before ListenAndServe, since a request that finds no copy waits for its brotli 11 compression. Copies are keyed by content, not path, which is why the whole embed.FS precompresses the files the server serves from its subdirectory. assetcompress.Config sets codings, levels and storage; modules/assetcompress/disk persists copies. WithNoAssetsCompression turns it off, and a CacheControl with no-transform keeps the assets uncompressed. Dev mode disables asset compression.
Use assets.Path("style.css") from the generated assets package to reference a file. Paths match file names exactly, case included, on every system and in dev mode. Use href.Asset("style.css") inside an <a href>. The linter checks app-internal URLs in <a href> and Datastar action attributes. It does not check asset URLs in <link> or <script> elements.
datapages.yaml
cmd names the command to create when there is no NewServer call. It defaults to cmd/server. watch configures datapages watch:
| key under watch | use |
| ----------------- | --- |
| app-host, proxy-timeout, debounce | dev server URL and timing |
| format, lint | format or lint during rebuilds |
| exclude, watcher-ignore | paths excluded from source scanning or file watching |
| flags, dir-work | command flags and working directory |
| log.level, log.clear-on, log.print-js-debug-logs | watcher output |
| tls.cert, tls.key | certificate and key for dev-server TLS |
| compiler.tags, compiler.env | Go build tags and environment; compiler also accepts Go compiler flags |
| custom-watchers | extra watchers with include/exclude patterns, command and rebuild action |
| toxiproxy | the second address that serves the app through Toxiproxy with network faults; disabled: true turns it off |
See datapages watch --help for CLI flags.
<!-- written by datapages sha256:e08be075e57994f8 -->