Static Multi-server
A small Go HTTP server that serves multiple static projects configured via config/projects.json.
This README documents how to build, run, configure, and package the server using the repo layout in this project.
Quick build & run
Build the server binary from the cmd/server package:
cd cmd/server
go build -o ../../static-multiserverRun the server (defaults shown):
./static-multiserver -config config/projects.json -addr :8080Flags
-config(default:config/projects.json) — path to the projects JSON file.-addr(default::8080) — address to listen on (host:port or :port).
Docker
A multi-stage Dockerfile is provided. The runtime image includes git and jq and uses scripts/entrypoint.sh to clone project sources and produce a final runtime config.
Build and run with Docker:
# build image (run from repo root)
docker build -t static-multiserver:latest .
# run container, forwarding port 8080
docker run -p 8080:8080 static-multiserver:latestConfiguration
The configuration file is a JSON document (example: config/projects.json). Top-level fields:
$schema(optional) — schema reference.global_headers(optional) — map of headers applied to every response.projects— array of project objects.
Project fields
name(string) — unique project name (used for host subdomain matching and cookie value).source(string, optional) — git repo URL (used by the Docker entrypoint script).branch(string, optional) — git branch to checkout.dist_path(string, default:dist) — path on disk where built static files live.endpoint(string, optional) — URL path prefix for path-based routing (e.g./blog). Normalized to have a leading slash and no trailing slash; use""for the root project.index_file(string, default:index.html) — file served for directories and SPA fallbacks.spa(bool, default: false) — if true, unknown paths fall back toindex_file(Single Page App support).headers(object, optional) — map of headers applied to responses for this project.
Routing rules (priority order)
- Cookie: if the browser sends a
projectcookie, the server will prefer the project with that name and serve files from it. - Host-based: requests to
project.example.comwill map to the project withname: "project"(only triggers when host has a subdomain). - Path-based: requests to
example.com/<endpoint>/...map to the project withendpointconfigured (the endpoint prefix is stripped when serving files). - Root project: if none of the above match, the first project with an empty
endpointacts as the root project.
Security and behavior notes
- Global headers are applied first, then project headers — project headers can override global ones.
- The server protects against path traversal: requests resolving outside a project's
dist_pathreturn 404. - When serving a project's index file (or when SPA fallback is used), the server sets a
projectcookie so subsequent asset requests from the browser can be attributed to the correct project. - Common MIME types (JS, WASM, CSS, JSON) are registered by the server. Unknown extensions fall back to
application/octet-stream.
Example config
See config/projects.json in the repo for a minimal example with global headers and a single SPA project.
Troubleshooting
- If your static files are not found, ensure
dist_pathis correct and the files are present inside the container or on disk. - For Docker use, the entrypoint script clones repositories listed in
source/branchand expects the build output under each project'sdist_path.
License
MIT