Skip to content

AmalSultanov/cafe_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CafeAPI - Food Ordering API

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.


๐Ÿ—‚๏ธ Contents


๐Ÿ“ Project Structure

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

๐Ÿ–ผ๏ธ Entities Involved

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

๐Ÿ› ๏ธ Tech Stack

  • Python
  • FastAPI
  • PostgreSQL
  • SQLAlchemy
  • Alembic
  • JWT
  • FastStream (Apache Kafka)
  • FastAPI-Amis-Admin
  • AWS S3
  • Kafka UI
  • Prometheus
  • Promtail
  • Loki
  • Grafana
  • Docker

๐Ÿš€ How to Run the App

๐Ÿณ Using Docker

  1. Clone the repository

    git clone https://github.com/AmalSultanov/cafe_api.git
    cd freq_counter
  2. Create a .env file and set environment variables in any editor

    Refer to the โš™๏ธ Environment Variables section for clarification.

    cp .env.example .env
    nano .env
  3. Build and run using Docker Compose

    docker compose up --build

The app will be available at http://localhost.


โš™๏ธ Environment Variables

The application uses the following environment variables (check .env.example):

FastAPI

  • 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 (True or False)
  • FASTAPI_VERSION - Application version (e.g., 1.0.0)

Prometheus FastAPI Instrumentator

  • ENABLE_METRICS - Enabling metrics (e.g., True)

CORS

  • CORS_ORIGINS - CORS origins allowed (e.g., http://localhost,http://127.0.0.1)

PostgreSQL

  • 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., localhost or a Docker Compose service name)
  • POSTGRES_PORT - Port number for PostgreSQL (e.g., 5432)

JWT

  • JWT_SECRET_KEY - Secret key for signing access tokens
  • JWT_REFRESH_SECRET_KEY - Secret key for signing refresh tokens
  • JWT_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

  • 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

  • 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

  • PROMETHEUS_PORT - Prometheus port (e.g., 9090)

Promtail

  • PROMTAIL_PORT - Promtail port (e.g., 9080)

Loki

  • LOKI_PORT - Loki port (e.g., 3100)

Grafana

  • 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 S3

  • AWS_ACCESS_KEY_ID - AWS access key ID
  • AWS_SECRET_ACCESS_KEY - AWS secret access key
  • AWS_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.


๐Ÿงพ API Documentation

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

๐Ÿ”— API Endpoints

Meal Categories

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

Meals

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

Users

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

Shopping Cart

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

Cart Items

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

Orders

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

๐Ÿงช Interactive API Docs & Access

Once the server is running, access the various interfaces:


๐Ÿ”ฎ Future Work

Planned Features

  • Payment Integration: Click/PayMe integration for online payments
  • Delivery Tracking: GPS-based delivery tracking system

Technical Improvements

  • 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

About

REST API designed for a cafe business

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages