A REST API backend service designed for cafes, enabling user management, menu browsing, cart operations, and order processing. Features include JWT authentication, Kafka event-driven integration, and an Amis Admin dashboard for easy system administration.
- Project Structure
- Entities Involved
- Tech Stack
- How to Run the App
- Environment Variables
- API Documentation
- Future Work
The project follows a clean architecture pattern with clear separation of concerns and event-driven design:
cafe_api/
โ
โโโ grafana/
โโโ nginx/
โโโ src/
โ โโโ admin/ # Admin dashboard config, models and custom endpoints for meals bulk insertion
โ โโโ controllers/ # Versioned API route handlers
โ โโโ core/ # Core configurations, dependencies, and utilities
โ โโโ exceptions/ # Custom exception classes and global handlers
โ โโโ message_broker/ # Event-driven architecture components
โ โโโ middlewares/ # Custom middleware components
โ โโโ migrations/ # Alembic files
โ โโโ models/ # SQLAlchemy database models with relationships
โ โโโ monitoring/ # Prometheus FastAPI Instrumentator config, metrics
โ โโโ repositories/ # Data access layer with interface abstractions
โ โโโ schemas/ # Pydantic models for request/response validation
โ โโโ services/ # Business logic layer with service interfaces
โ โโโ __init__.py
โ โโโ main.py # Application entry point
โ
โโโ tests/
โโโ .env.example
โโโ .gitignore
โโโ alembic.ini
โโโ docker-compose.yml
โโโ Dockerfile
โโโ loki-config.yml
โโโ poetry.lock
โโโ prometheus-config.yml
โโโ promtail-config.yml
โโโ pyproject.toml
โโโ README.md
The system uses PostgreSQL with the following main entities and relationships:
- Users: User profiles with phone-based auth
- UserIdentities: Multi-platform identity management (Telegram/Web) with provider-specific usernames
- MealCategories: Meal categorization with descriptions
- Meals: Menu items with pricing, descriptions, and images
- Carts: User shopping carts with total price calculation
- CartItems: Individual items in shopping carts with quantity and pricing
- Orders: Complete orders with delivery information and status tracking
- OrderItems: Items within orders with final quantity and pricing
- Python
- FastAPI
- PostgreSQL
- SQLAlchemy
- Alembic
- JWT
- FastStream (Apache Kafka)
- FastAPI-Amis-Admin
- AWS S3
- Kafka UI
- Prometheus
- Promtail
- Loki
- Grafana
- Docker
-
Clone the repository
git clone https://github.com/AmalSultanov/cafe_api.git cd freq_counter -
Create a
.envfile and set environment variables in any editorRefer to the โ๏ธ Environment Variables section for clarification.
cp .env.example .env nano .env
-
Build and run using Docker Compose
docker compose up --build
The app will be available at http://localhost.
The application uses the following environment variables (check .env.example):
FASTAPI_HOST- Host for the FastAPI server (e.g.,127.0.0.1)FASTAPI_PORT- Port number for FastAPI (e.g.,8000)FASTAPI_DEBUG- Enable debug mode (TrueorFalse)FASTAPI_VERSION- Application version (e.g.,1.0.0)
ENABLE_METRICS- Enabling metrics (e.g.,True)
CORS_ORIGINS- CORS origins allowed (e.g.,http://localhost,http://127.0.0.1)
POSTGRES_DB- PostgreSQL database name (e.g.,cafe_db)POSTGRES_USER- PostgreSQL username (e.g.,postgres)POSTGRES_PASSWORD- PostgreSQL password (e.g.,postgres)POSTGRES_HOST- Host for PostgreSQL (e.g.,localhostor a Docker Compose service name)POSTGRES_PORT- Port number for PostgreSQL (e.g.,5432)
JWT_SECRET_KEY- Secret key for signing access tokensJWT_REFRESH_SECRET_KEY- Secret key for signing refresh tokensJWT_ALGORITHM- JWT signing algorithm (e.g.,HS256)JWT_ACCESS_TOKEN_EXPIRE_MINUTES- Access token expiration time in minutes (e.g.,15)JWT_REFRESH_TOKEN_EXPIRE_DAYS- Refresh token expiration time in days (e.g.,1)
KAFKA_HOST- Kafka broker host (e.g.,localhost)KAFKA_PORT- Kafka broker port (e.g.,9092)KAFKA_CONTROLLER_PORT- Kafka controller port (e.g.,9093)
KAFKA_UI_PORT- Kafka UI port (e.g.,8080)KAFKA_UI_USERNAME- Kafka UI username (e.g.,user1)KAFKA_UI_PASSWORD- Kafka UI user password (e.g.,password1)
PROMETHEUS_PORT- Prometheus port (e.g.,9090)
PROMTAIL_PORT- Promtail port (e.g.,9080)
LOKI_PORT- Loki port (e.g.,3100)
GRAFANA_PORT- Grafana port (e.g.,3000)GRAFANA_ADMIN_USER- Grafana admin username (e.g.,admin)GRAFANA_ADMIN_PASSWORD- Grafana admin password (e.g.,admin)
AWS_ACCESS_KEY_ID- AWS access key IDAWS_SECRET_ACCESS_KEY- AWS secret access keyAWS_REGION- AWS region (e.g.,us-east-1)AWS_S3_BUCKET_NAME- S3 bucket name for storing meals images (e.g.,cafe-api-meals-bucket)
You can customize these based on your working environment.
This backend provides the following main API groups:
/meal-categories/*- Create, list, update, and delete meal categories/meal-categories/{category_id}/meals/*- Manage meals within categories (CRUD operations)/users/*- User registration, login, identity management, and profile operations/users/{user_id}/cart/*- Shopping cart creation, retrieval, and deletion/users/{user_id}/cart/items/*- Add, update, remove and clear cart items/users/{user_id}/orders/*- Create and manage user orders
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/meal-categories |
Create a category |
GET |
/api/v1/meal-categories |
List paginated categories |
GET |
/api/v1/meal-categories/{category_id} |
Get a category |
PATCH |
/api/v1/meal-categories/{category_id} |
Update a category |
DELETE |
/api/v1/meal-categories/{category_id} |
Delete a category |
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/meal-categories/{category_id}/meals |
Create a meal |
GET |
/api/v1/meal-categories/{category_id}/meals |
List paginated meals in category |
GET |
/api/v1/meal-categories/{category_id}/meals/{meal_id} |
Get meal details |
PUT |
/api/v1/meal-categories/{category_id}/meals/{meal_id} |
Fully update a meal |
PATCH |
/api/v1/meal-categories/{category_id}/meals/{meal_id} |
Partially update a meal |
DELETE |
/api/v1/meal-categories/{category_id}/meals/{meal_id} |
Delete a meal |
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/users/register |
Register a new user |
POST |
/api/v1/users/log-in |
Log in a user |
POST |
/api/v1/users/{user_id}/logout |
Log out a user |
GET |
/api/v1/users/me |
Get current user info |
GET |
/api/v1/users/check-identity?provider=provider_name&provider_id=provider_id&username=username |
Check identity existence in db |
GET |
/api/v1/users/by-provider?provider=provider_name&provider_id=provider_id&username=username |
Get identity |
GET |
/api/v1/users |
List paginated users |
GET |
/api/v1/users/{user_id} |
Get user details |
PUT |
/api/v1/users/{user_id} |
Fully update a user |
PATCH |
/api/v1/users/{user_id} |
Partially update a user |
DELETE |
/api/v1/users/{user_id} |
Delete a user |
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/users/{user_id}/cart |
Create a cart |
GET |
/api/v1/users/{user_id}/cart |
Get cart details |
DELETE |
/api/v1/users/{user_id}/cart |
Delete a cart |
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/users/{user_id}/cart/items |
Add an item to cart |
GET |
/api/v1/users/{user_id}/cart/items |
Get cart items |
GET |
/api/v1/users/{user_id}/cart/items/{item_id} |
Get specific cart item |
PATCH |
/api/v1/users/{user_id}/cart/items/{item_id} |
Update a cart item |
DELETE |
/api/v1/users/{user_id}/cart/items/{item_id} |
Remove a cart item |
DELETE |
/api/v1/users/{user_id}/cart/items |
Clear cart |
| Method | Path | Description |
|---|---|---|
POST |
/api/v1/users/{user_id}/orders |
Create order from cart |
GET |
/api/v1/users/{user_id}/orders |
Get user orders |
GET |
/api/v1/users/{user_id}/orders/{order_id} |
Get a specific order |
DELETE |
/api/v1/users/{user_id}/orders/{order_id} |
Delete a specific order |
DELETE |
/api/v1/users/{user_id}/orders |
Get all orders |
Once the server is running, access the various interfaces:
- Swagger UI: http://localhost/docs - Interactive API documentation
- ReDoc: http://localhost/redoc - Read-only API documentation
- Admin Dashboard: http://localhost/admin - Administrative interface
- Health Check: http://localhost/api/v1/health - Application health status
- Payment Integration: Click/PayMe integration for online payments
- Delivery Tracking: GPS-based delivery tracking system
- Comprehensive Testing: Unit, integration, and end-to-end tests
- Performance Optimization: Database query optimization and caching
- Monitoring & Logging: Structured logging with ELK stack integration
- CI/CD Pipeline: GitHub Actions for automated testing and deployment
- API Rate Limiting: Request throttling and abuse prevention