1 year ago
38 lines851 B
package main | |
| |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"os" | |
| |
"git.glibcode.net/glibcode/playground/runner" | |
) | |
| |
type request struct { | |
Language string `json:"language"` | |
Code string `json:"code"` | |
} | |
| |
func main() { | |
pool := runner.NewPool(4) | |
http.HandleFunc("POST /run", func(w http.ResponseWriter, r *http.Request) { | |
var req request | |
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&req); err != nil { | |
http.Error(w, "bad request", http.StatusBadRequest) | |
return | |
} | |
res, err := pool.Run(r.Context(), req.Language, req.Code) | |
if err != nil { | |
w.WriteHeader(http.StatusAccepted) | |
json.NewEncoder(w).Encode(map[string]string{"status": "queued"}) | |
return | |
} | |
json.NewEncoder(w).Encode(res) | |
}) | |
addr := os.Getenv("ADDR") | |
if addr == "" { | |
addr = ":8088" | |
} | |
log.Fatal(http.ListenAndServe(addr, nil)) | |
} |