# shelepuginivan/lunatic architecture

> Architecture diagram of the GitHub repository shelepuginivan/lunatic, 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/shelepuginivan/lunatic
- Repository: https://github.com/shelepuginivan/lunatic

## Overview

##### Main Components

a) Core Components:
- Server Component
- Router Component
- Request/Response Handlers
- Middleware System
- Type System

b) Built-in Middlewares:
- Body Parser
- Cookie Parser
- CORS Handler
- Form Parser
- Static File Server
- Not Found Handler

**┌─ Application Layer ─────────────────────┐**

- User Application Code              │

- Framework Core ──────────────────────┬─┤

- ┌─────────────┐    ┌──────────────┐ │ │
- Server    │◄───┤    Router    │ │ │
- ┘    └──────────────┘ │ │
- ▲                 ▲         │ │

- ┌─────────────┐    ┌──────────────┐ │ │
- Request   │    │   Response   │ │ │
- ┘    └──────────────┘ │ │

- Middleware Layer ───────────────────┤ │
- ┌─────────┐ ┌────────┐ ┌─────────┐  │ │
- Body   │ │ Cookie │ │  CORS   │  │ │
- Parser  │ │ Parser │ │ Handler │  │ │
- ┘ └────────┘ └─────────┘  │ │
- ┌─────────┐ ┌────────┐ ┌─────────┐  │ │
- Form   │ │ Static │ │   404   │  │ │
- Parser  │ │ Server │ │ Handler │  │ │
- ┘ └────────┘ └─────────┘  │ │

- Utilities ────────────────────────┬─┤ │
- ┌─────────┐ ┌────────┐ ┌────────┐ │ │ │
- MIME   │ │  Path  │ │ Other  │ │ │ │
- Types   │ │ Utils  │ │ Utils  │ │ │ │
- ┘ └────────┘ └────────┘ │ │ │
- ┘

##### Important Elements to Highlight

a) Request/Response Flow:
- How requests enter the system
- Middleware chain execution
- Demonstrate response generation and return path

b) Type System Integration:
- TypeScript interfaces and types
- Relationship between types and components

c) Middleware Pipeline:
- Demonstrate the sequential nature of middleware execution
- How middleware can be plugged into the system

##### Implementation Details

- HTTP method handlers (GET, POST, PUT, DELETE, PATCH)
- Static file serving capabilities
- Body parsing and form handling
- CORS configuration options
- Cookie management

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Application["Application Layer"]
        UserApp["User Application Code"]
    end

    subgraph Core["Framework Core"]
        Server["Server Component"]:::core
        Router["Router Component"]:::core
        Request["Request Handler"]:::core
        Response["Response Handler"]:::core

        Server --> Router
        Router --> Request
        Router --> Response
    end

    subgraph Middleware["Middleware Layer"]
        BodyParser["Body Parser"]:::middleware
        CookieParser["Cookie Parser"]:::middleware
        CORSHandler["CORS Handler"]:::middleware
        FormParser["Form Parser"]:::middleware
        StaticServer["Static File Server"]:::middleware
        NotFound["404 Handler"]:::middleware
    end

    subgraph Types["Type System"]
        HTTPTypes["HTTP Method Types"]:::type
        MiddlewareTypes["Middleware Types"]:::type
        RequestTypes["Request Handler Types"]:::type
        CookieTypes["Cookie Options Types"]:::type
        FileTypes["File Upload Types"]:::type
    end

    subgraph Utils["Utilities"]
        MIME["MIME Types"]:::util
        PathNorm["Path Normalization"]:::util
        PathTrim["Path Trimming"]:::util
        Constants["Constants"]:::util
    end

    %% Connections
    UserApp --> Server
    Server --> Middleware
    Middleware --> Router
    Router --> Types
    Utils --> Core

    %% Click Events
    click Server "https://github.com/shelepuginivan/lunatic/blob/main/src/server.ts"
    click Router "https://github.com/shelepuginivan/lunatic/blob/main/src/router.ts"
    click Request "https://github.com/shelepuginivan/lunatic/blob/main/src/request.ts"
    click Response "https://github.com/shelepuginivan/lunatic/blob/main/src/response.ts"
    click Types "https://github.com/shelepuginivan/lunatic/tree/main/src/types/"
    click Middleware "https://github.com/shelepuginivan/lunatic/tree/main/src/middlewares/"
    click BodyParser "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/body-parser.ts"
    click CookieParser "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/cookie-parser.ts"
    click CORSHandler "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/cors.ts"
    click FormParser "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/form-parser.ts"
    click StaticServer "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/serve-static.ts"
    click NotFound "https://github.com/shelepuginivan/lunatic/blob/main/src/middlewares/not-found.ts"
    click MIME "https://github.com/shelepuginivan/lunatic/blob/main/src/utils/mime.ts"
    click PathNorm "https://github.com/shelepuginivan/lunatic/blob/main/src/utils/normalize-path.ts"
    click PathTrim "https://github.com/shelepuginivan/lunatic/blob/main/src/utils/trim-path-start.ts"
    click Constants "https://github.com/shelepuginivan/lunatic/blob/main/src/utils/constants.ts"
    click HTTPTypes "https://github.com/shelepuginivan/lunatic/blob/main/src/types/http-method.ts"
    click MiddlewareTypes "https://github.com/shelepuginivan/lunatic/blob/main/src/types/middleware.ts"
    click RequestTypes "https://github.com/shelepuginivan/lunatic/blob/main/src/types/request-handler.ts"
    click CookieTypes "https://github.com/shelepuginivan/lunatic/blob/main/src/types/cookie-options.ts"
    click FileTypes "https://github.com/shelepuginivan/lunatic/blob/main/src/types/uploaded-file.ts"

    %% Styling
    classDef core fill:#2374ab
    classDef middleware fill:#57a773
    classDef type fill:#ffc857
    classDef util fill:#a4a8d1
    
    %% Legend
    subgraph Legend
        CoreLegend["Core Component"]:::core
        MiddlewareLegend["Middleware Component"]:::middleware
        TypeLegend["Type Definition"]:::type
        UtilLegend["Utility"]:::util
    end
```
