|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os/exec" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/onkernel/kernel-images/server/lib/logger" |
| 12 | +) |
| 13 | + |
| 14 | +type ExecutePlaywrightRequest struct { |
| 15 | + Code string `json:"code"` |
| 16 | + TimeoutSec *int `json:"timeout_sec,omitempty"` |
| 17 | +} |
| 18 | + |
| 19 | +type ExecutePlaywrightResult struct { |
| 20 | + Success bool `json:"success"` |
| 21 | + Result interface{} `json:"result,omitempty"` |
| 22 | + Error string `json:"error,omitempty"` |
| 23 | + Stdout string `json:"stdout,omitempty"` |
| 24 | + Stderr string `json:"stderr,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +func (s *Service) ExecutePlaywrightCode(w http.ResponseWriter, r *http.Request) { |
| 28 | + log := logger.FromContext(r.Context()) |
| 29 | + |
| 30 | + var req ExecutePlaywrightRequest |
| 31 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 32 | + http.Error(w, fmt.Sprintf("invalid request body: %v", err), http.StatusBadRequest) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if req.Code == "" { |
| 37 | + http.Error(w, "code is required", http.StatusBadRequest) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + timeout := 30 * time.Second |
| 42 | + if req.TimeoutSec != nil && *req.TimeoutSec > 0 { |
| 43 | + timeout = time.Duration(*req.TimeoutSec) * time.Second |
| 44 | + } |
| 45 | + |
| 46 | + ctx, cancel := context.WithTimeout(r.Context(), timeout) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + cmd := exec.CommandContext(ctx, "tsx", "/usr/local/lib/playwright-executor.ts", req.Code) |
| 50 | + |
| 51 | + output, err := cmd.CombinedOutput() |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + if ctx.Err() == context.DeadlineExceeded { |
| 55 | + log.Error("playwright execution timed out", "timeout", timeout) |
| 56 | + w.Header().Set("Content-Type", "application/json") |
| 57 | + w.WriteHeader(http.StatusOK) |
| 58 | + json.NewEncoder(w).Encode(ExecutePlaywrightResult{ |
| 59 | + Success: false, |
| 60 | + Error: fmt.Sprintf("execution timed out after %v", timeout), |
| 61 | + }) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + log.Error("playwright execution failed", "error", err, "output", string(output)) |
| 66 | + w.Header().Set("Content-Type", "application/json") |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + |
| 69 | + var result ExecutePlaywrightResult |
| 70 | + if jsonErr := json.Unmarshal(output, &result); jsonErr == nil { |
| 71 | + json.NewEncoder(w).Encode(result) |
| 72 | + } else { |
| 73 | + json.NewEncoder(w).Encode(ExecutePlaywrightResult{ |
| 74 | + Success: false, |
| 75 | + Error: fmt.Sprintf("execution failed: %v", err), |
| 76 | + Stderr: string(output), |
| 77 | + }) |
| 78 | + } |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + var result ExecutePlaywrightResult |
| 83 | + if err := json.Unmarshal(output, &result); err != nil { |
| 84 | + log.Error("failed to parse playwright output", "error", err, "output", string(output)) |
| 85 | + w.Header().Set("Content-Type", "application/json") |
| 86 | + w.WriteHeader(http.StatusOK) |
| 87 | + json.NewEncoder(w).Encode(ExecutePlaywrightResult{ |
| 88 | + Success: false, |
| 89 | + Error: fmt.Sprintf("failed to parse output: %v", err), |
| 90 | + Stdout: string(output), |
| 91 | + }) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + w.Header().Set("Content-Type", "application/json") |
| 96 | + w.WriteHeader(http.StatusOK) |
| 97 | + json.NewEncoder(w).Encode(result) |
| 98 | +} |
0 commit comments