Sign inSign up

carfdev/carfdev-gateway

By carfdev

β€’Updated 12 months ago

Production-ready API Gateway. Gin + NATS integration. Role-based auth. Minimal footprint.

Image
0

352

carfdev/carfdev-gateway repository overview

⁠API Gateway for NATS Microservices

High-performance API Gateway built with Go and Gin framework. Provides centralized HTTP entry point for NATS-based microservices with authentication, routing, and service orchestration.

β πŸš€ Quick Start

docker run -d \
  --name gateway \
  -p 3000:3000 \
  -e NATS_URL=nats://your-nats-server:4222 \
  -e ENV=production \
  -e DOMAIN=yourdomain.com \
  carfdev/carfdev-gateway:latest

⁠✨ Features

  • RESTful API Gateway: Centralized HTTP entry point for microservices
  • NATS Integration: Asynchronous request/response with microservices
  • Authentication Middleware: Role-based access control (RBAC)
  • Service Orchestration: Routes HTTP requests to appropriate NATS subjects
  • Multi-Architecture: Native support for linux/amd64 and linux/arm64
  • Production Ready: Minimal footprint (~12MB), graceful shutdown, structured logging
  • Contact Form API: Pre-built endpoint for email service integration

β πŸ“‹ Environment Variables

VariableDescriptionRequiredDefault
PORTHTTP server portNo8080
GIN_MODEGin mode (debug/release)Nodebug
ENVEnvironment (development/production)Nodevelopment
DOMAINDomain for cookiesNolocalhost:8080
NATS_URLNATS server URLNonats://localhost:4222

β πŸ“‘ API Endpoints

⁠Email Service
⁠POST /v1/email/send-contact

Send contact form submissions to email service via NATS.

Request:

{
  "firstName": "John",
  "lastName": "Doe",
  "email": "[email protected]",
  "companyName": "Acme Corp",
  "projectType": "new-website",
  "budget": "50k-100k",
  "message": "We need help with..."
}

Response:

{
  "status": 200,
  "data": {
    "message": "Contact email sent successfully"
  },
  "timestamp": 1699632000
}

Project Types: new-website, e-commerce, redesign, web-app, optimization, other

Budget Options: under-50k, 50k-100k, 100k-200k, 200k-plus

β πŸ” Authentication

Protected routes use Bearer token authentication:

curl -X GET https://api.yourdomain.com/v1/protected \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

The middleware validates tokens via NATS (users.check_access subject) and supports role-based access control.

⁠🐳 Docker Compose Example

version: '3.8'
services:
  nats:
    image: nats:latest
    ports:
      - "4222:4222"

  emailsvc:
    image: carfdev/carfdev-emailsvc:latest
    environment:
      - NATS_URL=nats://nats:4222
      - EMAIL_KEY=${RESEND_API_KEY}
    depends_on:
      - nats

  gateway:
    image: carfdev/carfdev-gateway:latest
    ports:
      - "3000:3000"
    environment:
      - PORT=3000
      - NATS_URL=nats://nats:4222
      - ENV=production
      - DOMAIN=yourdomain.com
      - GIN_MODE=release
    depends_on:
      - nats
      - emailsvc
    restart: unless-stopped

⁠☸️ Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
spec:
  replicas: 3
  selector:
    matchLabels:
      app: gateway
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
      - name: gateway
        image: carfdev/carfdev-gateway:latest
        ports:
        - containerPort: 8080
        env:
        - name: PORT
          value: "8080"
        - name: NATS_URL
          value: "nats://nats-service:4222"
        - name: ENV
          value: "production"
        - name: GIN_MODE
          value: "release"
        resources:
          requests:
            memory: "32Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
  - port: 80
    targetPort: 8080
  type: LoadBalancer

β πŸ”„ NATS Integration

The gateway communicates with microservices via NATS subjects:

  • email.send_contact - Forward contact form to email service
  • users.check_access - Validate authentication tokens

Envelope Format:

{
  "data": { ... },
  "error": {
    "code": "ERROR_CODE",
    "message": "Error description"
  }
}

β πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Clients   │──HTTP──→│ Gateway  │──NATS──→│Microservicesβ”‚
β”‚ (Web/Mobile)β”‚         β”‚ (REST)   β”‚         β”‚  (Email,    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚   Users)    β”‚
                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β πŸ“Š Performance

  • Memory: ~20MB base footprint
  • Throughput: 10,000+ requests/second per instance
  • Latency: <10ms gateway overhead (excluding service calls)
  • Image Size: ~12MB compressed

β πŸ”’ Security

  • Minimal attack surface with scratch-based image
  • Bearer token authentication with NATS validation
  • Role-based access control (RBAC)
  • Secure cookie handling (HttpOnly, Secure)
  • Request validation with strict JSON binding
  • No sensitive data in logs

β πŸ“ˆ Scaling

The gateway is stateless and scales horizontally:

# Scale to 5 replicas
kubectl scale deployment gateway --replicas=5

# Or with Docker Compose
docker-compose up --scale gateway=5

⁠🎯 Use Cases

  • Microservices Architecture: Central entry point for distributed services
  • API Aggregation: Combine multiple service calls into single endpoints
  • Authentication Gateway: Centralized token validation
  • Service Mesh: HTTP-to-NATS translation layer
  • Contact Forms: Pre-built integration with email service

β πŸ“ License

MIT License - see repository for details

β πŸ‘¨β€πŸ’» Author

carfdev - Building cloud-native microservices


Tags: api-gateway, microservices, nats, golang, gin, rest-api, docker, kubernetes, cloud-native

Tag summary

Content type

Image

Digest

sha256:0a0bcfebe…

Size

14.7 MB

Last updated

12 months ago

docker pull carfdev/carfdev-gateway