A simple Rust-based HTTP service that accepts POST requests at the /run
endpoint, executes a provided script with a specified language interpreter (e.g., Python or Node.js), and returns the stdout
and stderr
as JSON.
Additionally, this repository contains a Node.js script (node-runner.js
) that can be run alongside the Rust service for handling certain JavaScript execution scenarios more efficiently.
- Prerequisites
- Project Layout
- Building
- Running Locally
- Testing the Service
- Running via Docker
- Troubleshooting
- Install Rust (which includes Cargo) from rustup.rs.
- Verify installation:
rustc --version cargo --version
- Ensure you have Node.js and Python installed and accessible on your
PATH
.node --version python3 --version
- If
python
is not available butpython3
is, you may need to adjust the source to invokepython3
directly (see Testing the Service).
Both runtimes ship with PDF and Word parsing packages preinstalled:
- Python:
PyPDF2
andpython-docx
- Node.js:
pdf-parse
andmammoth
These allow user code to extract text from PDF and DOCX files without installing additional dependencies.
This project uses:
- Axum for the HTTP server
- Tokio for asynchronous runtime
- Serde and serde_json for JSON parsing
- Tempfile for creating temporary directories
Cargo handles all Rust dependencies automatically; no extra manual steps are required once you have Rust and Cargo installed.
.
├─ Dockerfile # Docker build steps
├─ src/
│ └─ main.rs # Rust-based HTTP server
├─ node-runner/
│ ├─ node-runner.js # Node.js script for handling JS requests
│ ├─ package.json
│ └─ package-lock.json
├─ Cargo.toml
└─ README.md
src/
: Contains the Rust service source code (main.rs
).node-runner/
: Contains the Node.js runner script (node-runner.js
) and itsnpm
dependencies.
Build in dev mode (faster compilation, unoptimized):
cargo build
The binary is located at target/debug/code-runner
.
Build in release mode (optimized):
cargo build --release
The binary is located at target/release/code-runner
.
-
Rust Service
From the repository root:cargo run
This starts the Rust service on
0.0.0.0:4000
. -
Node Runner (Optional)
If you need to run the separate Node.js script (for debugging or local testing), do the following in a separate terminal:cd node-runner npm install node node-runner.js
By default, this listens on a different port (5000) and simply run JavaScript tasks.
Below are sample curl
requests to test the /run
endpoint exposed by the Rust-based HTTP service.
curl -X POST http://localhost:4000/run \
-H 'Content-Type: application/json' \
-d '{
"image": "python:latest",
"payload": {
"language": "python",
"files": [
{
"name": "script.py",
"content": "print(\"Hello World from Python\")"
}
]
}
}'
If everything is configured correctly (and your system has python
), you should see JSON similar to:
{
"stdout": "Hello World from Python\n",
"stderr": "",
"exit_code": 0
}
Note: If
python
isn’t on your PATH, butpython3
is, you can either:
- Add an alias for
python
, or- Adjust the interpreter command in
src/main.rs
:Then rebuild and rerun.match language { "python" => "python3", _ => { /* handle error */ } };
curl -X POST http://localhost:4000/run \
-H 'Content-Type: application/json' \
-d '{
"image": "node:latest",
"payload": {
"language": "node",
"files": [
{
"name": "script.js",
"content": "console.log(\"Hello World from Node\");"
}
]
}
}'
Expected output:
{
"stdout": "Hello World from Node\n",
"stderr": "",
"exit_code": 0
}
-
Build the Docker image
From the repository root (where theDockerfile
resides):docker build -t code-runner-service:container .
This multi-stage build installs Rust, builds the
code-runner
binary, installs any Node.js dependencies innode-runner/
, and then copies them into a minimal final image. -
Run the container
docker run -p 4000:4000 code-runner-service:container
This starts the Rust HTTP service on port 4000 and also spawns
node-runner.js
in the background (depending on your Dockerfile setup). If everything is correct, you can send requests tohttp://localhost:4000/run
.
If you see an error like:
No such file or directory
This usually indicates the specified interpreter (e.g. python
or node
) is not on the PATH
. Make sure you have the interpreter installed and accessible. In a Docker environment, confirm the Dockerfile includes the relevant installations.
Ensure you have permissions to run executables and write to temporary directories. On Unix-like systems, ensure the binary (code-runner
) has the executable bit set, and that Docker has permissions to copy and run it.
Happy hacking!