Serve a Static Site
NotesThis 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 pipAdd the following requirements file:
fastapi
uvicorn[standard]
pydantic-settingsInstall them, then freeze the requirements:
pip install -r non_ver_reqs.txtpip freeze > requirements.txtAdd the following Python file:
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 thesitedirectory, and withhtml=True, servesindex.htmlfor a directory and404.html(if there is one) for pages that don’t exist.openapi_url=Noneturns off the API docs, which a static site doesn’t need.- TLS is optional: Uvicorn uses it when both
CONF_SSL_KEYFILEandCONF_SSL_CERTFILEare set. Behind a reverse proxy or on OpenShift, leave them unset, andproxy_headersmakes Uvicorn trust the proxy’sX-Forwarded-*headers.
Add a test index.html:
mkdir site<html>
<body>
<p>Hello World!</p>
</body>
</html>And run the single file Python application:
python serve.pyThe 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:
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.0The site is now at http://localhost:8080, served from the container.