How aditya18mew/api-gateway works
1. This repository is a small Node.js microservices system built around an API gateway plus three domain services: Auth, User, and Product. The top-level compose.yaml suggests the whole stack is intended to run together locally with container orchestration.
2. The gateway/ service is the public entrypoint. It contains route wiring, shared middleware, service discovery/configuration, and a public/docs.html page for API documentation or a basic UI for testing. In architecture terms, this is the only component clients should talk to directly.
3. The gateway also hosts the main cross-cutting controls: Authmiddleware, ratelimiter, circuitbreaker, and logger. That means authentication/authorization, traffic shaping, resilience, and request logging are enforced before requests are forwarded to downstream services.
4. The gateway/config/services.js and gateway/config/Redis.js files indicate the gateway knows where the downstream services live and uses Redis for shared state. Redis is likely supporting rate limiting and/or circuit breaker state rather than business data.
5. services/Auth-service/ is the credential and token boundary. Its utilities include bcrypt.js and generatejwt.js, which strongly suggests this service hashes passwords and issues JWTs. Its routes are isolated from the rest of the system, so login/registration/token flows stay separate from domain data services.
6. services/user-service/ and services/product-service/ are the domain backends. Each has its own index.js, routes/, config/, and utils/mongoosedb.js, which implies each service owns its own HTTP API and its own MongoDB connection setup.
7. The presence of mongoosedb.js in all three services points to a MongoDB-backed design, with persistence localized inside each service rather than through a shared database access layer. That keeps the service boundaries clear for an architecture diagram: each service is independently deployable and data-owning.
8. Request flow is therefore: client → gateway → appropriate service. For protected endpoints, the gateway authenticates the request first, then forwards it. For auth flows, the client likely hits the auth service through the gateway to obtain a JWT, which is then reused on later requests.
9. Operationally, compose.yaml, .env.example files in each service, and per-service config/ folders show the system is environment-driven. Ports, backend URLs, Redis, and database settings are intended to be injected at runtime rather than hardcoded.
10. There is little evidence of shared business logic across services; the main shared concerns live in the gateway, while domain logic is siloed per service. For an architecture diagram, the important boundaries are: external clients, gateway, Redis for shared control-state, and the three isolated HTTP + Mongo-backed services.
Main entry points: gateway/index.js Gateway · Auth-service/index.js Auth service · compose.yaml Compose · routes.js Routes
Components 19
Gateway
Public entrypoint and cross-cutting controls
- Gateway · Public HTTP entrypoint
gateway/index.js - Routes · Request forwarding and endpoint wiring
gateway/routes/routes.js - Auth guard · Authentication and authorization checks
gateway/middleware/Authmiddleware.js - Rate limit · Traffic shaping and throttling
gateway/middleware/ratelimiter.js - Circuit breaker · Resilience and downstream protection
gateway/middleware/circuitbreaker.js - Logger · Request logging and observability
gateway/middleware/logger.js - Service config · Downstream service locations
gateway/config/services.js - Redis config · Shared control-state backend settings
gateway/config/Redis.js - Docs UI · Public documentation and test page
gateway/public/docs.html
Services
Domain services with isolated APIs and data
- Auth service · Credentials and token boundary
services/Auth-service/index.js - Auth routes · Login and token endpoints
services/Auth-service/routes/routes.js - JWT utils · Password hashing and token issuance
services/Auth-service/utils/generatejwt.js - Auth DB · Service-local persistence
services/Auth-service/utils/mongoosedb.js - User service · User domain backend
services/user-service/index.js - User DB · Service-local persistence
services/user-service/utils/mongoosedb.js - Product service · Product domain backend
services/product-service/index.js - Product DB · Service-local persistence
services/product-service/utils/mongoosedb.js
Other components
- Client · Consumers of the API
Connections 21
- Client → Gateway: calls · All client traffic enters through the gateway
- Compose → Gateway: starts · Compose runs the gateway alongside services
- Compose → Auth service: starts · Compose runs the auth service
- Compose → User service: starts · Compose runs the user service
- Compose → Product service: starts · Compose runs the product service
- Gateway → Routes: uses · Gateway routes incoming requests
- Gateway → Auth guard: enforces · Authentication/authorization happens before forwarding
- Gateway → Rate limit: enforces · Gateway applies throttling controls
- Gateway → Circuit breaker: enforces · Gateway protects downstreams with circuit breaking
- Gateway → Logger: uses · Gateway logs request flow
- Gateway → Service config: reads · Gateway resolves downstream endpoints
- Gateway → Redis config: connects · Gateway uses Redis for shared control state
- Gateway → Docs UI: serves · Gateway exposes docs/test page
- Routes → Auth service: forwards to · Auth-related requests go to the auth service
- Routes → User service: forwards to · User-related requests go to the user service
- Routes → Product service: forwards to · Product-related requests go to the product service
- Auth service → Auth routes: exposes · Auth endpoints are defined in service routes
- Auth service → JWT utils: uses · Auth service hashes passwords and issues JWTs
- Auth service → Auth DB: persists to · Auth service owns its MongoDB connection
- User service → User DB: persists to · User service owns its MongoDB connection
- Product service → Product DB: persists to · Product service owns its MongoDB connection