# expressgateway/express-gateway architecture

> Architecture diagram of the GitHub repository expressgateway/express-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/expressgateway/express-gateway
- Repository: https://github.com/expressgateway/express-gateway

## Overview

##### Main Components

a. Core Gateway Layer
- Main Express.js gateway server (lib/gateway/server.js)
- Pipeline processor (lib/gateway/pipelines.js)
- Context handler (lib/gateway/context.js)

b. Policy Management Layer
- Policy engine (lib/policies)
- Core policies:
- Authentication (basic-auth, oauth2, jwt)
- Security (cors)
- Traffic Management (rate-limit)
- Request/Response Transformation
- Proxy handling

c. Admin and Configuration Layer
- Admin API (admin/)
- Configuration management (lib/config)
- Schema validation (lib/schemas)

d. Consumer Management Layer
- User management (lib/services/consumers)
- Application management
- Credential management
- Token services

##### Relationship Flows

- Incoming requests → Gateway Server
- Gateway Server → Pipeline Processor → Policies
- Admin API → Configuration Management
- Configuration Management → Gateway Server
- Consumer Management ↔ Authentication Policies

##### Component Details

Gateway Core:
**┌─────────────────────────┐**
- Express Gateway     │
- ┤
- Pipeline Processor    │
- Policy Engine        │
- Context Handler      │
- ┘

Policy Layer:
**┌─────────────────┐**
- Policy Engine   │
- ┤
- Auth Policies   │
- Security        │
- Traffic Mgmt    │
- Transformations │
- ┘

##### Implementation Notes

- Distributed nature of the gateway
- Plugin architecture
- CLI interface
- Configuration stores
- Demonstrate policy pipeline flow

##### Additional Elements

- Version information
- External integration points
- Scalability points

##### Specific Architectural Patterns

- Middleware chain pattern
- Plugin architecture
- Event-driven architecture (lib/eventBus.js)
- Service-oriented architecture

- Request processing
- Authentication/Authorization
- Policy enforcement
- Proxy capabilities
- Consumer management
- Configuration management

Remember to maintain a balance between detail and clarity, focusing on the core architectural components while providing enough detail to understand the system's operation.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and classes
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef policy fill:#2ecc71,stroke:#27ae60,color:white
    classDef admin fill:#f1c40f,stroke:#f39c12,color:black
    classDef consumer fill:#e67e22,stroke:#d35400,color:white
    classDef storage fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef external fill:#ecf0f1,stroke:#bdc3c7,color:black

    %% External Entry Points
    Client["Client Requests"]:::external
    AdminClient["Admin Clients"]:::external

    %% Core Gateway Layer
    subgraph Gateway
        Server["Gateway Server"]:::core
        Pipeline["Pipeline Processor"]:::core
        Context["Context Handler"]:::core
    end

    %% Policy Management Layer
    subgraph Policies
        PolicyEngine["Policy Engine"]:::policy
        subgraph AuthPolicies["Authentication Policies"]
            BasicAuth["Basic Auth"]:::policy
            OAuth2["OAuth2"]:::policy
            JWT["JWT"]:::policy
        end
        subgraph SecurityPolicies["Security Policies"]
            CORS["CORS"]:::policy
            RateLimit["Rate Limit"]:::policy
        end
        subgraph TransformPolicies["Transformation Policies"]
            ReqTransform["Request Transformer"]:::policy
            ResTransform["Response Transformer"]:::policy
            Proxy["Proxy Handler"]:::policy
        end
    end

    %% Admin and Configuration Layer
    subgraph AdminLayer["Admin & Configuration"]
        AdminAPI["Admin API"]:::admin
        Config["Configuration Management"]:::admin
        Schema["Schema Validation"]:::admin
        ConfigModels["Config Models"]:::admin
    end

    %% Consumer Management Layer
    subgraph ConsumerMgmt["Consumer Management"]
        UserMgmt["User Management"]:::consumer
        AppMgmt["Application Management"]:::consumer
        CredentialMgmt["Credential Management"]:::consumer
        TokenService["Token Services"]:::consumer
    end

    %% Relationships
    Client --> Server
    Server --> Pipeline
    Pipeline --> Context
    Pipeline --> PolicyEngine
    PolicyEngine --> AuthPolicies
    PolicyEngine --> SecurityPolicies
    PolicyEngine --> TransformPolicies
    AdminClient --> AdminAPI
    AdminAPI --> Config
    Config --> Server
    ConsumerMgmt <--> AuthPolicies

    %% Click events for component mapping
    click Server "https://github.com/ExpressGateway/express-gateway/blob/master/lib/gateway/server.js"
    click Pipeline "https://github.com/ExpressGateway/express-gateway/blob/master/lib/gateway/pipelines.js"
    click Context "https://github.com/ExpressGateway/express-gateway/blob/master/lib/gateway/context.js"
    click PolicyEngine "https://github.com/ExpressGateway/express-gateway/blob/master/lib/policies/index.js"
    click BasicAuth "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/basic-auth"
    click OAuth2 "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/oauth2"
    click JWT "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/jwt"
    click CORS "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/cors"
    click RateLimit "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/rate-limit"
    click ReqTransform "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/request-transformer"
    click ResTransform "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/response-transformer"
    click Proxy "https://github.com/ExpressGateway/express-gateway/tree/master/lib/policies/proxy"
    click AdminAPI "https://github.com/ExpressGateway/express-gateway/blob/master/admin/index.js"
    click Config "https://github.com/ExpressGateway/express-gateway/blob/master/lib/config/config.js"
    click Schema "https://github.com/ExpressGateway/express-gateway/blob/master/lib/schemas/index.js"
    click ConfigModels "https://github.com/ExpressGateway/express-gateway/tree/master/lib/config/models"
    click UserMgmt "https://github.com/ExpressGateway/express-gateway/blob/master/lib/services/consumers/user.service.js"
    click AppMgmt "https://github.com/ExpressGateway/express-gateway/blob/master/lib/services/consumers/application.service.js"
    click CredentialMgmt "https://github.com/ExpressGateway/express-gateway/blob/master/lib/services/credentials/credential.service.js"
    click TokenService "https://github.com/ExpressGateway/express-gateway/blob/master/lib/services/tokens/token.service.js"
```
