# hiteshchoudhary/codebox architecture

> Architecture diagram of the GitHub repository hiteshchoudhary/codebox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hiteshchoudhary/codebox
- Repository: https://github.com/hiteshchoudhary/codebox

## Overview

#### Purpose and Process Entry Points
CodeBox is a self-hosted, Judge0-compatible API for securely compiling/running submitted code. The API process starts at `src/index.js` and serves HTTP through `src/api/server.js`; execution workers run independently through `src/queue/worker.js`. Local and production service topology is defined in `docker-compose.yml` and `docker-compose.prod.yml`.

#### HTTP API Boundary
Express routes expose submission creation, polling, batches, language/system information, and observability. Submission requests enter through `src/api/routes/submissions.js`; supporting endpoints are in `src/api/routes/system.js` and `src/api/routes/languages.js`. Authentication, request validation, rate limiting, and centralized failures form the untrusted-client boundary in `src/api/middleware/auth.js`, `src/api/middleware/validation.js`, and `src/api/middleware/errorHandler.js`.

#### Queue-Based Submission Lifecycle
The API turns accepted submissions into Redis-backed jobs via `src/queue/producer.js`. A separate worker consumes them in `src/queue/worker.js` and executes the job implementation in `src/queue/jobs/executeCode.js`. This separates request latency from potentially slow compilation/execution; synchronous `wait=true` is an API-level wait over the same submission lifecycle.

#### Execution Abstraction and Engine Choice
`src/executor/ExecutorFactory.js` selects Docker, Firecracker, or automatic fallback based on runtime availability/configuration. `src/executor/DockerExecutor.js` provides container sandbox execution, while `src/executor/FirecrackerExecutor.js` targets microVM isolation. `src/executor/ContainerPool.js` is the reuse/performance layer for container-backed execution. `src/executor/ResultParser.js` normalizes compiler/runtime output into Judge0-style submission results.

#### Language Contract and Runtime Images
Language IDs, compile commands, run commands, and image mappings are centralized under `src/languages/index.js`, with individual contracts such as `src/languages/python.js` and `src/languages/java.js`. Sandboxed runtime environments are built from language-specific Dockerfiles, including `docker/images/python/Dockerfile`, `docker/images/gcc/Dockerfile`, and `docker/images/java/Dockerfile`. Adding a language requires both its execution definition and an available isolated image.

#### Security and Resource Boundary
User code is analyzed before execution by `src/security/codeAnalyzer.js`, then run in isolated engines with disabled networking and configured CPU, memory, process, and time constraints. The API-side protection boundary is distinct from the execution boundary: API middleware protects service access, while Docker/Firecracker protects the host from submitted programs. Runtime policy is driven by environment-backed settings in `src/utils/config.js`.

#### Operations and Observability
Redis is the durable coordination dependency between API and workers. Prometheus metrics originate in `src/api/metrics.js` and are exposed by `src/api/routes/metrics.js`, with scrape configuration in `prometheus.yml`. Production places Caddy in front of services for HTTPS via `Caddyfile`; `docker-compose.prod.yml` defines that deployment composition. Build/setup scripts provision runtime images and Firecracker rootfs support, notably `scripts/build-images.sh` and `scripts/build-firecracker-rootfs.sh`.

Main entry points: [`src/queue/jobs/executeCode.js`](https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/jobs/executeCode.js) (Execute submission job), [`src/api/server.js`](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/server.js) (Express server), [`src/executor/ExecutorFactory.js`](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ExecutorFactory.js) (Executor factory), [`src/executor/DockerExecutor.js`](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/DockerExecutor.js) (Docker executor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_api_entry["API entrypoint<br/>Node.js process<br/>[index.js]"]
  node_api_server["Express server<br/>HTTP server<br/>[server.js]"]
  node_submission_routes["Submission routes<br/>Express routes<br/>[submissions.js]"]
  node_api_middleware["API protection<br/>middleware<br/>[auth.js]"]
  node_info_routes["System and language routes<br/>Express routes<br/>[system.js]"]
end

subgraph group_queue["Async Lifecycle"]
  node_queue_producer["Job producer<br/>queue producer<br/>[producer.js]"]
  node_redis[("Redis<br/>durable coordination")]
  node_queue_worker["Execution worker<br/>worker process<br/>[worker.js]"]
  node_execute_job["Execute submission job<br/>queue job<br/>[executeCode.js]"]
end

subgraph group_execution["Execution Boundary"]
  node_code_analyzer["Code analyzer<br/>pre-execution security<br/>[codeAnalyzer.js]"]
  node_executor_factory{{"Executor factory<br/>engine selector<br/>[ExecutorFactory.js]"}}
  node_docker_executor["Docker executor<br/>container sandbox<br/>[DockerExecutor.js]"]
  node_firecracker_executor["Firecracker executor<br/>microVM sandbox"]
  node_container_pool["Container pool<br/>reuse layer<br/>[ContainerPool.js]"]
  node_result_parser["Result parser<br/>result normalization<br/>[ResultParser.js]"]
end

subgraph group_runtime["Language Runtimes"]
  node_language_catalog["Language catalog<br/>language contracts<br/>[index.js]"]
  node_runtime_images["Runtime images<br/>sandbox images"]
end

subgraph group_operations["Operations"]
  node_config["Runtime policy<br/>environment config<br/>[config.js]"]
  node_metrics["Prometheus metrics<br/>observability<br/>[metrics.js]"]
  node_production_topology["Production topology<br/>deployment composition"]
  node_caddy["Caddy ingress<br/>HTTPS proxy"]
end

node_api_entry -->|"starts"| node_api_server
node_api_server -->|"protects requests with"| node_api_middleware
node_api_middleware -->|"admits"| node_submission_routes
node_api_server -->|"serves"| node_info_routes
node_submission_routes -->|"enqueues accepted submissions"| node_queue_producer
node_queue_producer -->|"writes jobs"| node_redis
node_queue_worker -->|"consumes jobs"| node_redis
node_queue_worker -->|"runs"| node_execute_job
node_execute_job -->|"analyzes before execution"| node_code_analyzer
node_execute_job -->|"selects engine through"| node_executor_factory
node_executor_factory -->|"selects Docker"| node_docker_executor
node_executor_factory -->|"selects Firecracker"| node_firecracker_executor
node_docker_executor -->|"reuses containers via"| node_container_pool
node_docker_executor -->|"runs in"| node_runtime_images
node_firecracker_executor -.->|"uses runtime environment"| node_runtime_images
node_execute_job -->|"normalizes output with"| node_result_parser
node_language_catalog -->|"maps languages to"| node_runtime_images
node_info_routes -->|"reports"| node_language_catalog
node_execute_job -->|"loads compile/run contract"| node_language_catalog
node_config -.->|"configures availability"| node_executor_factory
node_config -.->|"sets resource policy"| node_docker_executor
node_config -.->|"sets resource policy"| node_firecracker_executor
node_api_server -->|"emits"| node_metrics
node_production_topology -.->|"deploys"| node_api_entry
node_production_topology -.->|"deploys"| node_queue_worker
node_production_topology -->|"includes"| node_caddy
node_caddy -->|"proxies HTTPS traffic"| node_api_server

click node_api_entry "https://github.com/hiteshchoudhary/codebox/blob/main/src/index.js"
click node_api_server "https://github.com/hiteshchoudhary/codebox/blob/main/src/api/server.js"
click node_submission_routes "https://github.com/hiteshchoudhary/codebox/blob/main/src/api/routes/submissions.js"
click node_api_middleware "https://github.com/hiteshchoudhary/codebox/blob/main/src/api/middleware/auth.js"
click node_info_routes "https://github.com/hiteshchoudhary/codebox/blob/main/src/api/routes/system.js"
click node_queue_producer "https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/producer.js"
click node_queue_worker "https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/worker.js"
click node_execute_job "https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/jobs/executeCode.js"
click node_code_analyzer "https://github.com/hiteshchoudhary/codebox/blob/main/src/security/codeAnalyzer.js"
click node_executor_factory "https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ExecutorFactory.js"
click node_docker_executor "https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/DockerExecutor.js"
click node_firecracker_executor "https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/FirecrackerExecutor.js"
click node_container_pool "https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ContainerPool.js"
click node_result_parser "https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ResultParser.js"
click node_language_catalog "https://github.com/hiteshchoudhary/codebox/blob/main/src/languages/index.js"
click node_runtime_images "https://github.com/hiteshchoudhary/codebox/blob/main/docker/images/python/Dockerfile"
click node_config "https://github.com/hiteshchoudhary/codebox/blob/main/src/utils/config.js"
click node_metrics "https://github.com/hiteshchoudhary/codebox/blob/main/src/api/metrics.js"
click node_production_topology "https://github.com/hiteshchoudhary/codebox/blob/main/docker-compose.prod.yml"
click node_caddy "https://github.com/hiteshchoudhary/codebox/blob/main/Caddyfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_entry,node_api_server,node_submission_routes,node_api_middleware,node_info_routes toneBlue
class node_queue_producer,node_redis,node_queue_worker,node_execute_job toneAmber
class node_code_analyzer,node_executor_factory,node_docker_executor,node_firecracker_executor,node_container_pool,node_result_parser toneMint
class node_language_catalog,node_runtime_images toneRose
class node_config,node_metrics,node_production_topology,node_caddy toneIndigo
```

## Components

### HTTP API

Untrusted request boundary and public endpoints.

- [API entrypoint](https://github.com/hiteshchoudhary/codebox/blob/main/src/index.js): Node.js process · `src/index.js`
- [Express server](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/server.js): HTTP server · `src/api/server.js`
- [Submission routes](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/routes/submissions.js): Express routes · Creates, polls, and batches submissions; wait=true observes the same lifecycle. · `src/api/routes/submissions.js`
- [API protection](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/middleware/auth.js): middleware · Authentication, validation, rate limiting, and centralized error handling. · `src/api/middleware/auth.js`
- [System and language routes](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/routes/system.js): Express routes · `src/api/routes/system.js`

### Async Lifecycle

Redis-backed submission processing.

- [Job producer](https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/producer.js): queue producer · `src/queue/producer.js`
- **Redis**: durable coordination · Shared queue and submission coordination dependency.
- [Execution worker](https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/worker.js): worker process · Runs independently from HTTP request handling. · `src/queue/worker.js`
- [Execute submission job](https://github.com/hiteshchoudhary/codebox/blob/main/src/queue/jobs/executeCode.js): queue job · `src/queue/jobs/executeCode.js`

### Execution Boundary

Isolated compilation and runtime selection.

- [Code analyzer](https://github.com/hiteshchoudhary/codebox/blob/main/src/security/codeAnalyzer.js): pre-execution security · `src/security/codeAnalyzer.js`
- [Executor factory](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ExecutorFactory.js): engine selector · Chooses Docker, Firecracker, or an available fallback. · `src/executor/ExecutorFactory.js`
- [Docker executor](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/DockerExecutor.js): container sandbox · Runs code with disabled networking and resource limits. · `src/executor/DockerExecutor.js`
- [Firecracker executor](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/FirecrackerExecutor.js): microVM sandbox · Provides microVM isolation when available. · `src/executor/FirecrackerExecutor.js`
- [Container pool](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ContainerPool.js): reuse layer · `src/executor/ContainerPool.js`
- [Result parser](https://github.com/hiteshchoudhary/codebox/blob/main/src/executor/ResultParser.js): result normalization · Converts compiler and runtime output into Judge0-style results. · `src/executor/ResultParser.js`

### Language Runtimes

Language contracts and sandbox images.

- [Language catalog](https://github.com/hiteshchoudhary/codebox/blob/main/src/languages/index.js): language contracts · Maps IDs to compile/run commands and sandbox images. · `src/languages/index.js`
- [Runtime images](https://github.com/hiteshchoudhary/codebox/blob/main/docker/images/python/Dockerfile): sandbox images · Language-specific Docker environments for submitted programs. · `docker/images/python/Dockerfile`

### Operations

Deployment, metrics, and ingress.

- [Runtime policy](https://github.com/hiteshchoudhary/codebox/blob/main/src/utils/config.js): environment config · Supplies engine availability and execution resource constraints. · `src/utils/config.js`
- [Prometheus metrics](https://github.com/hiteshchoudhary/codebox/blob/main/src/api/metrics.js): observability · API metrics exposed for scraping. · `src/api/metrics.js`
- [Production topology](https://github.com/hiteshchoudhary/codebox/blob/main/docker-compose.prod.yml): deployment composition · Caddy fronts the production service deployment with HTTPS. · `docker-compose.prod.yml`
- [Caddy ingress](https://github.com/hiteshchoudhary/codebox/blob/main/Caddyfile): HTTPS proxy · `Caddyfile`

## Connections

- API entrypoint → Express server: starts
- Express server → API protection: protects requests with
- API protection → Submission routes: admits
- Express server → System and language routes: serves
- Submission routes → Job producer: enqueues accepted submissions
- Job producer → Redis: writes jobs
- Execution worker → Redis: consumes jobs
- Execution worker → Execute submission job: runs
- Execute submission job → Code analyzer: analyzes before execution
- Execute submission job → Executor factory: selects engine through
- Executor factory → Docker executor: selects Docker
- Executor factory → Firecracker executor: selects Firecracker
- Docker executor → Container pool: reuses containers via
- Docker executor → Runtime images: runs in
- Firecracker executor → Runtime images: uses runtime environment
- Execute submission job → Result parser: normalizes output with
- Language catalog → Runtime images: maps languages to
- System and language routes → Language catalog: reports
- Execute submission job → Language catalog: loads compile/run contract
- Runtime policy → Executor factory: configures availability
- Runtime policy → Docker executor: sets resource policy
- Runtime policy → Firecracker executor: sets resource policy
- Express server → Prometheus metrics: emits
- Production topology → API entrypoint: deploys
- Production topology → Execution worker: deploys
- Production topology → Caddy ingress: includes
- Caddy ingress → Express server: proxies HTTPS traffic
