The Code-Execution-Engine is a Docker-based API for securely running code snippets in various languages. Clone the repo, build and run the Docker containers, and use tools like Postman to test code. It supports multiple languages and provides detailed execution results. Leave a star if you like it! π
If you need a simple frontend you can check https://github.com/NaheedRayan/online-code-editor
User
|
| Sends POST request with JSON content
|------------------------------------> localhost:9090/submit
|
| Receives response with a unique identifier and URL for status check
|<-----------------------------------
|
v
+-------------------+
| API |
| (Node.js + Express)
| |
| 1. Generates unique ID (e.g., Test1873426786c5f959d288)
| 2. Sends task to RabbitMQ
| 3. Generates URL for status check
+-------------------+
|
|
v
+---------------------+
| RabbitMQ Task Queue |
+---------------------+
|
|
v
+---------------------+
| Docker Containers |
| (Task processing) |
+---------------------+
|
|
v
+---------------------+
| Redis Cache Server |
| (Stores output) |
+---------------------+
|
User |
| |
| Sends GET request to check status |
|------------------------------------> localhost:9090/results/Test1873426786c5f959d288
|
| API checks Redis for task status
|<-----------------------------------
|
v
Receives final output in JSON format
Ensure you have Docker and Docker Compose installed on your system.
git clone <repository-url>
cd <repository-folder>
sudo docker-compose build
sudo docker-compose up
Ping the server to ensure it's running:
curl http://localhost:8080/
You should receive a response: Hello from Titan-Engine
with status code 200
.
Open your browser and navigate to: http://localhost:8080/
You should see the same response: Hello from Titan-Engine
.
Send a POST request to http://localhost:8080/submit
with the following JSON body:
{
"src": "\n\n#include<bits/stdc++.h>\n\nusing namespace std ;\n\nint main()\n{\n int a ;\n cin >> a ;\n\n cout << \"The first number is \" << a << endl ;\n \n int b ;\n cin >> b ;\n \n cout << \"The second number is \" << b << endl ;\n\n cout << \"Hello from cpp\" <<endl ;\n\n // while(1)\n // {\n // cout << 1 << endl ;\n // }\n return 0;\n}\n\n",
"stdin": "48\n95",
"lang": "cpp",
"timeout": "5"
}
You will receive a response like:
http://localhost:8080/results/Test646d62525e1b09171058
You can then use this URL in Postman or your browser to view the execution result.
To view the logs for the Docker containers, you can use the following commands:
View the Last 50 Lines of Logs with Timestamps
docker-compose logs -ft --tail 50
View the Last 50 Lines of Logs without Timestamps
docker-compose logs -f --tail 50
Clearing Docker Resources To clear unused Docker resources (dangling images, stopped containers, etc.) and free up space, run:
docker system prune
This command will remove all unused data, so use it with caution.
src
: Contains the script in raw format.stdin
: Contains the inputs in raw format.lang
: The programming language of the script.(c
,cpp
,java
,python3
,openJDK-8
)timeout
: The maximum time (in seconds) the script is allowed to run. (Max 5 sec)
The API will respond with a URL like:
http://localhost:8080/results/Test646d62525e1b09171058
Navigate to this URL to view the result.
output
: The output of the script.status
: Status of the execution (e.g., Successful, Failed).stderr
: Any errors encountered during execution.submission_id
: A unique ID for the submission.
Here are the different status codes you may encounter:
Invalid Request
: The request was not properly formatted or required fields were missing.Queued
: The request is waiting to be processed.Processing
: The script is currently being executed.Runtime Error
: An error occurred during execution on the worker nodes.Failed
: The script failed due to a compilation or runtime error.Successful
: The script executed successfully.
The Docker Compose setup exposes two ports:
- Port 8080: Used for debugging (HTTP).
- Port 9090: Uses a self-signed SSL certificate (HTTPS).
Congratulations on setting up and running the Code-Execution-Engine! π