Skip to content

Commit 6f0af34

Browse files
committed
made readme easier to read and changed the api routes
1 parent f1d9d59 commit 6f0af34

3 files changed

Lines changed: 37 additions & 64 deletions

File tree

README.md

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,24 @@ To **Host** this API, you need to have Linux or WSL (if you're on windows) or Ma
1111

1212
## Usage
1313

14-
Using this API is designed to be as simple as possible, at it's core, it's just an HTTP request that you send to `/execute`, or `/simple-execute`
14+
Using this API is designed to be as simple as possible, at it's core, it's just an HTTP request that you send to `/v2/execute`, or `/execute`
1515

1616
The following are the routes that this API makes available, click one to see it's documentation
1717

18-
<details>
19-
<summary><h3>POST Route: /simple-execute (click to expand)</h3></summary>
20-
21-
This route is a drop in replacement for [CodeExecutionAPI](https://github.com/thealcodingclub/CodeExecutionAPI), just take the code where you called the old API and replace `https://codeapi.anga.codes/execute` with `${url}/simple-execute` and watch as ur response times get faster!.
18+
---
19+
### POST Route: /execute
20+
---
21+
This route is a drop in replacement for [CodeExecutionAPI](https://github.com/thealcodingclub/CodeExecutionAPI), if you send any request to the API defined in that repository, it's actually going to be handled by this codebase (because it does the same exact thing but faster)
2222

2323
This route takes 4 fields:
2424

25-
1. `langauge`: The language of the code snippet.
26-
<details>
27-
<summary>Click to see supported languages</summary>
28-
29-
- python
30-
- cpp
31-
- c
32-
- java
33-
34-
</details>
35-
36-
2. `code`: The code snippet to be executed.
37-
3. `timeout` (optional field): The maximum number of seconds for which the code should run. If the code takes more time to execute than what it was allocated, it will be terminated.
38-
- default (value assumed if you dont specify): **5 seconds**
39-
- max (you can't allocate more seconds than this): **60 seconds**
40-
4. `max_memory` (optional field): The maximum memory in KB (kilobytes) that the code can use. If the code uses more memory than this, it will be terminated.
41-
- default (value assumed if you dont specify): **32768KB** (or 32MB)
42-
- max (you can't allocate more RAM than this): **131072KB** (or 128MB)
43-
5. `inputs` (optional field): This is an array of strings where every element is 1 line, its completely optional and you should use it if your code reads input from STDIN (like with python input() or scanf() in C), read example 4 to see usage
25+
| Parameter | Type | Description | Default | Limits | Additional Details |
26+
|-----------|------|-------------|---------|--------|-------------------|
27+
| **language** | `string` | The programming language of the code snippet to be executed. | *Required*<br>*(No default)* | *Must be one of the supported languages* | **Supported Languages:**<br>• `python` - Python 3.x<br>• `cpp` - C++ (GCC compiler)<br>• `c` - C (GCC compiler)<br>• `java` - Java (OpenJDK)<br><br>The language must exactly match one of these strings (case-sensitive, all lowercase). |
28+
| **code** | `string` | The actual source code to be executed. Use `\n` to represent line breaks within the string. | *Required*<br>*(No default)* | *No explicit size limit, but practical limits apply* | **Formatting Rules:**<br>• For single-line code: `"print('Hello')"`<br>• For multi-line code: `"line1\nline2\nline3"`<br><br>**Example:**<br>```json\n"code": "def hello():\n print('world')\nhello()"\n```<br><br>All special characters in your code must be properly escaped for JSON. |
29+
| **timeout** | `integer` | Maximum execution time in **seconds**. If the code runs longer than this, it will be forcefully terminated. | `5` seconds | **Minimum:** `1` second<br>**Maximum:** `60` seconds | **Behavior:**<br>• When timeout is exceeded → execution stops immediately<br>• Returns error: `"Execution Timed Out"`<br>• `cpu_time` in response shows actual time before termination<br>• `output` will be empty (no partial output)<br><br>**Use Cases:**<br>• Prevent infinite loops<br>• Control costs for long-running code<br>• Ensure API responsiveness |
30+
| **max_memory** | `integer` | Maximum memory allocation in **KB (kilobytes)**. If the code exceeds this limit, it will be terminated immediately. | `32768` KB<br>(32 MB) | **Minimum:** `1024` KB (1 MB)<br>**Maximum:** `131072` KB (128 MB) | **Memory Conversions:**<br>• 1 MB = 1,024 KB<br>• 32 MB = 32,768 KB (default)<br>• 64 MB = 65,536 KB<br>• 128 MB = 131,072 KB (max)<br><br>**Behavior:**<br>• Monitored in real-time during execution<br>• Exceeding limit → immediate termination<br>• Returns error: `"Memory limit exceeded"`<br>• `memory_used` shows usage at termination<br><br>**Use Cases:**<br>• Prevent memory leaks<br>• Control resource usage for large data structures<br>• Ensure fair resource allocation |
31+
| **inputs** | `array` | An array of strings where **each element represents one line** of input. This is fed to the code via STDIN at runtime. | `[]`<br>(Empty array - no input) | *No explicit size limit, but practical limits apply* | **How It Works:**<br>• Each array element becomes one line of input<br>• Elements are provided in order as the code requests input<br>• Works with any language that reads from STDIN<br><br>**Example 1** (single input):<br>`["bob"]`<br><br>**Example 2** (multiple inputs):<br>`["bob", "alice"]`<br><br>**Example 3** (mixed types):<br>`["John", "25", "3.14"]`<br><br>**Language Examples:**<br>• Python: `input()` reads each element sequentially<br>• C: `scanf()` reads each element<br>• Java: `Scanner.nextLine()` reads each element<br>• C++: `cin >>` or `getline()` reads each element |
4432

4533
*Note: the above mentioned default and max values can be modified by editing the environment variables mentioned at the bottom of this README*
4634

@@ -50,15 +38,15 @@ This route takes 4 fields:
5038

5139
```json
5240
{
53-
"language": "python",
54-
"code": "print('Hello World')"
41+
"language": "python", // this is the language your code is going to be executed in
42+
"code": "print('Hello World')" // a string containing the code itself, with lines separated by \n
5543
}
5644
```
5745
<details>
5846
<summary>Click to copy curl command</summary>
5947

6048
```bash
61-
curl --location '${url}/simple-execute' \
49+
curl --location 'https://codeapi.anga.codes/execute' \
6250
--header 'Content-Type: application/json' \
6351
--data '{
6452
"language": "python",
@@ -95,7 +83,7 @@ curl --location '${url}/simple-execute' \
9583
<summary>Click to copy curl command</summary>
9684

9785
```bash
98-
curl --location '${url}/simple-execute' \
86+
curl --location 'https://codeapi.anga.codes/execute' \
9987
--header 'Content-Type: application/json' \
10088
--data '{
10189
"language": "python",
@@ -133,7 +121,7 @@ curl --location '${url}/simple-execute' \
133121
<summary>Click to copy curl command</summary>
134122

135123
```bash
136-
curl --location '${url}/simple-execute' \
124+
curl --location 'https://codeapi.anga.codes/execute' \
137125
--header 'Content-Type: application/json' \
138126
--data '{
139127
"language": "python",
@@ -174,7 +162,7 @@ curl --location '${url}/simple-execute' \
174162
<summary>Click to copy curl command</summary>
175163

176164
```bash
177-
curl --location '${url}/simple-execute' \
165+
curl --location 'https://codeapi.anga.codes/execute' \
178166
--header 'Content-Type: application/json' \
179167
--data '{
180168
"language": "python",
@@ -202,31 +190,21 @@ curl --location '${url}/simple-execute' \
202190

203191
</details>
204192

205-
**Note**: the route `/simple-execute` also calls `/execute` under the hood, its just that the formatting for the input and output are slightly more easy to read and understand.
206-
<details>
207-
<summary><h3>POST Route: /execute</h3></summary>
208-
209-
This route takes 4 fields:
210-
211-
1. `langauge`: The language of the code snippet.
212-
<details>
213-
<summary>Click to see supported languages</summary>
193+
**Note**: the route `/execute` also calls `/v2/execute` under the hood, its just that the formatting for the input and output are slightly more easy to read and understand.
214194

215-
- python
216-
- cpp
217-
- c
218-
- java
195+
---
196+
### POST Route: /v2/execute</h3></summary>
197+
---
219198

220-
</details>
199+
This route takes 4 fields:
221200

222-
2. `code`: The code snippet to be executed.
223-
3. `timeout` (optional field): The maximum number of **nanoseconds** for which the code should run. If the code takes more time to execute than what it was allocated, it will be terminated.
224-
- default (value assumed if you dont specify): **5 seconds**
225-
- max (you can't allocate more seconds than this): **60 seconds**
226-
4. `max_memory` (optional field): The maximum memory in KB (kilobytes) that the code can use. If the code uses more memory than this, it will be terminated.
227-
- default (value assumed if you dont specify): **32768KB** (or 32MB)
228-
- max (you can't allocate more RAM than this): **131072KB** (or 128MB)
229-
5. `inputs` (optional field): This is a single string that is fed to the code via STDIN at runtime, if you want multiple lines in STDIN then separate them with `\n`, its completely optional and you should use it if your code reads input from STDIN (like with python input() or scanf() in C), read example 4 to see usage
201+
| Parameter | Description | Default | Limits | Notes |
202+
|-----------|-------------|---------|--------|-------|
203+
| **language** | The programming language of the code snippet to be executed. | *Required* | *No default* | **Supported languages:**<br>• `python` - Python 3.x<br>• `cpp` - C++ (GCC)<br>• `c` - C (GCC)<br>• `java` - Java (OpenJDK) |
204+
| **code** | The actual source code to be executed. Use `\n` to represent line breaks within the string. | *Required* | *No explicit limit* | The code is executed in an isolated environment. For multi-line code, escape newlines as `\n` within the JSON string. |
205+
| **timeout** | Maximum execution time in **nanoseconds**. If the code runs longer than this, it will be forcefully terminated. | 5 seconds (5,000,000,000 ns) | **Maximum:** 60 seconds (60,000,000,000 ns) | ⚠️ **Important:** The value is specified in nanoseconds, not seconds. For example:<br>• 2 seconds = `2000000000`<br>• 30 seconds = `30000000000`<br>• 60 seconds = `60000000000` (max) |
206+
| **max_memory** | Maximum memory allocation in **KB (kilobytes)**. If the code exceeds this limit, it will be terminated immediately. | 32,768 KB (32 MB) | **Maximum:** 131,072 KB (128 MB) | Memory is monitored in real-time. When the limit is exceeded, execution stops and returns "Memory limit exceeded" error.<br><br>**Conversions:**<br>• 32 MB = 32,768 KB (default)<br>• 64 MB = 65,536 KB<br>• 128 MB = 131,072 KB (max) |
207+
| **inputs** | A single string containing all STDIN data. If your code reads input (e.g., `input()` in Python, `scanf()` in C, `Scanner` in Java), provide the input here. | *Empty (no input)* | *No explicit limit* | **For multiple lines:** Use `\n` to separate lines. The entire string is fed to STDIN exactly as provided.<br><br>**Example 1** (single input):<br>`"bob"`<br><br>**Example 2** (multiple inputs):<br>`"bob\nalice"`<br><br>**Example 3** (numeric input):<br>`"42\n100\n200"` |
230208

231209
*Note: the above mentioned default and max values can be modified by editing the environment variables mentioned at the bottom of this README*
232210

@@ -244,7 +222,7 @@ This route takes 4 fields:
244222
<summary>Click to copy curl command</summary>
245223

246224
```bash
247-
curl --location '${url}/execute' \
225+
curl --location 'https://codeapi.anga.codes/v2/execute' \
248226
--header 'Content-Type: application/json' \
249227
--data '{
250228
"language": "python",
@@ -281,7 +259,7 @@ curl --location '${url}/execute' \
281259
<summary>Click to copy curl command</summary>
282260

283261
```bash
284-
curl --location '${url}/execute' \
262+
curl --location 'https://codeapi.anga.codes/v2/execute' \
285263
--header 'Content-Type: application/json' \
286264
--data '{
287265
"language": "python",
@@ -319,7 +297,7 @@ curl --location '${url}/execute' \
319297
<summary>Click to copy curl command</summary>
320298

321299
```bash
322-
curl --location '${url}/execute' \
300+
curl --location 'https://codeapi.anga.codes/v2/execute' \
323301
--header 'Content-Type: application/json' \
324302
--data '{
325303
"language": "python",
@@ -357,7 +335,7 @@ curl --location '${url}/execute' \
357335
<summary>Click to copy curl command</summary>
358336

359337
```bash
360-
curl --location '${url}/execute' \
338+
curl --location 'https://codeapi.anga.codes/v2/execute' \
361339
--header 'Content-Type: application/json' \
362340
--data '{
363341
"language": "python",
@@ -379,8 +357,6 @@ curl --location '${url}/execute' \
379357
}
380358
```
381359

382-
</details>
383-
384360
## Environment Variables
385361

386362
- `MAX_TIMEOUT`: This is the maximum timeout that a request can set, if an incoming request has a higher timeout than the value you set here, then it will ignore the request's set timeout only be executed for `MAX_TIMEOUT` time.

config/globals.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import "github.com/caarlos0/env/v11"
44

55
type globals struct {
66
RAM_LIMIT uint `env:"GLOBAL_RAM_LIMIT" envDefault:"1048576"` // 1 GB by default (or 1048576 KB), this is the total amount of RAM that can be used by all running sandboxes at any given time
7-
ENABLE_QUEUE bool `env:"ENABLE_QUEUE" envDefault:"false"` // if false, the server will reject requests when ram limit is reached, if true, the server will queue requests until sufficient ram is available
7+
ENABLE_QUEUE bool `env:"ENABLE_QUEUE" envDefault:"true"` // if false, the server will reject requests when ram limit is reached, if true, the server will queue requests until sufficient ram is available
88
ENABLE_DEBUG_ROUTES bool `env:"ENABLE_DEBUG" envDefault:"false"` // if true, the server will expose debug routes for monitoring and debugging purposes
99
}
1010

11-
// i know the docs say that queueing is true by default
12-
// ill set it to false by default for now because queueing is not implemented yet
13-
1411
func (cfg *globals) load() error {
1512
return env.Parse(cfg)
1613
}

routes/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import (
66

77
func Setup(router *gin.Engine) {
88
router.GET("/", root)
9-
router.POST("/execute", CallDispatcher)
10-
router.POST("/simple-execute", SimpleDispatcher)
9+
router.POST("/v2/execute", CallDispatcher)
10+
router.POST("/execute", SimpleDispatcher)
1111
}

0 commit comments

Comments
 (0)