Skip to content

magicloops/code-runner

Repository files navigation

code-runner

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.


Table of Contents

  1. Prerequisites
  2. Project Layout
  3. Building
    1. Development Build
    2. Production Build
  4. Running Locally
  5. Testing the Service
    1. Sample Input (Python)
    2. Sample Input (Node.js)
  6. Running via Docker
  7. Troubleshooting

Prerequisites

Rust and Cargo

  • Install Rust (which includes Cargo) from rustup.rs.
  • Verify installation:
    rustc --version
    cargo --version

Interpreters (Node.js and Python)

  • Ensure you have Node.js and Python installed and accessible on your PATH.
    node --version
    python3 --version
  • If python is not available but python3 is, you may need to adjust the source to invoke python3 directly (see Testing the Service).

Document Parsing Libraries

Both runtimes ship with PDF and Word parsing packages preinstalled:

These allow user code to extract text from PDF and DOCX files without installing additional dependencies.

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.


Project Layout

.
├─ 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 its npm dependencies.

Building

Development Build

Build in dev mode (faster compilation, unoptimized):

cargo build

The binary is located at target/debug/code-runner.

Production Build

Build in release mode (optimized):

cargo build --release

The binary is located at target/release/code-runner.


Running Locally

  1. Rust Service
    From the repository root:

    cargo run

    This starts the Rust service on 0.0.0.0:4000.

  2. 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.


Testing the Service

Below are sample curl requests to test the /run endpoint exposed by the Rust-based HTTP service.

Sample Input (Python)

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, but python3 is, you can either:

  • Add an alias for python, or
  • Adjust the interpreter command in src/main.rs:
    match language {
        "python" => "python3",
        _ => { /* handle error */ }
    };
    Then rebuild and rerun.

Sample Input (Node.js)

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
}

Running via Docker

  1. Build the Docker image
    From the repository root (where the Dockerfile resides):

    docker build -t code-runner-service:container .

    This multi-stage build installs Rust, builds the code-runner binary, installs any Node.js dependencies in node-runner/, and then copies them into a minimal final image.

  2. 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 to http://localhost:4000/run.


Troubleshooting

Command Execution Error (No such file or directory)

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.

Permission Issues

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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •