Skip to content

Gateway

The Gateway is the API gateway for the backend. It acts as a reverse proxy, providing a single entry point for all client requests and routing them to the appropriate microservice. It also handles several cross-cutting concerns.

Responsibilities

  • Request Routing: Routes incoming requests to the correct microservice based on the URL path.
  • Single Entry Point: Simplifies the client-side configuration by providing a single URL for all backend services.
  • Security: Applies security middleware to all incoming requests.
  • CORS: Handles Cross-Origin Resource Sharing (CORS) for the entire backend.
  • Rate Limiting: Implements rate limiting to protect the backend services from abuse.
  • Request Logging: Logs incoming requests for monitoring and debugging.
  • Health Checks: Provides a /health endpoint to check the status of the gateway.

Middleware

The gateway uses several middleware from the @ce-sdr/shared package to handle cross-cutting concerns:

  • securityMiddleware(): Applies various security headers.
  • gatewayCors(): Configures CORS for allowed origins.
  • rateLimiter(): Limits the number of requests per IP address.
  • requestLogger("api-gateway"): Logs incoming requests with a "api-gateway" tag.
  • healthCheck("api-gateway"): Provides a health check endpoint.
  • notFoundHandler(): Handles requests to non-existent routes.
  • errorHandler(): A centralized error handler.

Routing Flow

The following diagram illustrates how the gateway routes requests to the different backend services.

Routing Table

The gateway uses a path-based routing strategy. Here is the routing configuration based on apps/gateway/src/server.ts:

Path PrefixTarget ServiceTarget URL (from env)
/auth-serviceAuth ServiceAUTH_SERVICE_URL
/brand-serviceBrand ServiceBRAND_SERVICE_URL
/campaigns-serviceCampaign ServiceCAMPAIGNS_SERVICE_URL
/healthGateway (itself)-

Configuration

The URLs for the downstream services are configured via environment variables. For more details, please refer to the Configuration documentation.

Structure

The gateway is located in the apps/gateway directory.

apps/gateway
├── src
│   ├── index.ts
│   └── server.ts
├── package.json
├── tsconfig.json
└── turbo.json