This project provides an API to run code in various languages (JavaScript, Go, Java, and Python) based on provided unit tests. It leverages Docker to create a secure and isolated environment for executing the code.
- Getting Started
- Prerequisites
- Building the Docker Image
- Running the Docker Container
- API Endpoints
- Example Requests
These instructions will help you set up the project on your local machine.
- Docker
- Go (if you plan to run the application outside of Docker)
To build the Docker image, run the following command:
docker build -t code-runner .
Run the Docker container:
docker run -p 8080:8080 code-runner
This will start the server on port 8080.
Executes the provided code and runs it against the provided test cases.
Request Body
{
"language": "javascript",
"code": "console.log('Hello, World!');",
"testCases": [
{
"input": "",
"expected": "Hello, World!"
},
{
"input": "Another test case",
"expected": "Another test case"
}
]
}
- language: The programming language of the code (supported: javascript, golang, java, python)
- code: The code to execute
- testCases: An array of test cases, each containing:
- input: The input to pass to the code
- expected: The expected output
Response Body
[
{
"passed": true,
"output": "Hello, World!",
"error": ""
},
{
"passed": true,
"output": "Another test case",
"error": ""
}
]
- passed: Indicates whether the test case passed
- output: The actual output of the code
- error: Any error encountered during execution
curl -X POST http://localhost:8080/run \
-H "Content-Type: application/json" \
-d '{
"language": "javascript",
"code": "console.log(\"Hello, World!\");",
"testCases": [
{
"input": "",
"expected": "Hello, World!"
}
]
}'
curl -X POST http://localhost:8080/run \
-H "Content-Type: application/json" \
-d '{
"language": "golang",
"code": "package main\nimport \"fmt\"\nfunc main() { var input string; fmt.Scanln(&input); fmt.Println(input) }",
"testCases": [
{
"input": "Hello, World!",
"expected": "Hello, World!"
}
]
}'
curl -X POST http://localhost:8080/run \
-H "Content-Type: application/json" \
-d '{
"language": "java",
"code": "import java.util.Scanner;\n\npublic class Main {\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n String input = scanner.nextLine();\n System.out.println(input);\n }\n}",
"testCases": [
{
"input": "Hello, World!",
"expected": "Hello, World!"
}
]
}'
curl -X POST http://localhost:8080/run \
-H "Content-Type: application/json" \
-d '{
"language": "python",
"code": "print(input())",
"testCases": [
{
"input": "Hello, World!",
"expected": "Hello, World!"
}
]
}'
This project is licensed under the MIT License.