### Build and run custom SPA image Source: https://github.com/devforth/spa-to-http/blob/main/docs/getting-started.md Commands to build the custom Docker image defined in the Dockerfile and execute it locally. ```bash docker build -t my-spa . docker run --rm -p 8080:8080 my-spa ``` -------------------------------- ### Run SPA with Basic Authentication Source: https://github.com/devforth/spa-to-http/blob/main/docs/getting-started.md Configures the spa-to-http container to require Basic Authentication for access. Uses environment variables to define credentials and the authentication realm. ```bash docker run --rm -p 8080:8080 \ -e BASIC_AUTH="admin:secret" \ -e BASIC_AUTH_REALM="SPA Server" \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest ``` -------------------------------- ### Configuring via Environment Variables Source: https://context7.com/devforth/spa-to-http/llms.txt Example of running the containerized application using environment variables for full configuration control. ```bash docker run --rm -p 8080:8080 \ -e ADDRESS=0.0.0.0 \ -e PORT=8080 \ -e GZIP=true \ -e BROTLI=true \ -e THRESHOLD=1024 \ -e DIRECTORY=/code \ -e BASE_PATH=/ \ -e CACHE_MAX_AGE=604800 \ -e SPA_MODE=true \ -e IGNORE_CACHE_CONTROL_PATHS=/sw.js,/manifest.json \ -e CACHE=true \ -e CACHE_BUFFER=51200 \ -e LOGGER=true \ -e LOG_PRETTY=false \ -e BASIC_AUTH=admin:secret \ -e BASIC_AUTH_REALM=Protected \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest ``` -------------------------------- ### Quick Start SPA Deployment with Docker Source: https://context7.com/devforth/spa-to-http/llms.txt Deploy a static SPA bundle by mounting the distribution directory into the container. The server automatically handles SPA routing by serving index.html for all unmatched paths. ```bash docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest curl -i http://localhost:8080/ curl -i http://localhost:8080/some/spa/route curl -i http://localhost:8080/assets/main.js ``` -------------------------------- ### Run SPA locally with Docker Source: https://github.com/devforth/spa-to-http/blob/main/docs/getting-started.md Serves a local SPA build directory at a specified port using the spa-to-http container. Requires a mapped volume containing the built SPA files. ```bash docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest ``` -------------------------------- ### Verify Cache Headers Source: https://context7.com/devforth/spa-to-http/llms.txt Demonstrates how to verify the cache control headers set by spa-to-http for different file types. It shows examples for JavaScript files (with caching enabled) and index.html (with no-store enforced). ```bash # Verify cache headers curl -i http://localhost:8080/assets/app.abc123.js # Response includes: Cache-Control: max-age=604800 curl -i http://localhost:8080/index.html # Response includes: Cache-Control: no-store ``` -------------------------------- ### Traefik + Docker Compose Example for spa-to-http Source: https://github.com/devforth/spa-to-http/blob/main/docs/deployment.md This example demonstrates how to configure Traefik and spa-to-http within a Docker Compose setup. It exposes the spa-to-http service on port 8080 and configures Traefik to route requests based on the Host header for 'spa.localhost'. ```yaml version: "3.3" services: traefik: image: "traefik:v2.7" command: - "--providers.docker=true" - "--providers.docker.exposedbydefault=false" - "--entrypoints.web.address=:80" ports: - "80:80" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" spa: image: devforth/spa-to-http:latest labels: - "traefik.enable=true" - "traefik.http.routers.spa.rule=Host(`spa.localhost`)" - "traefik.http.services.spa.loadbalancer.server.port=8080" ``` -------------------------------- ### Multi-stage Dockerfile for SPA deployment Source: https://github.com/devforth/spa-to-http/blob/main/docs/getting-started.md A Dockerfile pattern that builds an SPA using Node.js and copies the resulting artifacts into the spa-to-http runtime image. This ensures a clean and minimal production image. ```dockerfile FROM node:20-alpine AS builder WORKDIR /code COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM devforth/spa-to-http:latest COPY --from=builder /code/dist/ . ``` -------------------------------- ### Host SPA on a Subpath with spa-to-http Source: https://github.com/devforth/spa-to-http/blob/main/README.md This command demonstrates how to configure spa-to-http to serve the SPA from a specific subpath, in this case, '/app'. The `--base-path /app` flag ensures that requests starting with '/app' are correctly routed to the SPA's root directory. ```bash docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --base-path /app ``` -------------------------------- ### Docker Compose with Traefik for Production Source: https://context7.com/devforth/spa-to-http/llms.txt Deploy spa-to-http behind a Traefik reverse proxy using Docker Compose. This setup enables automatic service discovery and load balancing, suitable for production environments. Traefik is configured to route traffic to the spa-to-http service based on the hostname. ```yaml # docker-compose.yml version: "3.3" services: traefik: image: "traefik:v2.7" command: - "--providers.docker=true" - "--providers.docker.exposedbydefault=false" - "--entrypoints.web.address=:80" ports: - "80:80" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" spa: image: devforth/spa-to-http:latest command: --brotli --cache-max-age 604800 volumes: - ./dist:/code labels: - "traefik.enable=true" - "traefik.http.routers.spa.rule=Host(`spa.localhost`)" - "traefik.http.services.spa.loadbalancer.server.port=8080" ``` ```bash # Start the stack docker compose up -d # Test through Traefik curl -H "Host: spa.localhost" http://localhost/ ``` -------------------------------- ### Building and Running from Source Source: https://context7.com/devforth/spa-to-http/llms.txt Commands to clone, build, and execute the spa-to-http binary locally with various configuration options. ```bash git clone https://github.com/devforth/spa-to-http.git cd spa-to-http/src go build -o spa-to-http ./spa-to-http --directory ../test/frontend/dist ./spa-to-http \ --directory ../test/frontend/dist \ --brotli \ --gzip \ --spa=true \ --logger \ --log-pretty \ --cache-max-age 3600 \ --threshold 2048 \ --base-path /app go run . --directory ../test/frontend/dist --logger --log-pretty ``` -------------------------------- ### Project Directory Structure Source: https://github.com/devforth/spa-to-http/blob/main/docs/architecture.md The project follows a modular Go structure. It separates the entry point, HTTP server logic, configuration parsing, and utility functions into distinct packages within the src directory. ```text ./src ./src/main.go # Entry point and wiring ./src/app # HTTP server and handlers ./src/param # CLI and environment parsing ./src/util # Small shared helpers ``` -------------------------------- ### Deploying with Docker Compose and Traefik Source: https://context7.com/devforth/spa-to-http/llms.txt A docker-compose configuration demonstrating how to serve an SPA on a subpath using Traefik as a reverse proxy. ```yaml version: "3.3" services: traefik: image: "traefik:v2.7" command: - "--providers.docker=true" - "--providers.docker.exposedbydefault=false" - "--entrypoints.web.address=:80" ports: - "80:80" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" spa: image: devforth/spa-to-http:latest command: --base-path /app --brotli volumes: - ./dist:/code labels: - "traefik.enable=true" - "traefik.http.routers.spa.rule=PathPrefix(`/app`)" - "traefik.http.services.spa.loadbalancer.server.port=8080" api: image: "traefik/whoami" labels: - "traefik.enable=true" - "traefik.http.routers.api.rule=PathPrefix(`/api`)" ``` -------------------------------- ### Configure Server Address and Port Source: https://context7.com/devforth/spa-to-http/llms.txt Customize the server's listening port and network interface using either CLI flags or environment variables. ```bash # Using CLI flags docker run --rm -p 8082:8082 -v $(pwd)/dist:/code devforth/spa-to-http:latest --port 8082 --address 0.0.0.0 # Using environment variables docker run --rm -p 3000:3000 -e PORT=3000 -e ADDRESS=0.0.0.0 -v $(pwd)/dist:/code devforth/spa-to-http:latest ``` -------------------------------- ### Configure Brotli and Gzip Compression Source: https://context7.com/devforth/spa-to-http/llms.txt Enable on-the-fly compression for static assets to improve load times. You can specify compression types, set size thresholds, and exclude specific file extensions. ```bash docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest --brotli --gzip --threshold 500 --no-compress .woff2 --no-compress .png curl -H "Accept-Encoding: br" -i http://localhost:8080/assets/app.js ``` -------------------------------- ### Multi-stage Dockerfile Build Pattern Source: https://context7.com/devforth/spa-to-http/llms.txt Create an optimized production image by building the SPA in a Node.js environment and copying the resulting assets into the spa-to-http runtime image. ```dockerfile FROM node:20-alpine AS builder WORKDIR /code COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM devforth/spa-to-http:latest COPY --from=builder /code/dist/ . ``` ```bash docker build -t my-spa . docker run --rm -p 8080:8080 my-spa curl -i http://localhost:8080/ ``` -------------------------------- ### Configure Subpath Hosting Source: https://context7.com/devforth/spa-to-http/llms.txt Use the --base-path flag to serve the application under a specific URL prefix, which is useful when deploying behind a reverse proxy. ```bash docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest --base-path /app curl -i http://localhost:8080/app curl -i http://localhost:8080/app/assets/logo.png ``` -------------------------------- ### Run spa-to-http with Custom Port and Brotli Compression Source: https://github.com/devforth/spa-to-http/blob/main/README.md This command illustrates how to run spa-to-http with a custom port (8082) and enable Brotli compression. The `--brotli` flag activates compression, and `--port 8082` specifies the listening port for the server. ```bash docker run --rm -p 8082:8082 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --brotli --port 8082 ``` -------------------------------- ### Serve SPA Dist Folder with Docker Source: https://github.com/devforth/spa-to-http/blob/main/README.md This command demonstrates how to run the spa-to-http Docker image to serve a local 'dist' folder. It maps port 8080 on the host to port 8080 in the container and mounts the local 'dist' directory to '/code' inside the container, making the SPA files accessible. ```bash # Serve ./dist at http://localhost:8080 docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest ``` -------------------------------- ### spa-to-http Subpath Deployment Configuration Source: https://github.com/devforth/spa-to-http/blob/main/docs/deployment.md This configuration snippet shows how to deploy spa-to-http when it's mounted under a subpath, such as '/app', using the --base-path command-line argument. This ensures correct routing for index.html and assets within the subpath. ```yaml services: spa: image: devforth/spa-to-http:latest command: --base-path /app ``` -------------------------------- ### Enable HTTP Basic Authentication Source: https://context7.com/devforth/spa-to-http/llms.txt Protect your SPA with HTTP Basic Authentication. This feature is useful for staging environments or private applications. Always use HTTPS in production when Basic Auth is enabled. Authentication can be configured via environment variables or CLI flags. ```bash # Enable Basic Auth with Docker (environment variable) docker run --rm -p 8080:8080 \ -e BASIC_AUTH="admin:secret" \ -e BASIC_AUTH_REALM="SPA Server" \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest # Using CLI flags docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --basic-auth "admin:secret" \ --basic-auth-realm "Protected Area" # Test authentication curl -i http://localhost:8080/ # Returns: 401 Unauthorized with WWW-Authenticate header curl -i -u admin:secret http://localhost:8080/ # Returns: 200 OK with the SPA content ``` -------------------------------- ### Configure Cache-Control Headers Source: https://context7.com/devforth/spa-to-http/llms.txt Adjust the cache-max-age setting to control how long browsers and CDNs cache assets. Set to -1 to disable caching entirely. ```bash docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest --cache-max-age 3600 docker run --rm -p 8080:8080 -v $(pwd)/dist:/code devforth/spa-to-http:latest --cache-max-age -1 ``` -------------------------------- ### Toggle SPA Mode Source: https://context7.com/devforth/spa-to-http/llms.txt Control whether spa-to-http behaves as a Single Page Application server (falling back to index.html for missing paths) or a static file server (returning 404 for missing paths). This can be set via CLI flags or environment variables. ```bash # SPA mode enabled (default) - missing paths serve index.html docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --spa=true # Static mode - missing paths return 404 docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --spa=false # Using environment variable docker run --rm -p 8080:8080 \ -e SPA_MODE=false \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest # Test SPA mode behavior curl -i http://localhost:8080/nonexistent/route # With --spa=true: 200 OK (index.html) # With --spa=false: 404 Not Found ``` -------------------------------- ### Enable Request Logging Source: https://context7.com/devforth/spa-to-http/llms.txt Configure structured request logging in JSON or human-readable format for debugging and monitoring. You can enable logging via an environment variable or CLI flag, and control the log level. JSON is the default format. ```bash # Enable JSON logging (default format) docker run --rm -p 8080:8080 \ -e LOGGER=true \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest # Enable pretty-printed logs for development docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --logger --log-pretty # Set log level via environment docker run --rm -p 8080:8080 \ -e LOGGER=true \ -e LOG_LEVEL=debug \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest # Sample log output (JSON format): # {"time":"2024-01-15T10:30:00Z","level":"INFO","msg":"request","method":"GET","path":"/","status":200,"duration":"1.234ms"} ``` -------------------------------- ### Subpath Deployment with Traefik Source: https://context7.com/devforth/spa-to-http/llms.txt Configure spa-to-http to serve your SPA at a specific subpath (e.g., `/app`) while Traefik handles routing for other paths. This allows multiple applications to be served from the same domain or IP address. ```yaml # docker-compose.yml snippet for subpath deployment # ... (traefik service definition remains the same) services: # ... traefik service definition ... spa: image: devforth/spa-to-http:latest command: --brotli --cache-max-age 604800 volumes: - ./dist:/code labels: - "traefik.enable=true" - "traefik.http.routers.spa.rule=Host(`yourdomain.com`) && PathPrefix(`/app`)" - "traefik.http.routers.spa.middlewares=strip-app-prefix" - "traefik.http.middlewares.strip-app-prefix.stripprefix.prefixes=/app" - "traefik.http.services.spa.loadbalancer.server.port=8080" ``` -------------------------------- ### Configure In-Memory File Cache Source: https://context7.com/devforth/spa-to-http/llms.txt Optimize repeated file access by configuring the LRU file cache. This feature stores file contents in memory to reduce disk I/O. You can enable or disable the cache and set a custom buffer size for cached files. Configuration can be done via CLI flags or environment variables. ```bash # Enable cache with custom buffer size (100KB) docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --cache --cache-buffer 102400 # Disable file caching (for development or low-memory environments) docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --cache=false # Using environment variables docker run --rm -p 8080:8080 \ -e CACHE=true \ -e CACHE_BUFFER=51200 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest ``` -------------------------------- ### Force No-Store Cache for Specific Paths Source: https://context7.com/devforth/spa-to-http/llms.txt Configure spa-to-http to ignore cache control headers for specified paths, such as service workers and manifest files. This ensures that these critical assets are always fetched fresh from the server. The `--ignore-cache-control-paths` flag accepts a comma-separated list of paths. ```bash docker run --rm -p 8080:8080 \ -v $(pwd)/dist:/code \ devforth/spa-to-http:latest \ --ignore-cache-control-paths "/sw.js,/manifest.json" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.