Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DurdeuVlad/arithmos-service

Repository files navigation

๐Ÿงฎ Arithmos Service

A production-grade FastAPI microservice delivering essential mathematical operations (power, Fibonacci, factorial) with built-in request logging, Redis-based caching, Prometheus monitoring (optional), and support for JWT authentication and asynchronous messaging (RabbitMQ/Kafka).


๐Ÿ“‚ Project Structure

arithmos-service/
โ”œโ”€โ”€ app/
โ”‚ โ”œโ”€โ”€ main.py # FastAPI app and router inclusion
โ”‚ โ”œโ”€โ”€ routers/
โ”‚ โ”‚ โ””โ”€โ”€ math_ops.py # API endpoints
โ”‚ โ”œโ”€โ”€ services/
โ”‚ โ”‚ โ””โ”€โ”€ calculator.py # Core logic: pow, fib, fact
โ”‚ โ”œโ”€โ”€ models/
โ”‚ โ”‚ โ”œโ”€โ”€ db.py # Database connection (SQLite + SQLAlchemy/databases)
โ”‚ โ”‚ โ””โ”€โ”€ request_log.py # ORM model for logging API calls
โ”‚ โ”œโ”€โ”€ schemas/
โ”‚ โ”‚ โ””โ”€โ”€ math.py # Pydantic models
โ”‚ โ””โ”€โ”€ utils/
โ”‚ โ”œโ”€โ”€ cache.py # fastapi-cache2 config
โ”‚ โ”œโ”€โ”€ monitoring.py # Prometheus metrics setup (if enabled)
โ”‚ โ”œโ”€โ”€ auth.py # JWT-based auth (optional)
โ”‚ โ””โ”€โ”€ logging.py # Async message publishing (RabbitMQ/Kafka)
โ”œโ”€โ”€ docker/
โ”‚ โ”œโ”€โ”€ Dockerfile # Build definition
โ”‚ โ””โ”€โ”€ docker-compose.yml # Service stack: API, Redis, RabbitMQ/Kafka
โ”œโ”€โ”€ requirements.txt # Dependencies
โ”œโ”€โ”€ .flake8 # Linting rules
โ””โ”€โ”€ .gitignore

๐Ÿš€ Getting Started

Prerequisites

  • Docker (>= 20.x)
  • Docker Compose v1.29+ or v2
  • Windows PowerShell 5+ or Bash (macOS/Linux)

๐Ÿ’ก Windows users: Docker must be running with administrator privileges to avoid engine connection errors.

1. Clone & Setup

git clone https://github.com/DurdeuVlad/arithmos-service.git
cd arithmos-service
python3 -m venv .venv
# Activate environment:
# PowerShell:
.\.venv\Scripts\Activate.ps1
# macOS/Linux:
source .venv/bin/activate

2. Install Dependencies

pip install -r requirements.txt

3. Configure Environment

Copy .env.example to .env and update variables as needed. Default setup uses SQLite, Redis, and optionally RabbitMQ/Kafka via Docker.

4. Launch Services

docker compose -f docker/docker-compose.yml up -d --build

5. Health Check

Invoke-RestMethod -Uri http://localhost:8000/health -Method Get

Expected response:

{ "status": "ok" }

๐Ÿ“‹ API Endpoints

Method Path Description
POST /api/pow Compute base ** exp
GET /api/fib/{n} Return the n-th Fibonacci number
GET /api/fact/{n} Return the factorial of n
GET /api/logs Retrieve recent request logs

โš™๏ธ Example Requests (PowerShell)

Power (POST /pow)

Invoke-RestMethod -Uri http://localhost:8000/api/pow -Method Post -ContentType "application/json" -Body '{ "base": 2, "exp": 8 }'

Response:

{ "result": 256.0 }

Fibonacci (GET /fib/{n})

Invoke-RestMethod -Uri http://localhost:8000/api/fib/10 -Method Get

Response:

{ "result": 55 }

Factorial (GET /fact/{n})

Invoke-RestMethod -Uri http://localhost:8000/api/fact/5 -Method Get

Response:

{ "result": 120 }

View Logs (GET /logs)

Invoke-RestMethod -Uri http://localhost:8000/api/logs?limit=5 -Method Get

Sample Response:

[
 {
 "id": 3,
 "endpoint": "fact",
 "params": { "n": 5 },
 "result": 120,
 "created_at": "2025ๅนด07ๆœˆ17ๆ—ฅT10:13:39.683296"
 },
 {
 "id": 2,
 "endpoint": "fib",
 "params": { "n": 10 },
 "result": 55,
 "created_at": "2025ๅนด07ๆœˆ17ๆ—ฅT10:13:35.416599"
 }
]

๐Ÿ” Authentication (JWT - Optional)

To enable JWT authentication:

  1. Configure .env:
JWT_SECRET=supersecretkey
JWT_ALGORITHM=HS256
JWT_EXPIRE_HOURS=24
  1. Use Depends(get_current_user) to secure routes.
  2. Issue tokens via a login route (to be implemented separately).

Useful for securing logs or extending service with user access control.


๐Ÿณ Docker Commands

# Build & launch full stack
docker compose -f docker/docker-compose.yml up -d --build
# Stop & clean up (preserves database volume)
docker compose -f docker/docker-compose.yml down
# Remove SQLite database
rm -rf data/arithmos.db

About

arithmos-service is a lightweight math expression evaluation microservice built with Python. It exposes a REST API for parsing and computing arithmetic expressions safely and efficiently. Originally developed in a workplace training context (Endava).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /