This project is a modular Flask API with a clean separation between:
- API layer (Flask-RESTX namespaces)
- Service layer (business logic)
- Repository layer (database access)
- Database initialization service (singleton engine + session factory)
- DTOs & validation (Pydantic)
- SQL queries (stored in a dedicated module)
app/
β
βββ api/
β βββ namespaces/
β β βββ users_namespace.py # Flask-RESTX endpoints
β β βββ ...
β β
β βββ services/
β β βββ users_service.py # Business logic for Users
β β βββ ...
β β
β βββ db/
β β βββ database.py # Database engine + session singleton
β β βββ repositories/
β β β βββ base_repo.py # Shared helpers (execute, load_to_db, etc.)
β β β βββ users_repo.py # User repository
β β β βββ ...
β β βββ data/
β β β βββ queries.py # Raw SQL queries
β β β βββ ...
β β βββ ...
β β
β βββ models/
β β βββ users_models.py # Marshal schemas for requests input/output
β β βββ ...
| |__ dto/
β β βββ users.py # Custom Data Type models for params and return values
β β βββ ...
β β
β βββ utils/
β β βββ auth_token.py # Token encoding/decoding utilities
β β
β βββ __init__.py # create_app(), namespace registration
β
βββ config.py # Environment & DB config
βββ main.py # Flask API declaration
Database is a singleton responsible for creating and exposing:
- SQLAlchemy engine
- SQLAlchemy SessionLocal factory
Services and repositories never create engines themselvesβthey call:
from app.api.db.database import Database
engine = Database.get_engine()
session = Database.get_session()
Business logic layer. Each service loads its repository and DB engine internally:
class UsersService:
def __init__(self):
self.engine = Database.get_engine()
self.repo = UsersRepository(self.engine)
Contain only data operations (SQL queries, selects, inserts, updates):
- BaseRepository provides _execute_query() and _load_data_to_db().
- Each child repository implements domain-specific DB operations.
To create a virtual environment:
python -m venv venv
To activate a virtual environment:
venv\Scripts\activate
To install library:
pip install <library>
To install libraries from requirements.txt file:
pip install -r requirements.txt
To run the application:
python run.py
or:
python runWaitressServer.py
https://github.com/JeevanSandhu/Documentation/blob/master/Flask%20API%20on%20IIS.md
- Python >= 3.12
- Python Formatter Black (Nice to have for code formatting purpose)