How melbanna3/blog-ai works
Project type and purpose
• This is a standalone backend service: a RESTful Blog API built with FastAPI.
• Purpose: Manage users, categories, posts, and comments over HTTP, backed by PostgreSQL, secured with JWT.
File structure
• Top-level files:
• main.py – entry point where the FastAPI app, routers, and middleware are wired up.
• requirements.txt, runtime.txt – list Python dependencies & specify Python runtime (e.g., for Heroku).
• .env – environment variables (DB URL, JWT secret).
• Procfile – deployment command (e.g., web: uvicorn main:app).
• No separate “frontend” or “services” directories: this is a monolithic service.
Key README insights
• Endpoints grouped by resource (Users, Categories, Posts, Comments).
• Uses SQLAlchemy ORM for DB models.
• JWT for stateless authentication.
• Interactive docs (Swagger) at /docs.
Main components
a. Clients
• Web/Mobile clients or API clients (curl, Postman).
b. API Layer
• FastAPI app instance
• Routers/controllers for each resource (users, auth, categories, posts, comments)
• Dependency injection (DB session, security)
c. Business Logic
• Services or use-case modules (e.g., user_service, post_service)
• Data validation (Pydantic schemas)
d. Persistence Layer
• PostgreSQL database
• Tables: users, categories, posts, comments
• SQLAlchemy ORM (models + session management)
e. Security
• JWT authentication/authorization component
• Token issuance (/token endpoint)
• Protected endpoints requiring Authorization: Bearer
f. Infrastructure / Deployment
• Uvicorn ASGI server
• Hosting environment (Heroku, AWS, Docker, etc.)
• Environment variables (.env)
Relationships and data flows
• Client → HTTP request → FastAPI router
• Router → Validate input (Pydantic) → Delegate to service
• Service → Query/modify DB via SQLAlchemy session
• Service → Return domain object → Router → Serialize to JSON response
• For protected endpoints: Router checks JWT (via FastAPI dependency), rejects or injects current user
• Deployment: Procfile invokes Uvicorn, reads env variables to configure DB and JWT.
Architectural patterns & design principles
• Layered architecture: Presentation (routers) → Application (services) → Persistence (ORM)
• Stateless REST API, relying on JWT for auth (no server-side sessions)
• Dependency injection pattern for DB sessions and security credentials
• Single codebase monolith, but cleanly separated modules (routers, models, schemas, services)
Final checklist
• [ ] Clear labels on all components (Client, FastAPI API, Routers, Services, DB, Auth)
• [ ] Call out environment variables and deployment entrypoint (Procfile → Uvicorn)
• [ ] Show tables (users, categories, posts, comments) inside the DB component