Initial commit

Daniel Tanaka 1 year ago
commit 2b62f451060df2e6297e7e4dfac8a322c6990cfd
3 changed files with 49 additions and 0 deletions
+8-0README.md ADDED
View File
@@ -0,0 +1,8 @@
1+# playground
2+
3+The code runner behind the glibcode.net playground. Every run gets a container with no network, a read-only file system, 128 MB and 5 seconds.
4+
5+```sh
6+go run . # listens on :8088
7+curl -X POST localhost:8088/run -d '{"language":"python","code":"print(42)"}'
8+```
+3-0go.mod ADDED
View File
@@ -0,0 +1,3 @@
1+module git.glibcode.net/glibcode/playground
2+
3+go 1.22
+38-0main.go ADDED
View File
@@ -0,0 +1,38 @@
1+package main
2+
3+import (
4+ "encoding/json"
5+ "log"
6+ "net/http"
7+ "os"
8+
9+ "git.glibcode.net/glibcode/playground/runner"
10+)
11+
12+type request struct {
13+ Language string `json:"language"`
14+ Code string `json:"code"`
15+}
16+
17+func main() {
18+ pool := runner.NewPool(4)
19+ http.HandleFunc("POST /run", func(w http.ResponseWriter, r *http.Request) {
20+ var req request
21+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&req); err != nil {
22+ http.Error(w, "bad request", http.StatusBadRequest)
23+ return
24+ }
25+ res, err := pool.Run(r.Context(), req.Language, req.Code)
26+ if err != nil {
27+ w.WriteHeader(http.StatusAccepted)
28+ json.NewEncoder(w).Encode(map[string]string{"status": "queued"})
29+ return
30+ }
31+ json.NewEncoder(w).Encode(res)
32+ })
33+ addr := os.Getenv("ADDR")
34+ if addr == "" {
35+ addr = ":8088"
36+ }
37+ log.Fatal(http.ListenAndServe(addr, nil))
38+}