diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:19:04 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 19:19:04 +0900 |
| commit | 0b09103e894a5f7c6865e5bdd20992457cec3c9f (patch) | |
| tree | ec0f73ab1c1f1fb884e357209f2976edf535d510 /worker | |
| parent | 0ecb9872b0e0e421d498f0afb4a8f603cba50b39 (diff) | |
| download | phperkaigi-2025-albatross-0b09103e894a5f7c6865e5bdd20992457cec3c9f.tar.gz phperkaigi-2025-albatross-0b09103e894a5f7c6865e5bdd20992457cec3c9f.tar.zst phperkaigi-2025-albatross-0b09103e894a5f7c6865e5bdd20992457cec3c9f.zip | |
feat: add worker server
Diffstat (limited to 'worker')
| -rw-r--r-- | worker/Dockerfile | 13 | ||||
| -rw-r--r-- | worker/Makefile | 7 | ||||
| -rw-r--r-- | worker/go.mod | 3 | ||||
| -rw-r--r-- | worker/main.go | 57 |
4 files changed, 80 insertions, 0 deletions
diff --git a/worker/Dockerfile b/worker/Dockerfile new file mode 100644 index 0000000..1d1523d --- /dev/null +++ b/worker/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.22.3 AS builder + +WORKDIR /build +COPY . /build +RUN go build -o /build/server . + +################################################################################ +FROM golang:1.22.3 + +WORKDIR /app +COPY --from=builder /build/server /app/server + +CMD ["/app/server"] diff --git a/worker/Makefile b/worker/Makefile new file mode 100644 index 0000000..4d6cf39 --- /dev/null +++ b/worker/Makefile @@ -0,0 +1,7 @@ +.PHONY: fmt +fmt: + go fmt ./... + +.PHONY: check +check: + go build -o /dev/null ./... diff --git a/worker/go.mod b/worker/go.mod new file mode 100644 index 0000000..456ce16 --- /dev/null +++ b/worker/go.mod @@ -0,0 +1,3 @@ +module github.com/nsfisis/iosdc-2024-albatross/worker + +go 1.22.3 diff --git a/worker/main.go b/worker/main.go new file mode 100644 index 0000000..0a52b0e --- /dev/null +++ b/worker/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "encoding/json" + "net/http" + "strconv" + "time" +) + +type RequestBody struct { + Code string `json:"code"` + Stdin string `json:"stdin"` +} + +type ResponseBody struct { + Result string `json:"result"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} + +func doExec(code string, stdin string, maxDuration time.Duration) ResponseBody { + _ = code + _ = stdin + _ = maxDuration + + return ResponseBody{ + Result: "success", + Stdout: "42", + Stderr: "", + } +} + +func execHandler(w http.ResponseWriter, r *http.Request) { + maxDurationStr := r.URL.Query().Get("max_duration") + maxDuration, err := strconv.Atoi(maxDurationStr) + if err != nil || maxDuration <= 0 { + http.Error(w, "Invalid max_duration parameter", http.StatusBadRequest) + return + } + + var reqBody RequestBody + err = json.NewDecoder(r.Body).Decode(&reqBody) + if err != nil { + http.Error(w, "Invalid request body", http.StatusBadRequest) + return + } + + resBody := doExec(reqBody.Code, reqBody.Stdin, time.Duration(maxDuration)*time.Second) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(resBody) +} + +func main() { + http.HandleFunc("/api/exec", execHandler) + http.ListenAndServe(":80", nil) +} |
