Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

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-multiserver

Run the server (defaults shown):

./static-multiserver -config config/projects.json -addr :8080

Flags

  • -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:latest

Configuration

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 to index_file (Single Page App support).
  • headers (object, optional) — map of headers applied to responses for this project.

Routing rules (priority order)

  1. Cookie: if the browser sends a project cookie, the server will prefer the project with that name and serve files from it.
  2. Host-based: requests to project.example.com will map to the project with name: "project" (only triggers when host has a subdomain).
  3. Path-based: requests to example.com/<endpoint>/... map to the project with endpoint configured (the endpoint prefix is stripped when serving files).
  4. Root project: if none of the above match, the first project with an empty endpoint acts 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_path return 404.
  • When serving a project's index file (or when SPA fallback is used), the server sets a project cookie 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_path is correct and the files are present inside the container or on disk.
  • For Docker use, the entrypoint script clones repositories listed in source/branch and expects the build output under each project's dist_path.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages