How faezehghiasi/fastapi-project works
Project Type and Purpose
• This is a backend web‐service project (“Blog API”) built with FastAPI for HTTP endpoints, SQLAlchemy for ORM, and SQLite as the database.
• It provides user registration/authentication (OAuth2 + JWT) and CRUD operations on blog posts linked to users.
File Structure Analysis
• Top‐Level Files:
• README.md – Project overview, tech stack, API spec
• requirements.txt – Dependency list
• “blog/” Package:
• main.py – FastAPI app instantiation and router inclusion
• database.py – SQLAlchemy engine/session setup for SQLite
• models.py – ORM models (User, Blog)
• schemas.py – Pydantic schemas for request/response validation
• hashing.py – Password hashing utilities
• oauth2.py & token.py – JWT generation/validation, token settings
• repository/… – Data‐access layer with methods to CRUD users/blogs
• routers/… – Route handlers divided by domain (authentication, blogs, users)
Key Architectural Patterns
• Layered Architecture:
1. Presentation/API Layer (FastAPI routers)
2. Service/Business Layer (authentication, hashing, token logic)
3. Data Access Layer (repository classes using SQLAlchemy sessions)
4. Persistence Layer (SQLite database)
• Separation of Concerns: routers only handle HTTP; repositories only handle DB; models and schemas separate definitions.
Main Components
1. Client (e.g., browser, Postman, or mobile app)
2. FastAPI App
• Router Modules: authentication, user, blog
• Dependency Injection (DB session, OAuth2)
3. Authentication Service
• hashing.py (password hashing)
• oauth2.py/token.py (JWT creation & verification)
4. Repository Layer
• user.py & blog.py in blog/repository
• Exposes create, read, update, delete methods
5. Database
• SQLite DB file
• SQLAlchemy Engine + Session
Optional: External Clients (e.g., third‐party OAuth providers if extended)
Relationships & Data Flow
• Client → HTTP request (POST/GET/PUT/DELETE) → FastAPI Router
• Router uses Pydantic Schema to validate input → passes data to Service or Repository
• For protected routes: Router invokes OAuth2 dependency → verifies JWT → extracts user ID
• Repository layer uses SQLAlchemy session to query/commit data → returns model instances
• Router returns Pydantic Schema‐serialized response to Client
• Callouts for key technologies (FastAPI, SQLAlchemy, JWT, SQLite)
• Notes on protected routes and token validation