Skip to content

Latest commit

Β 

History

History
133 lines (91 loc) Β· 3.45 KB

File metadata and controls

133 lines (91 loc) Β· 3.45 KB

NGO NÑruč REST API

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)

API Structure

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

Key Concepts

Database Initialization

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()

Services

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)

Repositories

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.

Installing libraries

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

Running the application

To run the application:

python run.py

or:

python runWaitressServer.py

Application Deployment

https://github.com/JeevanSandhu/Documentation/blob/master/Flask%20API%20on%20IIS.md

Requirements

  • Python >= 3.12
  • Python Formatter Black (Nice to have for code formatting purpose)