How qimolin/fastapi-multi-tenancy works
This is a FastAPI starter template for tenant-isolated applications. The main workflow creates a tenant and owner, authenticates users to issue tenant-bearing JWTs, then applies authenticated requests under PostgreSQL row-level security; tenant admins can manage users, and the example domain demonstrates tenant-scoped CRUD. Redis stores refresh-token state, while audit logging and health checks support operations. The README documents PostgreSQL RLS and the example CRUD capability; their implementation details are not fully sampled, so uncertain internal wiring is omitted.
Main entry points: main.py FastAPI app · service.py Auth service · tenants/router.py Tenant endpoints · auth/router.py Auth endpoints
Components 20
API and middleware
- FastAPI app
src/main.py - Correlation IDs
src/middleware.py - Error handlers
src/exception_handlers.py
Tenant management
- Tenant endpoints
src/tenants/router.py - Tenant service
src/tenants/service.py - Tenant model
src/tenants/models.py
Authentication and RBAC
- Auth endpoints
src/auth/router.py - Auth service
src/auth/service.py - Auth and roles
src/auth/dependencies.py - Token and passwords
src/auth/utils.py - User model
src/auth/models.py
Example domain
- Example endpoints
src/example/router.py - Example CRUD
src/example/service.py - Post model
src/example/models.py
Persistence and operations
- DB sessions
src/database.py - PostgreSQL
- Redis
- Audit logging
src/audit/utils.py - Health endpoint
src/health/router.py
Other components
- API client
Connections 29
- API client → FastAPI app: sends requests
- FastAPI app → Correlation IDs: processes through
- FastAPI app → Tenant endpoints: dispatches
- FastAPI app → Auth endpoints: dispatches
- FastAPI app → Example endpoints: dispatches
- FastAPI app → Health endpoint: dispatches
- FastAPI app → Error handlers: registers
- Tenant endpoints → Tenant service: creates tenant
- Tenant endpoints → Auth service: creates owner
- Tenant endpoints → DB sessions: sets tenant context
- Tenant service → Tenant model: persists
- Tenant endpoints → Audit logging: logs events
- Auth endpoints → Auth service: authenticates and issues tokens
- Auth endpoints → Auth and roles: requires user or admin
- Auth endpoints → DB sessions: queries users
- Auth endpoints → Audit logging: logs events
- Auth service → Token and passwords: hashes and signs
- Auth service → User model: reads and creates
- Auth service → Redis: stores refresh tokens
- Auth and roles → Token and passwords: decodes tokens
- Auth and roles → DB sessions: sets RLS context
- Auth and roles → Auth service: loads user
- Example endpoints → Example CRUD: calls CRUD
- Example endpoints → Auth and roles: requires authentication
- Example CRUD → Post model: queries and persists
- Example CRUD → DB sessions: uses session
- DB sessions → PostgreSQL: executes queries
- Health endpoint → PostgreSQL: checks connectivity
- Health endpoint → Redis: checks connectivity