Skip to content

Latest commit

Β 

History

History
521 lines (404 loc) Β· 12.8 KB

File metadata and controls

521 lines (404 loc) Β· 12.8 KB

Last Mile Delivery Platform - Deployment Guide

πŸš€ Deployment Overview

The Last Mile Delivery Platform is now ready for deployment with comprehensive API versioning, backward compatibility, and all core features implemented.

βœ… Implementation Status

Completed Features

  • βœ… User Authentication & Role Management (V1 & V2)
  • βœ… Enhanced Offer Management with geospatial queries
  • βœ… Location Services with real-time tracking
  • βœ… Notification System (multi-channel support)
  • βœ… Payment Processing & earnings tracking
  • βœ… Delivery Tracking with real-time updates
  • βœ… Error Handling & comprehensive validation
  • βœ… Admin Dashboard & monitoring
  • βœ… Performance Optimization with Redis caching
  • βœ… Role-Specific Dashboard APIs
  • βœ… API Versioning & backward compatibility

API Versions Available

  • V1.0: Current stable version with backward compatibility
  • V2.0: Enhanced version with advanced security and features

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend      β”‚    β”‚   API Gateway   β”‚    β”‚   Backend       β”‚
β”‚   (React)       │◄──►│   (Express)     │◄──►│   (Node.js)     β”‚
β”‚                 β”‚    β”‚   - Versioning  β”‚    β”‚   - Business    β”‚
β”‚                 β”‚    β”‚   - Auth        β”‚    β”‚   - Services    β”‚
β”‚                 β”‚    β”‚   - Rate Limit  β”‚    β”‚   - Models      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Database      β”‚    β”‚   Cache         β”‚
                       β”‚   (MongoDB)     β”‚    β”‚   (Redis)       β”‚
                       β”‚   - Users       β”‚    β”‚   - Sessions    β”‚
                       β”‚   - Offers      β”‚    β”‚   - Offers      β”‚
                       β”‚   - Payments    β”‚    β”‚   - Locations   β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ Environment Setup

Required Environment Variables

Create a .env file with the following variables:

# Database
MONGO_URI=mongodb://localhost:27017/lastmile
REDIS_URL=redis://localhost:6379

# Authentication
JWT_SECRET=your-super-secure-jwt-secret-key-here
JWT_REFRESH_SECRET=your-refresh-token-secret-key-here

# API Configuration
NODE_ENV=production
PORT=5000

# External Services (Optional)
GEOCODING_API_KEY=your-geocoding-api-key
EMAIL_SERVICE_API_KEY=your-email-service-key
PAYMENT_GATEWAY_API_KEY=your-payment-gateway-key

# Security
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100

# Caching
CACHE_TTL=3600
CACHE_MAX_SIZE=1000

Dependencies Installation

# Backend dependencies
npm install

# Frontend dependencies (if deploying frontend)
cd frontend
npm install
cd ..

πŸš€ Deployment Options

Option 1: Heroku Deployment (Recommended for Quick Start)

  1. Prepare for Heroku

    # Install Heroku CLI
    # Create Procfile (already exists)
    echo "web: node server.js" > Procfile
  2. Deploy to Heroku

    # Login to Heroku
    heroku login
    
    # Create Heroku app
    heroku create your-app-name
    
    # Add MongoDB Atlas addon
    heroku addons:create mongolab:sandbox
    
    # Add Redis addon
    heroku addons:create heroku-redis:hobby-dev
    
    # Set environment variables
    heroku config:set JWT_SECRET=your-jwt-secret
    heroku config:set NODE_ENV=production
    
    # Deploy
    git add .
    git commit -m "Deploy to Heroku"
    git push heroku main
  3. Configure Database

    # Get MongoDB connection string
    heroku config:get MONGODB_URI
    
    # Get Redis URL
    heroku config:get REDIS_URL

Option 2: AWS Deployment

  1. EC2 Instance Setup

    # Launch EC2 instance (Ubuntu 20.04 LTS)
    # SSH into instance
    ssh -i your-key.pem ubuntu@your-ec2-ip
    
    # Install Node.js
    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    # Install PM2 for process management
    sudo npm install -g pm2
    
    # Install MongoDB
    wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    sudo apt-get update
    sudo apt-get install -y mongodb-org
    
    # Install Redis
    sudo apt-get install redis-server
  2. Deploy Application

    # Clone repository
    git clone your-repo-url
    cd lastmile-delivery-platform
    
    # Install dependencies
    npm install
    
    # Set up environment variables
    cp .env.example .env
    # Edit .env with your values
    
    # Start with PM2
    pm2 start server.js --name "lastmile-api"
    pm2 startup
    pm2 save
  3. Configure Nginx (Optional)

    # Install Nginx
    sudo apt-get install nginx
    
    # Configure reverse proxy
    sudo nano /etc/nginx/sites-available/lastmile
    server {
        listen 80;
        server_name your-domain.com;
        
        location / {
            proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_bypass $http_upgrade;
        }
    }

Option 3: Docker Deployment

  1. Create Docker Configuration

    # Dockerfile (create this file)
    FROM node:18-alpine
    
    WORKDIR /app
    
    COPY package*.json ./
    RUN npm ci --only=production
    
    COPY . .
    
    EXPOSE 5000
    
    CMD ["node", "server.js"]
  2. Docker Compose Setup

    # docker-compose.yml (create this file)
    version: '3.8'
    
    services:
      app:
        build: .
        ports:
          - "5000:5000"
        environment:
          - NODE_ENV=production
          - MONGO_URI=mongodb://mongo:27017/lastmile
          - REDIS_URL=redis://redis:6379
        depends_on:
          - mongo
          - redis
    
      mongo:
        image: mongo:6.0
        ports:
          - "27017:27017"
        volumes:
          - mongo_data:/data/db
    
      redis:
        image: redis:7-alpine
        ports:
          - "6379:6379"
    
    volumes:
      mongo_data:
  3. Deploy with Docker

    # Build and run
    docker-compose up -d
    
    # View logs
    docker-compose logs -f app

πŸ”’ Security Configuration

Production Security Checklist

  • βœ… Environment Variables: All secrets in environment variables
  • βœ… JWT Security: Strong secret keys with proper expiration
  • βœ… Rate Limiting: Configured for production traffic
  • βœ… Input Validation: Comprehensive validation middleware
  • βœ… CORS Configuration: Properly configured for your domain
  • βœ… HTTPS: Enable SSL/TLS in production
  • βœ… Database Security: MongoDB authentication enabled
  • βœ… Error Handling: No sensitive data in error responses

Additional Security Measures

// Add to server.js for production
if (process.env.NODE_ENV === 'production') {
  // Security headers
  app.use(helmet());
  
  // Trust proxy for Heroku/AWS
  app.set('trust proxy', 1);
  
  // Secure session configuration
  app.use(session({
    secret: process.env.SESSION_SECRET,
    secure: true, // HTTPS only
    httpOnly: true,
    maxAge: 24 * 60 * 60 * 1000 // 24 hours
  }));
}

πŸ“Š Monitoring & Logging

Application Monitoring

  1. Health Check Endpoint

    // Add to server.js
    app.get('/health', (req, res) => {
      res.json({
        status: 'healthy',
        timestamp: new Date().toISOString(),
        version: process.env.npm_package_version,
        uptime: process.uptime()
      });
    });
  2. Logging Configuration

    # Install logging dependencies
    npm install winston morgan
  3. Performance Monitoring

    • Use PM2 monitoring for Node.js metrics
    • Set up MongoDB monitoring
    • Configure Redis monitoring
    • Set up error tracking (e.g., Sentry)

πŸ§ͺ Testing in Production

API Testing

# Test version endpoints
curl https://your-domain.com/api/version

# Test authentication
curl -X POST https://your-domain.com/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{"name":"Test User","email":"test@example.com","password":"TestPass123!","role":"business"}'

# Test versioning
curl https://your-domain.com/api/v1/version
curl https://your-domain.com/api/v2/version

Load Testing

# Install artillery for load testing
npm install -g artillery

# Create load test configuration
# artillery-config.yml
config:
  target: 'https://your-domain.com'
  phases:
    - duration: 60
      arrivalRate: 10
scenarios:
  - name: "API Health Check"
    requests:
      - get:
          url: "/health"

πŸ”„ API Versioning in Production

Version Management

  • Current Version: V1 (stable)
  • Beta Version: V2 (enhanced features)
  • Deprecation Policy: 6 months notice before sunset
  • Migration Support: Automatic compatibility layer

Client Integration

// JavaScript client example
const API_BASE = 'https://your-domain.com/api';

// V1 client (backward compatible)
const v1Client = {
  baseURL: `${API_BASE}/v1`,
  headers: { 'Accept-Version': 'v1' }
};

// V2 client (enhanced features)
const v2Client = {
  baseURL: `${API_BASE}/v2`,
  headers: { 'Accept-Version': 'v2' }
};

πŸ“ˆ Scaling Considerations

Horizontal Scaling

  1. Load Balancer Configuration

    • Use Nginx or AWS ALB
    • Session affinity for WebSocket connections
    • Health check configuration
  2. Database Scaling

    • MongoDB replica sets
    • Read replicas for analytics
    • Sharding for large datasets
  3. Cache Scaling

    • Redis Cluster for high availability
    • Cache warming strategies
    • Cache invalidation patterns

Performance Optimization

  • Database Indexing: All geospatial and frequently queried fields
  • Redis Caching: User sessions, offer data, location data
  • CDN: Static assets and API responses
  • Compression: Gzip compression enabled
  • Connection Pooling: MongoDB and Redis connection pools

🚨 Troubleshooting

Common Issues

  1. Database Connection Issues

    # Check MongoDB status
    sudo systemctl status mongod
    
    # Check connection string
    echo $MONGO_URI
  2. Redis Connection Issues

    # Check Redis status
    redis-cli ping
    
    # Check Redis configuration
    redis-cli config get "*"
  3. API Version Issues

    # Test version detection
    curl -H "Accept-Version: v1" https://your-domain.com/api/version
    curl -H "Accept-Version: v2" https://your-domain.com/api/version

Logs and Debugging

# PM2 logs
pm2 logs lastmile-api

# Application logs
tail -f logs/app.log

# MongoDB logs
tail -f /var/log/mongodb/mongod.log

# Redis logs
tail -f /var/log/redis/redis-server.log

πŸ“ž Support and Maintenance

Backup Strategy

  1. Database Backups

    # MongoDB backup
    mongodump --uri="$MONGO_URI" --out=/backup/$(date +%Y%m%d)
  2. Redis Backups

    # Redis backup
    redis-cli BGSAVE

Update Strategy

  1. Rolling Updates

    • Deploy to staging first
    • Test all API versions
    • Gradual production rollout
    • Monitor error rates
  2. Rollback Plan

    • Keep previous version ready
    • Database migration rollback scripts
    • Quick rollback procedures

πŸŽ‰ Deployment Complete!

Your Last Mile Delivery Platform is now ready for production with:

  • βœ… Comprehensive API versioning
  • βœ… Backward compatibility
  • βœ… Production-ready security
  • βœ… Scalable architecture
  • βœ… Monitoring and logging
  • βœ… Complete feature set

Next Steps

  1. Choose your deployment option
  2. Configure environment variables
  3. Set up monitoring
  4. Test all API endpoints
  5. Configure domain and SSL
  6. Set up automated backups
  7. Monitor performance and scale as needed

Your platform is ready to connect businesses with riders for efficient last-mile delivery! πŸššπŸ“¦