How notarious2/fastapi-chat works
Project type and purpose
• This is a full-stack, real-time chat application.
• Backend: FastAPI (async), WebSockets, Redis (Pub/Sub & cache), PostgreSQL, Alembic migrations, SQLAlchemy.
• Frontend: Vue 3 + Vuetify 3 (separate repo), communicates over HTTP/REST + WebSocket.
File structure
• Top-level files: Dockerfile, docker-compose.yml, Makefile, Procfile, alembic/, pyproject.toml, src/ (app code), templates/, static/, tests/.
• src/ submodules:
• admin/ – admin panel models, services, custom auth backend
• authentication/ – JWT cookie auth, OAuth2 integration
• registration/, contact/, settings/ – REST routers, Pydantic schemas, service layer
• chat/ – chat REST endpoints (history, metadata)
• websocket/ – WS router, handlers, rate limiting, exceptions
• managers/ – PubSubManager, WebsocketManager to coordinate live events
• database.py, models.py, config.py, dependencies.py, main.py, routers.py
• Support: Alembic migrations; tests for each module.
README for additional insights
• Tech stack: FastAPI, WebSockets, Pydantic v2, asyncpg, async Redis, SQLAlchemy v2, Alembic, Poetry, Docker.
• Features: JWT HTTP-Only cookies, Google OAuth2, real-time pub/sub scaling, rate-limiting, background file uploads (to AWS S3 in prod), message/read status, user presence/typing, SQLAdmin panel.
Components
a. External Clients
• Web browser (Vue 3 + Vuetify)
• Mobile clients (if any)
b. API & WebSocket Layer
• FastAPI HTTP routers (authentication, registration, contact, settings, chat)
• FastAPI WebSocket router (real-time events)
c. Business Logic / Service Layer
• AuthenticationService, RegistrationService, ChatService, ContactService, SettingsService, AdminService
• PubSubManager (wrapping Redis Pub/Sub)
• WebsocketManager (managing active WS connections)
d. Data Layer
• PostgreSQL (asyncpg + SQLAlchemy models + Alembic migrations)
• Redis (Pub/Sub & caching for rate-limiting, presence, “is typing”)
• AWS S3 (file storage in production) / Local StaticFiles (dev)
e. Background Workers / Tasks
• FastAPI background tasks for file upload and non-critical updates
f. External Services
• Google OAuth2
• SQLAdmin panel (built upon FastAPI)
Relationships and data flows
• HTTP requests from clients → FastAPI HTTP routers → Service layer → Database or Redis → response.
• WebSocket connection established client ↔ FastAPI WS router → WebsocketManager → PubSubManager → Redis Pub/Sub → other instances’ WebsocketManager → WS handlers → deliver messages to clients.
• Background tasks triggered by endpoints → upload to S3 / update DB → notify via PubSub if needed.
• Authentication flow: login → JWT cookie set → subsequent HTTP/WS requests validated via dependencies.
• OAuth2: Frontend obtains Google token → sends to backend auth endpoint → backend verifies with Google → creates/fetches user in DB → issues JWT.
Architectural patterns & design principles
• Layered architecture: routers → services → data access
• CQRS-style split between HTTP (queries, commands) and WebSocket (real-time events)
• Pub/Sub for horizontal scalability across multiple backend replicas
• Dependency injection via FastAPI’s Depends for database sessions, auth, config
• Asynchronous all the way: database, Redis, HTTP, WS