https://github.com/nirdosh17/go-sandbox
gRPC API sandbox to execute arbitrary Go code.
https://github.com/nirdosh17/go-sandbox
container go golang grpc grpc-go isolate sandbox sandbox-playground
Last synced: 7 months ago
JSON representation
gRPC API sandbox to execute arbitrary Go code.
- Host: GitHub
- URL: https://github.com/nirdosh17/go-sandbox
- Owner: nirdosh17
- License: mit
- Created: 2024-04-06T11:41:13.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-27T10:07:12.000Z (about 2 years ago)
- Last Synced: 2024-12-29T01:58:02.606Z (over 1 year ago)
- Topics: container, go, golang, grpc, grpc-go, isolate, sandbox, sandbox-playground
- Language: Go
- Homepage: https://nirdoshgautam.dev/sandbox-grpc-api-golang
- Size: 26.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Sandbox Service
A containerized service that exposes a gRPC server streaming endpoint for running an arbitrary Go code in a sandbox.
The arbitrary code runs inside multiple sandboxes using [isolate](https://github.com/ioi/isolate).

**Sandbox:**
- Multiple sandboxes are created to handle concurrent requests. One sandbox serves one request at a time and keeps other requests waiting till the sandbox is available again.
- Network calls / File creation(size) are restricted.
- Files created inside a specific sandbox are not visible to any other sandboxes.
- Sandboxes are cleaned up periodically.
#### See the full implementation in action: [https://goplayground.dev](https://goplayground.dev)
## Running locally
1. **Build image**
```bash
make build
```
2. **Run gRPC service (server streaming)**
```bash
# starts service in localhost:8080
make run
```
3. **Make RPC call to execute arbitrary code**
You get real-time output from the executing code through the streaming endpoint, mirroring local execution.
**Request sample:**
`session_id` can be used to bind a sandbox to a session(execution), e.g for authenticated users. If not provided, the code will run in random sandboxes.
```jsonc
{
"code": "package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\tfor i := 0; i < 3; i++ {\n\t\ttime.Sleep(time.Second)\n\t\tfmt.Println(\"Hello\", i)\n\t}\n\n}\n",
"session_id": "user_1" // optional
}
```
**Response Stream:**
Success:
```jsonc
{
"output": "Hello", // stdout/stderr from executed Go code
"exec_err": "", // server error
"is_error": false, // true for server error
"timestamp": "1712415917223" // stdout/err timestamp
}
```
Error:
```json
{
"output": "main.go:10:8: undefined: time.Slseep",
"exec_err": "",
"is_error": false,
"timestamp": "1712416529383"
}
```