How hiteshchoudhary/codebox works
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: executeCode.js Execute submission job · server.js Express server · ExecutorFactory.js Executor factory · DockerExecutor.js Docker executor
Components 21
HTTP API
Untrusted request boundary and public endpoints.
- API entrypoint
src/index.js - Express server
src/api/server.js - Submission routes · Creates, polls, and batches submissions; wait=true observes the same lifecycle.
src/api/routes/submissions.js - API protection · Authentication, validation, rate limiting, and centralized error handling.
src/api/middleware/auth.js - System and language routes
src/api/routes/system.js
Async Lifecycle
Redis-backed submission processing.
- Job producer
src/queue/producer.js - Redis · Shared queue and submission coordination dependency.
- Execution worker · Runs independently from HTTP request handling.
src/queue/worker.js - Execute submission job
src/queue/jobs/executeCode.js
Execution Boundary
Isolated compilation and runtime selection.
- Code analyzer
src/security/codeAnalyzer.js - Executor factory · Chooses Docker, Firecracker, or an available fallback.
src/executor/ExecutorFactory.js - Docker executor · Runs code with disabled networking and resource limits.
src/executor/DockerExecutor.js - Firecracker executor · Provides microVM isolation when available.
src/executor/FirecrackerExecutor.js - Container pool
src/executor/ContainerPool.js - Result parser · Converts compiler and runtime output into Judge0-style results.
src/executor/ResultParser.js
Language Runtimes
Language contracts and sandbox images.
- Language catalog · Maps IDs to compile/run commands and sandbox images.
src/languages/index.js - Runtime images · Language-specific Docker environments for submitted programs.
docker/images/python/Dockerfile
Operations
Deployment, metrics, and ingress.
- Runtime policy · Supplies engine availability and execution resource constraints.
src/utils/config.js - Prometheus metrics · API metrics exposed for scraping.
src/api/metrics.js - Production topology · Caddy fronts the production service deployment with HTTPS.
docker-compose.prod.yml - Caddy ingress
Caddyfile
Connections 27
- 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