The Social Media Backend is built as a clean, layered REST API that separates concerns across five distinct levels: the HTTP transport (Uvicorn), the web framework (FastAPI), authentication middleware (OAuth2PasswordBearer), data validation (Pydantic v2), and database access (SQLAlchemy ORM over PostgreSQL). Each layer has a single responsibility, making the codebase straightforward to extend and test in isolation. Alembic sits alongside the ORM layer to version-control schema changes without requiring manual SQL migrations.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pvnm4/Social-Media-Backend/llms.txt
Use this file to discover all available pages before exploring further.
Request lifecycle
Every inbound HTTP request travels through the following chain before a response is returned to the caller.- Uvicorn receives the raw TCP connection and forwards the ASGI-compatible request to FastAPI.
- CORSMiddleware inspects the
Originheader and attaches the appropriateAccess-Control-*response headers. - FastAPI router matches the path and HTTP method, then resolves all
Depends()annotations declared on the route handler. - OAuth2PasswordBearer (injected via
Depends(oauth2_scheme)) extracts theAuthorization: Bearer <token>header on protected routes. If the header is absent or malformed, FastAPI returns401before the handler is called. get_current_userdecodes and validates the JWT, then executes a database lookup to hydrate the fullUserORM object. This resolved user is passed directly into the route handler.get_dbyields aSessionLocalSQLAlchemy session that is bound to the lifetime of the request and automatically closed in thefinallyblock after the response is sent.- Route handler performs business logic using the session and ORM models, then returns a Python object.
- Pydantic response model serialises the returned object, stripping any fields not declared in the response schema (e.g.
password), before FastAPI serialises it to JSON.
SQLAlchemy sessions are created per-request via the
get_db generator dependency and are always closed after the response is delivered — even if an exception is raised inside the handler.Application layers
| Layer | Component | Purpose |
|---|---|---|
| HTTP server | Uvicorn | ASGI server that handles TCP connections and forwards requests to FastAPI |
| Framework | FastAPI | Routing, dependency injection, automatic OpenAPI/Swagger docs |
| Auth middleware | OAuth2PasswordBearer | JWT bearer token extraction from the Authorization header |
| Validation | Pydantic v2 | Request body parsing, response serialisation, and field-level constraints |
| ORM | SQLAlchemy 2.0 | Database abstraction — maps Python classes to PostgreSQL tables |
| Migrations | Alembic | Schema version control; runs upgrade/downgrade scripts against the live DB |
| Database | PostgreSQL | Persistent relational storage for users, posts, and votes |
Router structure
The application mounts four routers insideapp/main.py. Each router lives in its own module under app/routers/ and encapsulates all routes for a single resource.
| Router module | URL prefix | OpenAPI tag |
|---|---|---|
post.router | /posts | Posts |
user.router | /users | Users |
auth.router | (none) | Authentication |
vote.router | /vote | Vote |
main.py with app.include_router():
auth.router carries no prefix, the login endpoint is reachable directly at POST /login rather than under a sub-path.
CORS configuration
Cross-Origin Resource Sharing is configured to be fully permissive, allowing any browser origin to call the API. This is appropriate for development and public APIs, but should be tightened for production deployments that serve known front-end origins.Dependency injection
FastAPI’sDepends() system is used throughout the codebase to supply two cross-cutting concerns to route handlers: a database session and the currently authenticated user. Both are declared as function parameters; FastAPI resolves them automatically before the handler body executes.
get_current_user into a route handler automatically makes that endpoint require a valid JWT — no decorators or extra configuration are needed beyond the Depends() annotation.