文件

API 參考

Gin 伺服器提供的上傳與沙箱執行 HTTP 端點。

端點

方法 路徑 Handler 說明
POST /upload handler.Upload 將腳本存入 Redis
POST /run/*targetPath handler.Run 執行已儲存腳本
POST /run-now handler.RunNow 立即執行提交的程式碼

POST /upload

上傳並儲存腳本,回傳版本時間戳。

Request body

欄位 型別 必要 說明
path string 存取路徑;不得含 ..
code string 原始碼
language string pythonjavascripttypescript

成功回應

{
  "path": "math/add",
  "language": "python",
  "version": 1739000000
}

錯誤

狀態 時機
400 JSON 無效、path 含 ..、不支援語言
500 Redis 寫入失敗

POST /run/*targetPath

從 Redis 載入程式碼並在沙箱執行。路徑參數為 /run/ 之後的腳本 path。

Query parameters

參數 型別 必要 說明
version int64 時間戳版本;無效或缺省用最新

Request body

欄位 型別 必要 說明
input string JSON 字串,腳本內為 event / input
stream bool 啟用 SSE 串流

Body 內的 code / language 會被 Redis metadata 覆寫。

成功(非串流)

{
  "data": 8,
  "type": "number"
}

錯誤

狀態 時機
400 Body 無效
404 腳本或版本不存在
500 沙箱失敗/逾時

POST /run-now

不經 Redis 直接執行。

Request body

欄位 型別 必要 說明
code string 原始碼
language string pythonjavascripttypescript
input string JSON 字串輸入
stream bool 啟用 SSE

錯誤

狀態 時機
400 不支援語言、空 code、JSON 錯誤
500 執行失敗

回應 type

type 意義
string JSON 字串
number 數值
json 物件或陣列
text 非 JSON 輸出

SSE 格式

stream: true 時每筆事件為:

data: {"event":"log","data":"0","type":"text"}

data: {"event":"result","data":"done","type":"string"}
event 意義
log 中間 stdout 行
result 最終回傳值
error 錯誤訊息

標頭:Content-Type: text/event-streamCache-Control: no-cacheConnection: keep-alive

支援語言

語言 Runtime 副檔名 全域變數
Python python3 .py eventinput
JavaScript node .js eventinput
TypeScript tsx .ts eventinput

範例

上傳:

curl -X POST http://localhost:8080/upload \
  -H "Content-Type: application/json" \
  -d '{"path":"math/add","language":"python","code":"return event.get(\"a\", 0) + event.get(\"b\", 0)"}'

執行最新版:

curl -X POST http://localhost:8080/run/math/add \
  -H "Content-Type: application/json" \
  -d '{"input":"{\"a\": 3, \"b\": 5}"}'

Run-now 串流:

curl -X POST http://localhost:8080/run-now \
  -H "Content-Type: application/json" \
  -d '{"language":"python","code":"return 1","input":"{}","stream":true}'

相關頁面

EN