# key-runnnnnnn/compiler architecture

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

- Interactive diagram: https://gitdiagram.com/key-runnnnnnn/compiler
- Repository: https://github.com/key-runnnnnnn/compiler

## Overview

##### Project Type & Purpose
- This is a backend, open-source code-execution microservice built with Node.js and Express.
- Purpose: accept code snippets (single or multiple test cases), compile/interpret them in an isolated environment, measure resource usage, and return detailed results.

##### File Structure Analysis
- Server.js: entry point, sets up Express, middleware, routes, and connects to external services.
- routes/compilerRoutes.js: defines HTTP endpoints (/api/execute, /api/alltestcases, /health).
- controllers/compilerController.js & AllTestCasesController.js: orchestrate request validation, job submission, result aggregation.
- utils/: low-level utilities
- codeExecutionUtils.js – writes code to temp FS, spawns child_process or Docker for execution, monitors with pidusage.
- testValidationUtils.js – iterates multiple testcases.
- responseUtils.js – formats API responses.
- errorTypes.js – custom error definitions.
- temp/: scratch filesystem for writing user code before execution.
- package.json, Dockerfile: containerization, dependencies.
- No frontend; this is purely a backend processing/queueing service.

a. Clients (curl, Postman, UI)
b. API Gateway / Express App
- Routes → Controllers → Service Logic
c. Job Queue
- BullMQ queue backed by Redis
d. Worker Pool
- Node.js worker_threads or separate processes subscribe to the queue
- Each worker uses codeExecutionUtils to:
- write code to temp/<jobId>/Main.*
- sandbox execution via child_process (or Docker)
- monitor via pidusage
e. Temporary Storage (Local FS under temp/)
f. Optional Caching / Persistence
- Redis (for queue and optional result caching)
- MongoDB (via mongoose, if enabled)
g. Response Formatter (responseUtils)

##### Interactions & Data Flow
1. Client → HTTP POST /api/execute or /api/alltestcases
2. Express Router → Controller
3. Controller → validate input (testValidationUtils)
4. Controller → enqueue job to BullMQ (Redis)
5. Worker(s) ← subscribes to BullMQ
6. Worker
- writes code to temp FS
- runs child_process (or spawns Docker container) for language runtime
- collects stdout/stderr, pidusage metrics
- (for multiple testcases) loops through testcases, aggregates results
7. Worker → returns result to Controller (via queue job completion event)
8. Controller → responseUtils formats JSON → Express sends HTTP response

##### Architectural Patterns & Design Principles
- Micro-service style single responsibility: API vs worker vs queue
- Asynchronous, event-driven processing (BullMQ + worker threads)
- Isolated execution sandbox (child_process or Docker)
- Utilities layered under ‘utils/’ for reusable logic
- Fault-tolerant queueing with timeouts & rate limiting

## Diagram (Mermaid)

```mermaid
flowchart TB
    classDef client fill:#fff3cd,stroke:#333,stroke-width:1px;
    classDef api fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef util fill:#e2e3e5,stroke:#333,stroke-width:1px;
    classDef queue fill:#d1ecf1,stroke:#333,stroke-width:1px;
    classDef worker fill:#f8d7da,stroke:#333,stroke-width:1px;
    classDef storage fill:#d4edda,stroke:#333,stroke-width:1px;
    classDef infra fill:#f0e5ff,stroke:#333,stroke-width:1px;

    Client["Client\n(curl/Postman/UI)"]:::client

    subgraph "API Layer"
        Server["Express API Server\n(Express.js)"]:::api
        Routes["HTTP Routes\n(/api/execute, /api/alltestcases, /health)"]:::api
        Controller1["Compile/Execute Controller"]:::api
        Controller2["All Test Cases Controller"]:::api
        Validation["Test Case Validation Utility"]:::util
        Response["API Response Formatter"]:::util
        Errors["Custom Error Definitions"]:::util
    end

    subgraph "Queue Layer"
        Queue["BullMQ Queue\n(Redis)"]:::queue
    end

    subgraph "Worker Layer"
        Worker["Code Execution Worker\n(child_process + pidusage)"]:::worker
        CodeUtils["Code Execution Utils"]:::util
    end

    subgraph "Storage Layer"
        Temp["Temporary Storage FS\n(temp/)"]:::storage
        Redis["Redis Cache\n(optional result caching)"]:::storage
        Mongo["MongoDB\n(optional persistence)"]:::storage
    end

    subgraph "Infrastructure"
        Docker["Docker Container Config"]:::infra
        Package["Node.js Dependencies & Scripts"]:::infra
    end

    Client -->|"POST /api/execute\n/api/alltestcases"| Server
    Server --> Routes
    Routes --> Controller1
    Routes --> Controller2
    Controller1 --> Validation
    Controller2 --> Validation
    Controller1 --> Queue
    Controller2 --> Queue
    Controller1 --> Response
    Controller2 --> Response
    Queue --> Worker
    Worker --> CodeUtils
    CodeUtils --> Temp
    Worker --> Redis
    Worker --> Mongo
    Response --> Server
    Server -->|"HTTP Response"| Client

    click Server "https://github.com/key-runnnnnnn/compiler/blob/master/Server.js"
    click Routes "https://github.com/key-runnnnnnn/compiler/blob/master/routes/compilerRoutes.js"
    click Controller1 "https://github.com/key-runnnnnnn/compiler/blob/master/controllers/compilerController.js"
    click Controller2 "https://github.com/key-runnnnnnn/compiler/blob/master/controllers/AllTestCasesController.js"
    click CodeUtils "https://github.com/key-runnnnnnn/compiler/blob/master/utils/codeExecutionUtils.js"
    click Validation "https://github.com/key-runnnnnnn/compiler/blob/master/utils/testValidationUtils.js"
    click Response "https://github.com/key-runnnnnnn/compiler/blob/master/utils/responseUtils.js"
    click Errors "https://github.com/key-runnnnnnn/compiler/blob/master/utils/errorTypes.js"
    click Temp "https://github.com/key-runnnnnnn/compiler/tree/master/temp/"
    click Docker "https://github.com/key-runnnnnnn/compiler/tree/master/Dockerfile"
    click Package "https://github.com/key-runnnnnnn/compiler/blob/master/package.json"
```
