Serve a Static Site

This is a useful approach for using FastAPI and Uvicorn to serve a static website, such as one built with Hugo or MkDocs. It’s deliberately kept to a single Python file, and is easy to run in a container.

Create a project directory and a new Python virtual environment:

mkdir ~/project && cd ~/project
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip

Add the following requirements file:

non_ver_reqs.txttext
fastapi
uvicorn[standard]
pydantic-settings

Install them, then freeze the requirements:

pip install -r non_ver_reqs.txt
pip freeze > requirements.txt

Add the following Python file:

serve.pypython
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    CONF_MAIN_APP_HOST: str = "0.0.0.0"
    CONF_MAIN_APP_PORT: int = 8080
    CONF_DEBUG_LEVEL: str = "info"
    CONF_SSL_KEYFILE: str | None = None
    CONF_SSL_CERTFILE: str | None = None

settings = Settings()

app = FastAPI(
    title="FastAPI Static Web Server",
    description="A simple single file Python FastAPI application for serving a static web site.",
    version="1.0.0",
    openapi_url=None,
)

# Serve everything in ./site, with index.html for directories
app.mount('/', StaticFiles(directory="site", html=True), name="site")

if __name__ == '__main__':
    # TLS is used if both the key and certificate are set
    uvicorn.run(app,
                host=settings.CONF_MAIN_APP_HOST,
                port=settings.CONF_MAIN_APP_PORT,
                log_level=settings.CONF_DEBUG_LEVEL,
                ssl_keyfile=settings.CONF_SSL_KEYFILE,
                ssl_certfile=settings.CONF_SSL_CERTFILE,
                proxy_headers=True,
                forwarded_allow_ips="*")
  • StaticFiles(directory="site", html=True) serves the files in the site directory, and with html=True, serves index.html for a directory and 404.html (if there is one) for pages that don’t exist.
  • openapi_url=None turns off the API docs, which a static site doesn’t need.
  • TLS is optional: Uvicorn uses it when both CONF_SSL_KEYFILE and CONF_SSL_CERTFILE are set. Behind a reverse proxy or on OpenShift, leave them unset, and proxy_headers makes Uvicorn trust the proxy’s X-Forwarded-* headers.

Add a test index.html:

mkdir site
site/index.htmlhtml
<html>
  <body>
    <p>Hello World!</p>
  </body>
</html>

And run the single file Python application:

python serve.py

The site is at http://localhost:8080. Settings can be changed with environment variables, for example CONF_MAIN_APP_PORT=9000 python serve.py.

In a container#

A Containerfile, using the same Red Hat UBI image as the OpenShift Deployment section:

Containerfiledockerfile
FROM registry.access.redhat.com/ubi9/python-312:latest

ENV PYTHONUNBUFFERED=1

WORKDIR /opt/app-root/src

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY serve.py ./
COPY site ./site

EXPOSE 8080

CMD ["python", "serve.py"]

The files are only read, never written, so the image needs no special permissions to run as any user, on OpenShift or anywhere else.

podman build -t localhost/static-site:1.0.0 .
podman run -d --name static-site -p 8080:8080 localhost/static-site:1.0.0

The site is now at http://localhost:8080, served from the container.