How flash019/stackit_backend works
Project type and purpose
• This is a backend microservice (“StackIt-Backend”) built with FastAPI and MongoDB.
• Purpose: a Q&A system with JWT‐based authentication, real-time WebSocket notifications, background tasks, and standard CRUD operations for questions/answers.
File structure
• main.py: entry point that instantiates FastAPI, mounts middleware and routers.
• routers/: controller layer (HTTP endpoints grouped by feature: auth, questions, answers, notifications, WebSocket, search, admin, votes).
• services/: business‐logic layer (services invoked by routers).
• repository/: data‐access layer (MongoDB queries, user repository).
• models/: Pydantic models and domain schemas.
• utils/: auxiliary modules (JWT helpers, token generation, auth utilities).
• middleware/: cross‐cutting concerns (rate limiting).
• database.py & hashing.py: DB connection and password‐hashing utilities.
• requirements.txt and the venv structure establish dependencies (FastAPI, Uvicorn, pymongo, python-jose, etc.).
README for additional insights
• Highlights FastAPI (async API), MongoDB storage, Swagger UI docs (/docs & /redoc), background tasks, WebSocket support, and typical deployment via Render.
Main components
a. Clients
• Admin Panel (browser)
• User Panel (browser/mobile app)
b. API Layer
• ASGI Server (Uvicorn/Gunicorn)
• FastAPI App
c. Layered Architecture
• Router (Controllers)
• Service (Business Logic)
• Repository (Data Access)
• Models (Schemas/Pydantic)
d. Cross-cutting
• Middleware (Rate Limiter)
• Utils (JWT, hashing)
• Background Task Manager (FastAPI’s BackgroundTasks)
e. Database
• MongoDB cluster/instance
f. Real-time
• WebSocket Endpoint (notifications_ws)
g. External/Infrastructure
• Swagger UI / ReDoc docs
• Host/Deployment Platform (e.g., Render)
Relationships and interactions
• Client → HTTP/WebSocket requests → API Layer
• API Layer → Routers → Services → Repositories → MongoDB
• Middleware intercepts at the API Layer (rate‐limit)
• Authentication: JWT utils invoked by routers/services
• Background tasks invoked by routers/services enqueue notification jobs
• WebSocket endpoint pushes real‐time events to connected clients
Architectural patterns & design principles
• Layered (Controller → Service → Repository) — clear separation of concerns
• Hexagonal/Ports & Adapters: business logic decoupled from transport (HTTP vs. WebSocket)
• Async throughout: FastAPI + asyncio + background tasks
• Security by design: JWT + role-based access
Relevant technologies/frameworks
• FastAPI (routers, dependency injection, background tasks, WebSockets)
• Uvicorn (ASGI server)
• Pydantic (request/response models)
• PyMongo (MongoDB driver)
• python-jose (JWT)
• Middleware for rate limiting
• Hosting on Render (CI/CD integration)
Detail & labeling
• Non-blocking operations (async + background tasks)