# eggjs/egg architecture

> Architecture diagram of the GitHub repository eggjs/egg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eggjs/egg
- Repository: https://github.com/eggjs/egg

## Overview

##### Main Components Identification

The system architecture should highlight these key components:

a) Core Framework Layer:
- Application (lib/application.js)
- Agent (lib/agent.js)
- Core Utilities (lib/core/*)
- Loader System (lib/loader/*)

b) Middleware Layer:
- Built-in middlewares (app/middleware/*)
- Security
- Body Parser
- Session
- Static Files

c) Extension Layer:
- Context Extensions (app/extend/*)
- Application Extensions
- Request/Response Extensions
- Helper Extensions

d) Plugin System:
- Plugin Loader
- Plugin Registry
- Custom Plugins

##### Relationship Flows

Key interactions:

a) Request Flow:
- HTTP Request → Middleware Chain → Router → Controller → Service → Response

b) Lifecycle Flow:
- Master Process → Agent Workers → App Workers
- Plugin Loading → Config Loading → Application Initialization

##### Architectural Patterns

Patterns:

- MVC Architecture (Controller/Service/Model)
- Plugin-based Extension System
- Multi-process Model
- Middleware Pipeline

- Blue: Core Framework Components
- Green: Middleware Layer
- Yellow: Extension System
- Orange: Plugin System
- Gray: External Systems/Interfaces

- Rectangles: Major Components
- Hexagons: Plugins
- Diamonds: Decision Points

##### Key Sections

a) Process Architecture:
**Master Process**
- Agent Worker(s)
- App Worker(s)

b) Request Pipeline:
**Request → Middleware → Router → Controller → Service → Response**

c) Plugin System:
**Framework**
- Built-in Plugins
- Framework Plugins
- Application Plugins

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Process Architecture"
        Master(("Master Process"))
        AgentW["Agent Workers"]
        AppW["App Workers"]
        
        Master --> AgentW
        Master --> AppW
    end

    subgraph "Core Framework"
        App["Application"]:::core
        Agent["Agent"]:::core
        CoreUtils["Core Utilities"]:::core
        Loader["Loader System"]:::core
        
        subgraph "Core Services"
            HTTPClient["HTTP Client"]:::service
            Logger["Logger"]:::service
            Messenger["Messenger"]:::service
        end
        
        subgraph "Core Base Classes"
            BaseContext["Base Context"]:::base
            BaseHook["Base Hook"]:::base
            BaseContextLogger["Base Context Logger"]:::base
        end
    end

    subgraph "Request Pipeline"
        Request(("HTTP Request"))
        Response(("HTTP Response"))
        
        subgraph "Middleware Layer"
            BodyParser["Body Parser"]:::middleware
            NotFound["Not Found Handler"]:::middleware
            MethodOverride["Method Override"]:::middleware
            StaticFiles["Static Files"]:::middleware
        end
        
        subgraph "Extension Layer"
            ContextExt["Context Extensions"]:::extension
            HelperExt["Helper Extensions"]:::extension
            RequestExt["Request Extensions"]:::extension
            ResponseExt["Response Extensions"]:::extension
        end
    end

    subgraph "Configuration"
        DefaultConfig["Default Config"]:::config
        PluginConfig["Plugin Config"]:::config
    end

    subgraph "Loader Components"
        AgentLoader["Agent Worker Loader"]:::loader
        AppLoader["App Worker Loader"]:::loader
        MainLoader["Main Loader"]:::loader
    end

    Request --> BodyParser --> MethodOverride --> StaticFiles --> NotFound --> Response

    AppW --> App
    AgentW --> Agent
    App --> CoreUtils
    Agent --> CoreUtils
    App --> Loader
    Agent --> Loader

    Loader --> MainLoader
    MainLoader --> AgentLoader
    MainLoader --> AppLoader

    App --> DefaultConfig
    App --> PluginConfig

    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef middleware fill:#4CAF50,stroke:#2E7D32,color:white
    classDef extension fill:#FFC107,stroke:#FFA000,color:black
    classDef service fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef config fill:#FF5722,stroke:#E64A19,color:white
    classDef base fill:#607D8B,stroke:#455A64,color:white
    classDef loader fill:#795548,stroke:#5D4037,color:white

    click App "https://github.com/eggjs/egg/blob/master/lib/application.js"
    click Agent "https://github.com/eggjs/egg/blob/master/lib/agent.js"
    click CoreUtils "https://github.com/eggjs/egg/tree/master/lib/core/*"
    click Loader "https://github.com/eggjs/egg/tree/master/lib/loader/*"
    click BodyParser "https://github.com/eggjs/egg/blob/master/app/middleware/body_parser.js"
    click NotFound "https://github.com/eggjs/egg/blob/master/app/middleware/notfound.js"
    click MethodOverride "https://github.com/eggjs/egg/blob/master/app/middleware/override_method.js"
    click StaticFiles "https://github.com/eggjs/egg/blob/master/app/middleware/site_file.js"
    click ContextExt "https://github.com/eggjs/egg/blob/master/app/extend/context.js"
    click HelperExt "https://github.com/eggjs/egg/blob/master/app/extend/helper.js"
    click RequestExt "https://github.com/eggjs/egg/blob/master/app/extend/request.js"
    click ResponseExt "https://github.com/eggjs/egg/blob/master/app/extend/response.js"
    click HTTPClient "https://github.com/eggjs/egg/blob/master/lib/core/httpclient.js"
    click Logger "https://github.com/eggjs/egg/blob/master/lib/core/logger.js"
    click Messenger "https://github.com/eggjs/egg/blob/master/lib/core/messenger/index.js"
    click DefaultConfig "https://github.com/eggjs/egg/blob/master/config/config.default.js"
    click PluginConfig "https://github.com/eggjs/egg/blob/master/config/plugin.js"
    click AgentLoader "https://github.com/eggjs/egg/blob/master/lib/loader/agent_worker_loader.js"
    click AppLoader "https://github.com/eggjs/egg/blob/master/lib/loader/app_worker_loader.js"
    click MainLoader "https://github.com/eggjs/egg/blob/master/lib/loader/index.js"
    click BaseContext "https://github.com/eggjs/egg/blob/master/lib/core/base_context_class.js"
    click BaseHook "https://github.com/eggjs/egg/blob/master/lib/core/base_hook_class.js"
    click BaseContextLogger "https://github.com/eggjs/egg/blob/master/lib/core/base_context_logger.js"
```
