Monaco Code Execution Backend
A modern, secure, and efficient code execution backend inspired by online code editors like Programiz. This backend is written in Go and uses Docker containers for secure code execution.
Features
- Multi-language Support: Execute code in Python, Java, C, C++, JavaScript, and Go
- Real-time Output: Stream code execution output via WebSockets
- Interactive Input: Send input to running programs via WebSockets
- Secure Execution: All code runs in isolated Docker containers
- Resource Limits: Memory, CPU, and execution time limits
- Scalable Architecture: Concurrent execution with configurable worker pools
Requirements
- Go 1.19+
- Docker
- Git (for development)
Getting Started
Running Locally
-
Clone the repository:
git clone https://github.com/your-username/monaco.git cd monaco/new-backend -
Install dependencies:
go mod download -
Build and run:
go run main.go
The server will start on http://localhost:8080 by default.
Using Docker
Build and run using Docker:
docker build -t monaco-backend .
docker run -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock monaco-backend
Note: Mounting the Docker socket is necessary for container-in-container execution.
API Endpoints
POST /api/submit: Submit code for executionGET /api/status/{id}: Get execution statusGET /api/result/{id}: Get complete execution resultGET /api/languages: List supported languagesGET /api/health: Health check endpointWS /api/ws/terminal/{id}: WebSocket for real-time output
WebSocket Communication
The /api/ws/terminal/{id} endpoint supports these message types:
output: Code execution outputinput: User input to the programinput_prompt: Input prompt detectedstatus: Execution status updateserror: Error messages
Configuration
Configuration is handled through environment variables:
PORT: Server port (default: 8080)CONCURRENT_EXECUTIONS: Number of concurrent executions (default: 5)QUEUE_CAPACITY: Execution queue capacity (default: 100)DEFAULT_TIMEOUT: Default execution timeout in seconds (default: 30)
See config/config.go for more configuration options.
Security Considerations
- All code execution happens in isolated Docker containers
- Network access is disabled in execution containers
- Memory and CPU limits are enforced
- Process limits prevent fork bombs
- Execution timeouts prevent infinite loops
License
MIT