-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.go
More file actions
162 lines (129 loc) · 3.81 KB
/
Copy pathcommands.go
File metadata and controls
162 lines (129 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package handler
import (
"net/http"
"strings"
"voidrun/config"
"voidrun/model"
"voidrun/service"
"voidrun/util"
"github.com/gin-gonic/gin"
)
// CommandsHandler handles process management HTTP requests
type CommandsHandler struct {
commandsService *service.CommandsService
sandboxService *service.SandboxService
}
// NewCommandsHandler creates a new commands handler
func NewCommandsHandler(commandsService *service.CommandsService, sandboxService *service.SandboxService) *CommandsHandler {
return &CommandsHandler{
commandsService: commandsService,
sandboxService: sandboxService,
}
}
// Run starts a background process
// POST /sandboxes/:id/commands/run
func (h *CommandsHandler) Run(c *gin.Context) error {
id := c.Param("id")
if err := ensureSandboxRunning(c, h.sandboxService, id); err != nil {
return err
}
var req model.CommandRunRequest
if err := c.BindJSON(&req); err != nil {
return util.ErrBadRequest("Invalid request")
}
req.Command = strings.TrimSpace(req.Command)
if req.Command == "" {
return util.ErrBadRequest("Command is required")
}
if len(req.Command) > config.MaxCommandLength {
return util.ErrBadRequest("Command exceeds maximum length")
}
resp, err := h.commandsService.Run(id, req)
if err != nil {
return util.ErrInternal("Failed to run command", err)
}
c.JSON(http.StatusOK, resp)
return nil
}
// List returns all running processes
// GET /sandboxes/:id/commands/list
func (h *CommandsHandler) List(c *gin.Context) error {
id := c.Param("id")
if err := ensureSandboxRunning(c, h.sandboxService, id); err != nil {
return err
}
resp, err := h.commandsService.List(id)
if err != nil {
return util.ErrInternal("Failed to list processes", err)
}
c.JSON(http.StatusOK, resp)
return nil
}
// Kill terminates a process
// POST /sandboxes/:id/commands/kill
func (h *CommandsHandler) Kill(c *gin.Context) error {
id := c.Param("id")
if err := ensureSandboxRunning(c, h.sandboxService, id); err != nil {
return err
}
var req model.CommandKillRequest
if err := c.BindJSON(&req); err != nil {
return util.ErrBadRequest("Invalid request")
}
if req.PID <= 0 {
return util.ErrBadRequest("Invalid PID")
}
resp, err := h.commandsService.Kill(id, req.PID)
if err != nil {
return util.ErrBadRequest(err.Error())
}
c.JSON(http.StatusOK, resp)
return nil
}
// Attach streams output from a running process
// POST /sandboxes/:id/commands/attach
func (h *CommandsHandler) Attach(c *gin.Context) error {
id := c.Param("id")
if err := ensureSandboxRunning(c, h.sandboxService, id); err != nil {
return err
}
var req model.CommandAttachRequest
if err := c.BindJSON(&req); err != nil {
return util.ErrBadRequest("Invalid request")
}
if req.PID <= 0 {
return util.ErrBadRequest("Invalid PID")
}
// Set SSE headers — from this point the response is "taken over";
// any error from Attach is returned but WriteError will no-op since
// the writer is already open.
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
if err := h.commandsService.Attach(id, req.PID, c.Writer, func() { c.Writer.Flush() }); err != nil {
return util.ErrInternal(err.Error(), nil)
}
return nil
}
// Wait waits for a process to complete
// POST /sandboxes/:id/commands/wait
func (h *CommandsHandler) Wait(c *gin.Context) error {
id := c.Param("id")
if err := ensureSandboxRunning(c, h.sandboxService, id); err != nil {
return err
}
var req model.CommandWaitRequest
if err := c.BindJSON(&req); err != nil {
return util.ErrBadRequest("Invalid request")
}
if req.PID <= 0 {
return util.ErrBadRequest("Invalid PID")
}
resp, err := h.commandsService.Wait(id, req.PID)
if err != nil {
return util.ErrInternal("Wait failed", err)
}
c.JSON(http.StatusOK, resp)
return nil
}