# diegofmo0802/servercore architecture

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

- Interactive diagram: https://gitdiagram.com/diegofmo0802/servercore
- Repository: https://github.com/diegofmo0802/servercore

## Overview

##### Project type and purpose
- This is a Node.js/TypeScript library—“ServerCore”—that provides a lightweight framework for building HTTP/HTTPS servers, pluggable routing rules, WebSocket handling, templating, session/cookie management, plus optional “beta” modules for JWT and Mail.
- It is not a full-stack app but a server‐framework/toolkit intended for embedding in other projects.

##### File structure
- Top‐level:
- package.json, tsconfig.json, README.md, LICENSE
- global/ (static templates & CSS for the custom HSaml template engine)
- src/ (all TypeScript source code)
- tests/ & examples/
- src/ splits into:
- Beta/ (JwtManager.ts, Mail.ts — optional sub‐modules)
- Server/ (HTTP & HTTPS “Server.ts”, routing rules, Request/Response, BodyParser, Cookie, Session, WebSocket subfolder)
- Core files: ServerCore.ts (entry point), Template.ts (HSaml engine), ConsoleUI.ts, Debug.ts, Config.ts
- Utilities/ (Env, Path, general helper functions)

##### README for additional insights
- Emphasizes four routing‐rule types (Folder, File, Action, WebSocket) managed by ServerCore.Rule
- Shows how HTTP/HTTPS is initialized, how to register routes, and how to import Beta modules
- Mentions evolving template engine and future split of Beta modules

a. Main components
1. Client Layer
- HTTP/HTTPS clients (browsers, REST clients)
- WebSocket clients
2. ServerCore Library
- Entry: ServerCore class (src/ServerCore.ts)
- Underlying servers: Node HTTP.Server & HTTPS.Server (src/Server/Server.ts)
- Routing & Rule Engine (src/Server/Rule.ts)
- Request & Response abstractions (src/Server/Request.ts, Response.ts)
- Parsers & middleware:
– BodyParser (BodyParser.ts)
– Cookie manager (Cookie.ts)
– Session manager (Session.ts)
- WebSocket support (src/Server/WebSocket/WebSocket.ts + Chunk.ts)
- Template engine: Template.ts & global/Style + global/Template (custom HSaml)
- Beta namespace: JwtManager.ts, Mail.ts (optional pluggable modules)
- Utilities & config: Utilities/*.ts + Config.ts + Debug.ts + ConsoleUI.ts
3. External resources
- File system (static Folder/File handlers read from disk)
- SSL certificate files (for HTTPS)
- (Optional) SMTP server for Mail, Identity provider for JWT

b. Relationships and interactions
- ServerCore instantiates HTTP and/or HTTPS server listeners
- Incoming HTTP request → Node HTTP.Server emits → wrapped in custom Request object → middleware stack:
1. BodyParser
2. Cookie parser
3. Session loader
- Request passed to Rule Engine which matches against defined Rules:
- Folder/File → serves static content from disk
- Action → invokes user callback(request, response)
- WebSocket → upgrades to WS connection, hands off to WebSocket handler
- Response object sends data back over HTTP
- Template engine invoked by Action handlers or static rendering
- Beta.JwtManager and Beta.Mail injected or imported as needed by user code via ServerCore.Beta

c. Architectural patterns & design principles
- Modular architecture: core + optional Beta modules
- Rule‐based routing (pattern matching with wildcards and parameter extraction)
- Middleware‐style parsing pipeline (body, cookies, sessions)
- Adapter pattern: wrapping Node’s HTTP.Server in a higher‐level ServerCore API
- Extension points: addRule/addFolder/addFile/addAction/addWebSocket

d. Technologies, frameworks, and libraries
- Node.js built‐in http, https, fs, events
- TypeScript for source
- Custom HSaml template engine
- WebSocket protocol handling in src/Server/WebSocket

##### Key points to capture
- How ServerCore wraps Node’s HTTP/HTTPS primitives
- Detail the middleware → routing → handler flow
- Four rule types and WebSocket upgrade path
- Where template rendering and Beta modules fit in
- Do not omit configuration, utilities, and debug paths

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients"
        HTTPClient["HTTP/HTTPS Clients"]:::external
        WSClient["WebSocket Clients"]:::external
    end

    %% ServerCore Framework
    subgraph "ServerCore"
        %% Listener
        subgraph "Listener"
            HTTPListener["HTTP/HTTPS Listener"]:::core
        end

        %% Middleware Pipeline
        subgraph "Middleware"
            BodyParser["BodyParser"]:::middleware
            CookieMgr["Cookie Manager"]:::middleware
            SessionMgr["Session Manager"]:::middleware
        end

        %% Routing & Rule Engine
        RuleEngine["Routing & Rule Engine"]:::routing

        %% Handlers
        subgraph "Handlers"
            StaticHandler["Folder/File Handler"]:::handlers
            ActionHandler["Action Handler"]:::handlers
            WSHandler["WebSocket Handler"]:::handlers
        end

        %% Template Engine
        TemplateEngine["HSaml Template Engine"]:::template

        %% Beta Modules
        subgraph "Beta Modules"
            JwtMgr["JWT Manager"]:::beta
            MailMod["Mail Module"]:::beta
        end

        %% Utilities
        subgraph "Utilities"
            Config["Configuration"]:::utilities
            Debug["Debug Utilities"]:::utilities
            ConsoleUI["Console UI"]:::utilities
            EnvUtil["Env Helpers"]:::utilities
            PathUtil["Path Helpers"]:::utilities
            MiscUtil["Miscellaneous Helpers"]:::utilities
        end
    end

    %% External Resources
    subgraph "External Resources"
        FileSys["File System"]:::external
        SSLCerts["SSL Certificates"]:::external
        SMTP["SMTP Server"]:::external
        Identity["Identity Provider"]:::external
    end

    %% Data Flow
    HTTPClient -->|"HTTP Request"| HTTPListener
    HTTPListener -->|"Request Object"| BodyParser
    BodyParser -->|"Parsed Body"| CookieMgr
    CookieMgr -->|"Parsed Cookies"| SessionMgr
    SessionMgr -->|"Session Loaded"| RuleEngine

    RuleEngine -->|"Folder/File Rule"| StaticHandler
    RuleEngine -->|"Action Rule"| ActionHandler
    RuleEngine -->|"WebSocket Rule"| WSHandler

    WSClient -->|"WS Upgrade"| HTTPListener
    WSHandler -->|"WS Messages"| WSHandler

    StaticHandler -->|"Read From Disk"| FileSys
    StaticHandler -->|"Respond Static"| HTTPListener

    ActionHandler -->|"Invoke Callback"| TemplateEngine
    ActionHandler -->|"Invoke Callback"| JwtMgr
    ActionHandler -->|"Invoke Callback"| MailMod
    ActionHandler -->|"Invoke Callback"| Config
    ActionHandler -->|"Invoke Callback"| Debug
    ActionHandler -->|"Invoke Callback"| ConsoleUI
    ActionHandler -->|"Invoke Callback"| EnvUtil
    ActionHandler -->|"Invoke Callback"| PathUtil
    ActionHandler -->|"Invoke Callback"| MiscUtil
    ActionHandler -->|"Send Response"| HTTPListener

    TemplateEngine -->|"Load Templates/CSS"| FileSys
    TemplateEngine -->|"Render HTML"| HTTPListener

    JwtMgr -->|"Verify Token"| Identity
    MailMod -->|"Send Email"| SMTP

    HTTPListener -->|"HTTPS Certs"| SSLCerts

    %% Click Events
    click HTTPListener "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Server.ts"
    click RuleEngine "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Rule.ts"
    click BodyParser "https://github.com/diegofmo0802/servercore/blob/main/src/Server/BodyParser.ts"
    click CookieMgr "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Cookie.ts"
    click SessionMgr "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Session.ts"
    click WSHandler "https://github.com/diegofmo0802/servercore/blob/main/src/Server/WebSocket/WebSocket.ts"
    click StaticHandler "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Rule.ts"
    click HTTPListener "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Server.ts"
    click HTTPListener "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Server.ts"
    click StaticHandler "https://github.com/diegofmo0802/servercore/blob/main/src/Server/Rule.ts"
    click WSHandler "https://github.com/diegofmo0802/servercore/blob/main/src/Server/WebSocket/WebSocket.ts"
    click WSHandler "https://github.com/diegofmo0802/servercore/blob/main/src/Server/WebSocket/Chunk.ts"
    click TemplateEngine "https://github.com/diegofmo0802/servercore/blob/main/src/Template.ts"
    click FileSys "https://github.com/diegofmo0802/servercore/tree/main/global/Template"
    click FileSys "https://github.com/diegofmo0802/servercore/blob/main/global/Style/Template/*.css"
    click JwtMgr "https://github.com/diegofmo0802/servercore/blob/main/src/Beta/JwtManager.ts"
    click MailMod "https://github.com/diegofmo0802/servercore/blob/main/src/Beta/Mail.ts"
    click Config "https://github.com/diegofmo0802/servercore/blob/main/src/Config.ts"
    click Debug "https://github.com/diegofmo0802/servercore/blob/main/src/Debug.ts"
    click ConsoleUI "https://github.com/diegofmo0802/servercore/blob/main/src/ConsoleUI.ts"
    click EnvUtil "https://github.com/diegofmo0802/servercore/blob/main/src/Utilities/Env.ts"
    click PathUtil "https://github.com/diegofmo0802/servercore/blob/main/src/Utilities/Path.ts"
    click MiscUtil "https://github.com/diegofmo0802/servercore/blob/main/src/Utilities/Utilities.ts"

    %% Styles
    classDef core fill:#d0e7ff,stroke:#004d99
    classDef middleware fill:#e0ffe0,stroke:#009900
    classDef routing fill:#ffe0b2,stroke:#e65c00
    classDef handlers fill:#ffd1d1,stroke:#cc0000
    classDef beta fill:#e0d1ff,stroke:#660099
    classDef external fill:#f2f2f2,stroke:#666666
    classDef template fill:#fff5b1,stroke:#b38f00
    classDef utilities fill:#f0f0f0,stroke:#999999
```
