Introduces code.nochebuena.dev/einherjar/spa-server — a container-first HTTP
server for single-page applications and progressive web apps. Produces a Docker
base image; downstream SPA Dockerfiles require only a single COPY instruction.
Interfaces and types (CT-6: one TypeSpec per file):
- Config — Port (EINHERJAR_SPA_PORT, default 8080) and StaticDir
(EINHERJAR_SPA_STATIC_DIR, default /srv/www); parsed via os.Getenv
- Server — implements lifecycle.Component; registered with launcher.New
Implementation:
- NewServer(logger, cfg) *Server — returns a lifecycle-managed HTTP server
- OnInit: builds http.NewServeMux with GET /health and /* routes
- OnStart: binds TCP listener synchronously via net.Listen (port conflicts
surface immediately), then serves in a background goroutine
- OnStop: graceful shutdown via http.Server.Shutdown with 10s timeout
spa.NewHandler(logger, staticDir) http.Handler:
- Uses http.Dir.Open for path sanitisation (prevents directory traversal)
- File exists and is not a directory → served by http.FileServer
- File missing or is a directory → serves index.html (SPA router owns all routes)
- Directory listing is disabled
health.NewHandler(logger) http.Handler:
- Always responds 200 {"status":"UP","components":{}}
- Wire format matches web/health.Response without importing the web module
- No external checks — process reachability is the only meaningful health signal
Config (EINHERJAR_SPA_* env vars):
Port(8080), StaticDir(/srv/www)
Docker:
- Multi-stage: golang:1.26-alpine builder → alpine:3.21 runtime
- Image published as:
code.nochebuena.dev/einherjar/spa-server:v1.0.0
code.nochebuena.dev/einherjar/spa-server:latest
- VOLUME ["/srv/www"] declared; consumer overrides via COPY or volume mount
No external dependencies beyond contracts, core, and the Go standard library.
43 lines
1.5 KiB
Go
43 lines
1.5 KiB
Go
// 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/
|
|
package spaserver
|