aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/main.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-02 19:16:58 +0900
committernsfisis <nsfisis@gmail.com>2024-08-03 18:56:00 +0900
commitf70d6eed9f2c519aed030c9dbda99ed0435991a0 (patch)
tree2c4e95392c88fcdbccafe62e42b651f4fbde0491 /worker/main.go
parentdb06c9332776b41b3fef537f9e6d76d38f0463b3 (diff)
downloadiosdc-japan-2025-albatross-f70d6eed9f2c519aed030c9dbda99ed0435991a0.tar.gz
iosdc-japan-2025-albatross-f70d6eed9f2c519aed030c9dbda99ed0435991a0.tar.zst
iosdc-japan-2025-albatross-f70d6eed9f2c519aed030c9dbda99ed0435991a0.zip
feat: implement worker
Diffstat (limited to 'worker/main.go')
-rw-r--r--worker/main.go64
1 files changed, 20 insertions, 44 deletions
diff --git a/worker/main.go b/worker/main.go
index 0a52b0e..8134a56 100644
--- a/worker/main.go
+++ b/worker/main.go
@@ -1,57 +1,33 @@
package main
import (
- "encoding/json"
+ "log"
"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
+ echojwt "github.com/labstack/echo-jwt/v4"
+ "github.com/labstack/echo/v4"
+ "github.com/labstack/echo/v4/middleware"
+)
- return ResponseBody{
- Result: "success",
- Stdout: "42",
- Stderr: "",
+func main() {
+ if err := prepareDirectories(); err != nil {
+ log.Fatal(err)
}
-}
-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
- }
+ e := echo.New()
- var reqBody RequestBody
- err = json.NewDecoder(r.Body).Decode(&reqBody)
- if err != nil {
- http.Error(w, "Invalid request body", http.StatusBadRequest)
- return
- }
+ e.Use(middleware.Logger())
+ e.Use(middleware.Recover())
- resBody := doExec(reqBody.Code, reqBody.Stdin, time.Duration(maxDuration)*time.Second)
+ e.Use(echojwt.WithConfig(echojwt.Config{
+ SigningKey: []byte("TODO"),
+ }))
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(resBody)
-}
+ e.POST("/api/swiftc", handleSwiftCompile)
+ e.POST("/api/wasmc", handleWasmCompile)
+ e.POST("/api/testrun", handleTestRun)
-func main() {
- http.HandleFunc("/api/exec", execHandler)
- http.ListenAndServe(":80", nil)
+ if err := e.Start(":80"); err != http.ErrServerClosed {
+ log.Fatal(err)
+ }
}