Skip to content

abdoElHodaky/logisticsassgrpc

Repository files navigation

🚚 Logistics Management System - API Gateway

⚑ High-Performance gRPC API Gateway for Enterprise Logistics

Modern, scalable microservices architecture with TypeScript, Express.js, and gRPC following golden ratio design principles

TypeScript Express.js gRPC
MariaDB TypeORM Microservices

πŸ“‹ Overview

This project serves as the API Gateway for a comprehensive logistics management system. It provides RESTful HTTP endpoints that communicate with backend microservices via gRPC, offering a unified interface for managing logistics operations including shipments, inventory, tracking, and order management.

🎯 Key Features

  • πŸ”Œ RESTful API Gateway: Clean HTTP endpoints for client applications
  • ⚑ gRPC Communication: High-performance inter-service communication
  • πŸ—„οΈ Database Integration: MariaDB with TypeORM for robust data persistence
  • πŸ“¦ TypeScript: Full type safety and modern development experience
  • πŸ—οΈ Microservices Architecture: Scalable and maintainable service design
  • πŸ” Enterprise-Ready: Built for production logistics systems

πŸ› οΈ Technology Stack

Technology Purpose Version
TypeScript Primary programming language Latest
Express.js Web framework for API endpoints 4.x
gRPC Microservices communication protocol Latest
MariaDB Relational database management 10.x+
TypeORM Database ORM and migration management Latest

πŸ—οΈ Architecture - Golden Ratio Design

Enterprise logistics architecture following Ο† proportions for optimal system visualization

🌟 High-Level System Architecture - Golden Ratio Layout

graph TB
    %% Golden Ratio Client Layer (Ο† = 1.618 user interaction priority)
    subgraph "🌐 Client Layer - Ο† Priority Interface"
        direction LR
        WebApp[πŸ“± Web Application<br/>Angular/React<br/>Ο† User Experience]
        MobileApp[πŸ“² Mobile App<br/>React Native/Flutter<br/>Ο† Mobile Priority]
        AdminUI[πŸ‘¨β€πŸ’» Admin Dashboard<br/>Analytics & Reports<br/>1.0 Management]
        IoT[πŸ”Œ IoT Devices<br/>Sensors & Trackers<br/>1/Ο† Automation]
    end
    
    %% Golden Ratio Gateway Layer (1.0 balanced processing)
    subgraph "πŸšͺ API Gateway Layer - Balanced Processing"
        direction TB
        Gateway[⚑ Express.js Gateway<br/>Request Routing<br/>Load Balancing]
        Auth[πŸ” Authentication<br/>JWT & OAuth<br/>Security Layer]
        RateLimit[⏱️ Rate Limiting<br/>Traffic Control<br/>Protection]
        Swagger[πŸ“š API Documentation<br/>OpenAPI Spec<br/>Developer Portal]
    end
    
    %% Golden Ratio Microservices Layer (Ο† core business logic)
    subgraph "βš™οΈ Microservices Layer - Ο† Business Logic"
        direction TB
        
        subgraph "🚚 Logistics Core Services"
            Shipment[πŸ“¦ Shipment Service<br/>Package Management<br/>Ο† Priority]
            Tracking[πŸ“ Tracking Service<br/>Real-time Location<br/>Ο† Priority]
            Inventory[πŸ“Š Inventory Service<br/>Stock Management<br/>1.0 Priority]
        end
        
        subgraph "πŸ’Ό Business Support Services"
            Order[πŸ“‹ Order Service<br/>Order Processing<br/>1.0 Priority]
            User[πŸ‘₯ User Service<br/>Account Management<br/>1/Ο† Priority]
            Notification[πŸ”” Notification Service<br/>Alerts & Messages<br/>1/Ο† Priority]
        end
    end
    
    %% Golden Ratio Data Layer (1/Ο† = 0.618 persistence)
    subgraph "πŸ—„οΈ Data Persistence Layer - Ο† Storage"
        direction LR
        MariaDB[(πŸ—ƒοΈ MariaDB Cluster<br/>Primary Database<br/>Ο† Write Load)]
        Redis[(⚑ Redis Cache<br/>Session & Performance<br/>38.2% Cache)]
        FileSystem[(πŸ“ File Storage<br/>Documents & Assets<br/>38.2% Files)]
    end
    
    %% Golden Spiral Communication Flow (Primary - 61.8%)
    WebApp ==>|Primary Traffic<br/>Ο† Load| Gateway
    MobileApp ==>|Primary Traffic<br/>Ο† Load| Gateway
    AdminUI -->|Management Traffic<br/>Standard Load| Gateway
    IoT -.->|Sensor Data<br/>Background Load| Gateway
    
    %% Gateway Processing (Balanced - 1.0)
    Gateway ==>|Request Processing| Auth
    Auth ==>|Authenticated Requests| RateLimit
    RateLimit ==>|Controlled Traffic| Shipment
    RateLimit ==>|Controlled Traffic| Tracking
    RateLimit ==>|Controlled Traffic| Order
    
    %% gRPC Communication (Ο† distributed)
    Shipment -.->|gRPC Calls<br/>Service Mesh| Inventory
    Tracking -.->|gRPC Calls<br/>Location Updates| Shipment
    Order -.->|gRPC Calls<br/>Order Processing| User
    User -.->|gRPC Calls<br/>Notifications| Notification
    
    %% Data Layer Connections (Ο† distributed load)
    Shipment ==>|Write Operations<br/>Ο† Priority| MariaDB
    Tracking ==>|Write Operations<br/>Ο† Priority| MariaDB
    Order ==>|Write Operations<br/>Standard| MariaDB
    
    Gateway -.->|Session Management| Redis
    Auth -.->|Token Caching| Redis
    Shipment -.->|Document Storage| FileSystem
    
    %% Golden Ratio Color Scheme (Ο† visual hierarchy)
    style WebApp fill:#3498db,stroke:#2980b9,stroke-width:5px,color:#ffffff
    style MobileApp fill:#e74c3c,stroke:#c0392b,stroke-width:5px,color:#ffffff
    style AdminUI fill:#f39c12,stroke:#e67e22,stroke-width:3px,color:#ffffff
    style IoT fill:#95a5a6,stroke:#7f8c8d,stroke-width:2px,color:#ffffff
    
    style Gateway fill:#2c3e50,stroke:#1a252f,stroke-width:4px,color:#ffffff
    style Auth fill:#27ae60,stroke:#229954,stroke-width:4px,color:#ffffff
    style RateLimit fill:#8e44ad,stroke:#7d3c98,stroke-width:3px,color:#ffffff
    style Swagger fill:#16a085,stroke:#138d75,stroke-width:2px,color:#ffffff
    
    style Shipment fill:#e74c3c,stroke:#c0392b,stroke-width:4px,color:#ffffff
    style Tracking fill:#3498db,stroke:#2980b9,stroke-width:4px,color:#ffffff
    style Inventory fill:#f39c12,stroke:#e67e22,stroke-width:3px,color:#ffffff
    style Order fill:#27ae60,stroke:#229954,stroke-width:3px,color:#ffffff
    style User fill:#9b59b6,stroke:#8e44ad,stroke-width:2px,color:#ffffff
    style Notification fill:#34495e,stroke:#2c3e50,stroke-width:2px,color:#ffffff
    
    style MariaDB fill:#4caf50,stroke:#388e3c,stroke-width:5px,color:#ffffff
    style Redis fill:#ff5722,stroke:#e64a19,stroke-width:3px,color:#ffffff
    style FileSystem fill:#607d8b,stroke:#455a64,stroke-width:3px,color:#ffffff
Loading

πŸ”„ Request Flow Architecture - Golden Ratio Communication

gRPC communication patterns optimized with Ο† proportions for efficient data flow

sequenceDiagram
    participant C as πŸ“± Client
    participant G as πŸšͺ API Gateway
    participant A as πŸ” Auth Service
    participant S as πŸ“¦ Shipment Service
    participant T as πŸ“ Tracking Service
    participant D as πŸ—ƒοΈ Database
    
    Note over C,D: Golden Ratio Request Flow (Ο† = 1.618 optimization)
    
    %% Primary Flow (61.8% - Ο† priority)
    C->>+G: 1. HTTP Request<br/>Ο† Priority Traffic
    G->>+A: 2. Validate Token<br/>Security Check
    A-->>-G: 3. Auth Success<br/>User Context
    
    %% Business Logic Flow (1.0 - balanced)
    G->>+S: 4. gRPC Call<br/>Business Logic
    S->>+T: 5. Location Update<br/>Service Communication
    T-->>-S: 6. Tracking Data<br/>Real-time Info
    
    %% Data Layer Flow (1/Ο† - 38.2% persistence)
    S->>+D: 7. Database Query<br/>Data Persistence
    D-->>-S: 8. Query Result<br/>Retrieved Data
    
    %% Response Flow (Ο† spiral pattern)
    S-->>-G: 9. gRPC Response<br/>Processed Data
    G-->>-C: 10. HTTP Response<br/>JSON Payload
    
    %% Golden Ratio Timing Annotations
    Note over C,G: Ο† Response Time: <100ms
    Note over G,S: 1.0 Processing: <200ms  
    Note over S,D: 1/Ο† Query Time: <50ms
Loading

🌐 gRPC Service Mesh - Golden Ratio Microservices

Advanced microservices communication mesh using Ο† proportions for optimal service interaction

graph TB
    %% Golden Ratio Service Discovery Layer (Ο† = 1.618 priority)
    subgraph "πŸ” Service Discovery - Ο† Priority Layer"
        direction TB
        ServiceRegistry[πŸ“‹ Service Registry<br/>Consul/Eureka<br/>Ο† Discovery Priority]
        LoadBalancer[βš–οΈ Load Balancer<br/>HAProxy/Nginx<br/>Ο† Traffic Distribution]
        HealthCheck[πŸ’“ Health Monitoring<br/>Service Mesh<br/>1.0 Monitoring]
    end
    
    %% Golden Ratio gRPC Services Layer (Ο† core services)
    subgraph "⚑ gRPC Services Mesh - Ο† Business Core"
        direction TB
        
        subgraph "🚚 Logistics Domain Services"
            ShipmentGRPC[πŸ“¦ Shipment gRPC<br/>Proto: shipment.proto<br/>Ο† Core Service]
            TrackingGRPC[πŸ“ Tracking gRPC<br/>Proto: tracking.proto<br/>Ο† Core Service]
            InventoryGRPC[πŸ“Š Inventory gRPC<br/>Proto: inventory.proto<br/>1.0 Service]
        end
        
        subgraph "πŸ’Ό Support Domain Services"
            OrderGRPC[πŸ“‹ Order gRPC<br/>Proto: order.proto<br/>1.0 Service]
            UserGRPC[πŸ‘₯ User gRPC<br/>Proto: user.proto<br/>1/Ο† Service]
            NotificationGRPC[πŸ”” Notification gRPC<br/>Proto: notification.proto<br/>1/Ο† Service]
        end
    end
    
    %% Golden Ratio Message Queue Layer (1/Ο† = 0.618 async)
    subgraph "πŸ“¨ Message Queue Layer - Async Communication"
        direction LR
        EventBus[🚌 Event Bus<br/>Apache Kafka<br/>Ο† Event Priority]
        MessageQueue[πŸ“¬ Message Queue<br/>RabbitMQ<br/>38.2% Async Load]
        StreamProcessor[🌊 Stream Processing<br/>Apache Flink<br/>38.2% Real-time]
    end
    
    %% Golden Ratio Data Persistence (1/Ο† distributed)
    subgraph "πŸ—„οΈ Distributed Data Layer - Ο† Storage"
        direction TB
        
        subgraph "πŸ“Š Primary Databases"
            ShipmentDB[(πŸ“¦ Shipment DB<br/>MariaDB Cluster<br/>Ο† Write Load)]
            TrackingDB[(πŸ“ Tracking DB<br/>TimeSeries DB<br/>Ο† Write Load)]
            InventoryDB[(πŸ“Š Inventory DB<br/>PostgreSQL<br/>1.0 Load)]
        end
        
        subgraph "⚑ Cache & Search"
            RedisCluster[(⚑ Redis Cluster<br/>Distributed Cache<br/>38.2% Cache)]
            ElasticSearch[(πŸ” Elasticsearch<br/>Search Engine<br/>38.2% Search)]
        end
    end
    
    %% Service Discovery Connections (Ο† priority)
    ServiceRegistry ==>|Service Registration<br/>Ο† Priority| ShipmentGRPC
    ServiceRegistry ==>|Service Registration<br/>Ο† Priority| TrackingGRPC
    ServiceRegistry -->|Service Registration<br/>Standard| OrderGRPC
    ServiceRegistry -.->|Service Registration<br/>Background| UserGRPC
    
    LoadBalancer ==>|Traffic Distribution<br/>Ο† Load Balancing| ShipmentGRPC
    LoadBalancer ==>|Traffic Distribution<br/>Ο† Load Balancing| TrackingGRPC
    LoadBalancer -->|Traffic Distribution<br/>Standard| InventoryGRPC
    
    %% gRPC Inter-Service Communication (Ο† mesh)
    ShipmentGRPC -.->|gRPC Stream<br/>Real-time Updates| TrackingGRPC
    TrackingGRPC -.->|gRPC Call<br/>Location Data| ShipmentGRPC
    ShipmentGRPC -.->|gRPC Call<br/>Stock Check| InventoryGRPC
    OrderGRPC -.->|gRPC Call<br/>Order Processing| ShipmentGRPC
    UserGRPC -.->|gRPC Call<br/>User Context| OrderGRPC
    NotificationGRPC -.->|gRPC Stream<br/>Event Notifications| UserGRPC
    
    %% Event-Driven Communication (1/Ο† async)
    ShipmentGRPC ==>|Publish Events<br/>Shipment Created| EventBus
    TrackingGRPC ==>|Publish Events<br/>Location Updated| EventBus
    EventBus -.->|Subscribe Events<br/>Async Processing| NotificationGRPC
    EventBus -.->|Subscribe Events<br/>Analytics| StreamProcessor
    
    %% Data Layer Connections (Ο† distributed)
    ShipmentGRPC ==>|Write/Read<br/>Ο† Priority| ShipmentDB
    TrackingGRPC ==>|Write/Read<br/>Ο† Priority| TrackingDB
    InventoryGRPC -->|Write/Read<br/>Standard| InventoryDB
    OrderGRPC -->|Write/Read<br/>Standard| ShipmentDB
    
    %% Cache Layer Connections
    ShipmentGRPC -.->|Cache Operations<br/>Performance| RedisCluster
    TrackingGRPC -.->|Search Operations<br/>Location Queries| ElasticSearch
    InventoryGRPC -.->|Cache Operations<br/>Stock Levels| RedisCluster
    
    %% Golden Ratio Color Scheme (Ο† visual hierarchy)
    style ServiceRegistry fill:#2c3e50,stroke:#1a252f,stroke-width:5px,color:#ffffff
    style LoadBalancer fill:#27ae60,stroke:#229954,stroke-width:4px,color:#ffffff
    style HealthCheck fill:#3498db,stroke:#2980b9,stroke-width:3px,color:#ffffff
    
    style ShipmentGRPC fill:#e74c3c,stroke:#c0392b,stroke-width:5px,color:#ffffff
    style TrackingGRPC fill:#3498db,stroke:#2980b9,stroke-width:5px,color:#ffffff
    style InventoryGRPC fill:#f39c12,stroke:#e67e22,stroke-width:3px,color:#ffffff
    style OrderGRPC fill:#27ae60,stroke:#229954,stroke-width:3px,color:#ffffff
    style UserGRPC fill:#9b59b6,stroke:#8e44ad,stroke-width:2px,color:#ffffff
    style NotificationGRPC fill:#34495e,stroke:#2c3e50,stroke-width:2px,color:#ffffff
    
    style EventBus fill:#ff6b6b,stroke:#ee5a52,stroke-width:4px,color:#ffffff
    style MessageQueue fill:#4ecdc4,stroke:#45b7aa,stroke-width:3px,color:#ffffff
    style StreamProcessor fill:#45b7d1,stroke:#3498db,stroke-width:3px,color:#ffffff
    
    style ShipmentDB fill:#4caf50,stroke:#388e3c,stroke-width:5px,color:#ffffff
    style TrackingDB fill:#ff9800,stroke:#f57c00,stroke-width:5px,color:#ffffff
    style InventoryDB fill:#607d8b,stroke:#455a64,stroke-width:3px,color:#ffffff
    style RedisCluster fill:#ff5722,stroke:#e64a19,stroke-width:3px,color:#ffffff
    style ElasticSearch fill:#795548,stroke:#5d4037,stroke-width:3px,color:#ffffff
Loading

πŸš€ Getting Started

Prerequisites

  • Node.js (v16+ recommended)
  • npm or yarn
  • MariaDB Server (v10.5+)
  • Basic understanding of gRPC and microservices

Installation

  1. Clone the repository

    git clone https://github.com/abdoElHodaky/logisticsassgrpc.git
    cd logisticsassgrpc
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Configure environment variables

    cp .env.example .env

    Update .env with your configuration:

    PORT=3000
    DB_HOST=localhost
    DB_PORT=3306
    DB_USER=your_user
    DB_PASSWORD=your_password
    DB_NAME=logistics_db
    GRPC_SERVICE_HOST=localhost
    GRPC_SERVICE_PORT=50051
  4. Run database migrations

    npm run migration:run
  5. Start the development server

    npm run dev

πŸ“š API Endpoints

Core Endpoints

The API Gateway exposes the following endpoint categories:

  • πŸ“¦ Shipments: Create, track, and manage shipments
  • πŸ“Š Inventory: Monitor and update inventory levels
  • πŸš› Tracking: Real-time shipment tracking and status updates
  • πŸ“‹ Orders: Order processing and management
  • πŸ‘₯ Users: User authentication and profile management

Example Endpoints

GET    /api/shipments          - List all shipments
POST   /api/shipments          - Create new shipment
GET    /api/shipments/:id      - Get shipment details
PUT    /api/shipments/:id      - Update shipment
DELETE /api/shipments/:id      - Delete shipment

GET    /api/tracking/:id       - Track shipment status
GET    /api/inventory          - View inventory
POST   /api/orders             - Create new order

πŸ”§ Development

Available Scripts

npm run dev          # Start development server with hot reload
npm run build        # Build for production
npm run start        # Start production server
npm run test         # Run test suite
npm run lint         # Lint code
npm run migration:generate  # Generate new migration
npm run migration:run       # Run pending migrations

Project Structure

logisticsassgrpc/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ controllers/     # Request handlers
β”‚   β”œβ”€β”€ services/        # Business logic & gRPC clients
β”‚   β”œβ”€β”€ entities/        # TypeORM database entities
β”‚   β”œβ”€β”€ routes/          # API route definitions
β”‚   β”œβ”€β”€ middleware/      # Express middleware
β”‚   β”œβ”€β”€ config/          # Configuration files
β”‚   └── proto/           # gRPC protocol buffer definitions
β”œβ”€β”€ migrations/          # Database migrations
β”œβ”€β”€ tests/              # Test files
└── package.json

πŸ” Security Features

  • Input validation and sanitization
  • Rate limiting on API endpoints
  • CORS configuration
  • SQL injection protection via TypeORM
  • Environment-based configuration

πŸ§ͺ Testing

# Run all tests
npm test

# Run tests with coverage
npm run test:coverage

# Run specific test suite
npm test -- shipments.test.ts

πŸ“ˆ Performance

  • gRPC Protocol: Binary protocol for efficient service-to-service communication
  • Connection Pooling: Optimized database connections
  • Async/Await: Non-blocking asynchronous operations
  • Caching: Redis integration ready for response caching

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘€ Author

abdoElHodaky


πŸ™ Acknowledgments

  • Express.js community for the robust web framework
  • gRPC team for the efficient RPC framework
  • TypeORM contributors for the excellent ORM
  • MariaDB Foundation for the reliable database

πŸ“ž Support

For issues, questions, or contributions, please open an issue on GitHub or contact the maintainer.


⭐ Star this repository if you find it helpful!

Made with ❀️ for the logistics industry


πŸ“ One-Line Summary

A production-ready TypeScript API Gateway that bridges RESTful HTTP clients with gRPC-based logistics microservices (shipments, inventory, tracking, orders) using Express.js for routing and TypeORM with MariaDB for type-safe data persistence in an enterprise-grade, scalable microservices architecture.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors