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).
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
- 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.
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
pip install -r requirements.txt
Copy .env.example to .env and update variables as needed. Default setup uses SQLite, Redis, and optionally RabbitMQ/Kafka via Docker.
docker compose -f docker/docker-compose.yml up -d --build
- API: http://localhost:8000
- Docs (Swagger/OpenAPI): http://localhost:8000/docs
Invoke-RestMethod -Uri http://localhost:8000/health -Method Get
Expected response:
{ "status": "ok" }| 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 |
Invoke-RestMethod -Uri http://localhost:8000/api/pow -Method Post -ContentType "application/json" -Body '{ "base": 2, "exp": 8 }'
Response:
{ "result": 256.0 }Invoke-RestMethod -Uri http://localhost:8000/api/fib/10 -Method Get
Response:
{ "result": 55 }Invoke-RestMethod -Uri http://localhost:8000/api/fact/5 -Method Get
Response:
{ "result": 120 }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"
}
]To enable JWT authentication:
- Configure
.env:
JWT_SECRET=supersecretkey JWT_ALGORITHM=HS256 JWT_EXPIRE_HOURS=24
- Use
Depends(get_current_user)to secure routes. - Issue tokens via a login route (to be implemented separately).
Useful for securing logs or extending service with user access control.
# 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