feat: implement port forwarding
This commit is contained in:
@@ -17,6 +17,8 @@ type updateWorkspaceRequestBody struct {
|
||||
PortMappings []portMapping `json:"ports"`
|
||||
}
|
||||
|
||||
const keyCurrentWorkspace = "currentWorkspace"
|
||||
|
||||
func fetchAllWorkspaces(c echo.Context) error {
|
||||
mgr := workspaceManagerFrom(c)
|
||||
workspaces, err := mgr.findAllWorkspaces(c.Request().Context())
|
||||
@@ -26,28 +28,44 @@ func fetchAllWorkspaces(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, workspaces)
|
||||
}
|
||||
|
||||
func currentWorkspace(c echo.Context) *workspace {
|
||||
return c.Get(keyCurrentWorkspace).(*workspace)
|
||||
}
|
||||
|
||||
func currentWorkspaceMiddleware(ignoreMissing bool) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
workspaceName := c.Param("workspaceName")
|
||||
if workspaceName == "" || !workspaceNameRegex.MatchString(workspaceName) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
mgr := workspaceManagerFrom(c)
|
||||
workspace, err := mgr.findWorkspace(c.Request().Context(), workspaceName)
|
||||
if err != nil {
|
||||
if errors.Is(err, errWorkspaceNotFound) {
|
||||
if ignoreMissing {
|
||||
c.Set(keyCurrentWorkspace, nil)
|
||||
} else {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c.Set(keyCurrentWorkspace, workspace)
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateOrCreateWorkspace(c echo.Context) error {
|
||||
workspaceName := c.Param("workspaceName")
|
||||
if workspaceName == "" {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
workspace := currentWorkspace(c)
|
||||
if workspace == nil {
|
||||
return createWorkspace(c, c.Param("workspaceName"))
|
||||
}
|
||||
|
||||
if !workspaceNameRegex.MatchString(workspaceName) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
ctx := c.Request().Context()
|
||||
mgr := workspaceManagerFrom(c)
|
||||
|
||||
exists, err := mgr.hasWorkspace(ctx, workspaceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return createWorkspace(c, workspaceName)
|
||||
}
|
||||
|
||||
return updateWorkspace(c, workspaceName)
|
||||
return updateWorkspace(c, workspace)
|
||||
}
|
||||
|
||||
func createWorkspace(c echo.Context, workspaceName string) error {
|
||||
@@ -72,7 +90,7 @@ func createWorkspace(c echo.Context, workspaceName string) error {
|
||||
return c.JSON(http.StatusOK, w)
|
||||
}
|
||||
|
||||
func updateWorkspace(c echo.Context, workspaceName string) error {
|
||||
func updateWorkspace(c echo.Context, workspace *workspace) error {
|
||||
ctx := c.Request().Context()
|
||||
|
||||
var body updateWorkspaceRequestBody
|
||||
@@ -83,14 +101,6 @@ func updateWorkspace(c echo.Context, workspaceName string) error {
|
||||
|
||||
mgr := workspaceManagerFrom(c)
|
||||
|
||||
workspace, err := mgr.findWorkspace(ctx, workspaceName)
|
||||
if err != nil {
|
||||
if errors.Is(err, errWorkspaceNotFound) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
switch status(body.Status) {
|
||||
case statusStopped:
|
||||
if err = mgr.stopWorkspace(ctx, workspace); err != nil {
|
||||
@@ -115,13 +125,9 @@ func updateWorkspace(c echo.Context, workspaceName string) error {
|
||||
}
|
||||
|
||||
func deleteWorkspace(c echo.Context) error {
|
||||
workspaceName := c.Param("workspaceName")
|
||||
if workspaceName == "" {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
workspace := currentWorkspace(c)
|
||||
mgr := workspaceManagerFrom(c)
|
||||
if err := mgr.deleteWorkspace(c.Request().Context(), workspaceName); err != nil {
|
||||
if err := mgr.deleteWorkspace(c.Request().Context(), workspace); err != nil {
|
||||
if errors.Is(err, errWorkspaceNotFound) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
@@ -130,3 +136,31 @@ func deleteWorkspace(c echo.Context) error {
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func deleteWorkspacePortMapping(c echo.Context) error {
|
||||
workspace := currentWorkspace(c)
|
||||
mgr := workspaceManagerFrom(c)
|
||||
|
||||
portName := c.Param("portName")
|
||||
if portName == "" {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
var portMapping *portMapping
|
||||
for _, m := range workspace.PortMappings {
|
||||
if m.Subdomain == portName {
|
||||
portMapping = &m
|
||||
break
|
||||
}
|
||||
}
|
||||
if portMapping == nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
err := mgr.deletePortMapping(c.Request().Context(), workspace, portMapping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
Reference in New Issue
Block a user