This repository has been archived on 2026-07-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
codespace/internal/api/router.go
T
tao.chen 13b0c4e53f feat(shell): multi-shell per workspace + fix WS disconnect killing bash
BREAKING: every shell operation now requires a shellId. The Manager
previously keyed by workspaceID alone (one bash per workspace). It now
keys by (workspaceID, shellID) where shellID is a UUID returned by
Start.

Fixes the long-standing bug where the WS handler's closeAll called
stdin.Close() and killed bash when a WS client disconnected. The
Session owns the pty file; the WS handler no longer closes it. The
pty is only closed by Manager.Stop (explicit) or by captureOutput
when the process naturally exits (EOF).

- internal/shell/manager.go: Manager interface gains List and every
  method takes shellID; storage becomes
  map[workspaceID]map[shellID]*Session; Start returns (shellID, err)
  via uuid.NewString; Resize/Status/ExitStatus/Subscribe/Stdin/Stop
  route by shellID; new List(workspaceID) returns ShellInfo[] in
  creation order.
- internal/shell/session.go: Session gains ShellID + CreatedAt; Status
  type gains ShellID.
- internal/shell/manager_test.go: updated existing tests for new
  signatures; added TestShellMultiInstance (two shells in one
  workspace, no output cross-talk, independent stop, List behavior).
- internal/service/shell_service.go: wrappers carry shellID; new
  List method.
- internal/service/workspace_service.go: auto-start captures/logs
  shellID; Delete iterates and stops all workspace shells.
- internal/api/shell_handler.go: WS closeAll drops stdin.Close();
  start/restart return 201 with {shellId, pid, ...}; new list handler;
  stop/resize take shellId in body.
- internal/api/router.go: GET /api/workspaces/:id/shell (list).
- internal/model/shell.go: new ShellStartResponse, ShellInfo,
  ShellListResponse, ShellStopRequest, ShellRestartRequest; updated
  ShellStatusResponse + ShellResizeRequest to carry shellId.
- go.mod/go.sum: github.com/google/uuid.

E2E:
- workspace create -> 1 auto shell
- start 2 more -> 3 shells in list
- stop 1 -> 2 shells in list
- WS connect -> send cmd -> disconnect -> WS reconnect -> send cmd ->
  response OK, no [process exited] banner
2026-07-06 14:57:03 +08:00

65 lines
2.3 KiB
Go

package api
import (
"log/slog"
"net/http"
"codespace/internal/service"
"github.com/gin-gonic/gin"
)
// NewRouter builds a Gin engine with all API routes registered.
func NewRouter(workspaces *service.WorkspaceService, files *service.FileService, processes *service.ProcessService, shells *service.ShellService, acpSvc *service.AcpService, lg *slog.Logger, ginMode string) *gin.Engine {
gin.SetMode(ginMode)
r := gin.New()
r.Use(gin.Recovery())
r.Use(requestLogger(lg))
wsHandler := &workspaceHandler{svc: workspaces}
fileHandler := &fileHandler{svc: files}
procHandler := &processHandler{svc: processes}
shellHandler := &shellHandler{svc: shells}
acpHandler := &acpHandler{svc: acpSvc}
r.GET("/healthz", healthHandler)
api := r.Group("/api")
api.GET("/workspaces", wsHandler.list)
api.POST("/workspaces", wsHandler.create)
api.GET("/workspaces/:id", wsHandler.get)
api.DELETE("/workspaces/:id", wsHandler.delete)
api.GET("/workspaces/:id/files", fileHandler.list)
api.GET("/workspaces/:id/files/read", fileHandler.read)
api.GET("/workspaces/:id/files/stat", fileHandler.stat)
api.PUT("/workspaces/:id/files/write", fileHandler.write)
api.POST("/workspaces/:id/files/mkdir", fileHandler.mkdir)
api.DELETE("/workspaces/:id/files", fileHandler.remove)
api.POST("/workspaces/:id/files/rename", fileHandler.rename)
api.POST("/workspaces/:id/process/start", procHandler.start)
api.POST("/workspaces/:id/process/stop", procHandler.stop)
api.POST("/workspaces/:id/process/restart", procHandler.restart)
api.GET("/workspaces/:id/process/status", procHandler.status)
api.GET("/workspaces/:id/process/ws", procHandler.ws)
api.POST("/workspaces/:id/shell/start", shellHandler.start)
api.POST("/workspaces/:id/shell/stop", shellHandler.stop)
api.POST("/workspaces/:id/shell/restart", shellHandler.restart)
api.GET("/workspaces/:id/shell", shellHandler.list)
api.GET("/workspaces/:id/shell/status", shellHandler.status)
api.GET("/workspaces/:id/shell/ws", shellHandler.ws)
api.POST("/workspaces/:id/shell/resize", shellHandler.resize)
api.GET("/workspaces/:id/acp/status", acpHandler.status)
api.GET("/workspaces/:id/acp/history", acpHandler.history)
api.POST("/workspaces/:id/acp/prompt", acpHandler.prompt)
api.POST("/workspaces/:id/acp/cancel", acpHandler.cancel)
return r
}
func healthHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}