Uvicorn, auto-docs, and CORS

The app is written. Now we need to run it the way real teams do. That means uvicorn with reload so every save refreshes the server, the auto-generated /docs page so you can try routes without a client, and CORS middleware so a browser frontend can actually hit you.

The runtime topology

How uvicorn, the FastAPI app, and Docker fit together when the service runs.

terminal
bash
# Start the dev server with live reload
uv run uvicorn main:app --reload --host 0.0.0.0 --port 8000

# Then open the interactive docs in your browser
# http://localhost:8000/docs

The reload flag watches your source and restarts on save. Binding the host to 0.0.0.0 makes the server reachable from your LAN and from Docker. The /docs page is Swagger UI, built from the OpenAPI schema FastAPI generated for you.

main.py
python
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI(title="Streaming LLM Applications with FastAPI", lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

CORS middleware is what lets a browser at a different origin call your API. Wide open is fine for local dev, but lock it down to your real frontend origins in production.

No. Reload adds a file watcher and extra supervisor process that are great for dev and bad for prod. In production you drop the reload flag and run uvicorn with multiple workers behind a process manager, or you let gunicorn manage uvicorn workers. Our Docker image reflects that setup.

Quiz: Quiz

Loading practice…