Documentation
¶
Overview ¶
Package spaserver provides a lifecycle-managed HTTP server for serving single-page applications (SPA) and progressive web apps (PWA) from a container.
Overview ¶
The server delivers static assets from a configurable directory and falls back to index.html for any request path that does not map to an existing file — the standard SPA routing contract. A health endpoint is included at /health with the same JSON wire format used across all Einherjar modules.
Lifecycle ¶
NewServer returns a [lifecycle.Component] that integrates directly with [launcher.New]. Server.OnInit builds the HTTP mux. Server.OnStart begins serving in a goroutine. Server.OnStop performs a graceful shutdown.
cfg := spaserver.DefaultConfig()
srv := spaserver.NewServer(logger, cfg)
lc := launcher.New(logger)
lc.Append(srv)
if err := lc.Run(); err != nil {
log.Fatal(err)
}
Configuration ¶
All fields are read from environment variables:
- EINHERJAR_SPA_PORT — listen port (default: 8080)
- EINHERJAR_SPA_STATIC_DIR — path to the directory containing index.html and assets (default: /srv/www)
- EINHERJAR_LOG_LEVEL — log level: DEBUG, INFO, WARN, ERROR (default: INFO)
Container usage ¶
The module ships as a ready-to-use base image. A downstream SPA Dockerfile only needs to copy the built assets:
FROM code.nochebuena.dev/einherjar/spa-server:v1.0.0 COPY dist/ /srv/www/
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Port is the TCP port the server listens on.
Port int `env:"EINHERJAR_SPA_PORT" envDefault:"8080"`
// StaticDir is the filesystem path that contains index.html and all assets.
StaticDir string `env:"EINHERJAR_SPA_STATIC_DIR" envDefault:"/srv/www"`
}
Config holds the server configuration. All fields are populated from environment variables by DefaultConfig.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config populated from environment variables, falling back to safe defaults when variables are unset.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a lifecycle-managed HTTP server that serves a single-page application. Register with [launcher.New] via Append — it implements [lifecycle.Component].
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
spa-server
command
|
|
|
Package health provides the /health HTTP handler for spa-server.
|
Package health provides the /health HTTP handler for spa-server. |
|
Package spa provides the HTTP handler that serves a single-page application.
|
Package spa provides the HTTP handler that serves a single-page application. |