Skip to content

Modern Docker Management, Designed for Everyone

feat: use websockets for streaming logs (#495)

Kyle Mendell42cf76b7fac6
diff --git a/backend/go.mod b/backend/go.mod
index 5b0900625..1077af4bf 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -16,6 +16,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/golang-migrate/migrate/v4 v4.19.0
github.com/google/uuid v1.6.0
+ github.com/gorilla/websocket v1.5.3
github.com/jinzhu/copier v0.4.0
github.com/joho/godotenv v1.5.1
github.com/lmittmann/tint v1.1.2
diff --git a/backend/go.sum b/backend/go.sum
index c153cb3be..d0e6825f2 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -86,6 +86,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
+github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
diff --git a/backend/internal/api/container_handler.go b/backend/internal/api/container_handler.go
index d77d71092..3286d86ca 100644
--- a/backend/internal/api/container_handler.go
+++ b/backend/internal/api/container_handler.go
@@ -1,9 +1,11 @@
package api
import (
+ "context"
"io"
"net/http"
- "time"
+ "strings"
+ "sync"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
@@ -13,12 +15,14 @@ import (
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+ ws "github.com/ofkm/arcane-backend/internal/utils/ws"
)
type ContainerHandler struct {
containerService *services.ContainerService
imageService *services.ImageService
dockerService *services.DockerClientService
+ logStreams sync.Map // map[string]*logStream
}
func NewContainerHandler(group *gin.RouterGroup, dockerService *services.DockerClientService, containerService *services.ContainerService, imageService *services.ImageService, authMiddleware *middleware.AuthMiddleware) {
@@ -36,12 +40,66 @@ func NewContainerHandler(group *gin.RouterGroup, dockerService *services.DockerC
apiGroup.POST("/:id/stop", handler.Stop)
apiGroup.POST("/:id/restart", handler.Restart)
apiGroup.GET("/:id/logs", handler.GetLogs)
- apiGroup.GET("/:id/logs/stream", handler.GetLogsStream)
+ apiGroup.GET("/:id/logs/ws", handler.GetLogsWS)
apiGroup.DELETE("/:id", handler.Delete)
}
}
+type containerLogStream struct {
+ hub *ws.Hub
+ once sync.Once
+ cancel context.CancelFunc
+}
+
+func (h *ContainerHandler) getOrStartContainerLogHub(containerID string, follow bool, tail, since string, timestamps bool) *ws.Hub {
+ v, _ := h.logStreams.LoadOrStore(containerID, &containerLogStream{
+ hub: ws.NewHub(1024),
+ })
+ ls := v.(*containerLogStream)
+
+ ls.once.Do(func() {
+ ctx, cancel := context.WithCancel(context.Background())
+ ls.cancel = cancel
+
+ go ls.hub.Run(ctx)
+
+ lines := make(chan string, 256)
+ go func() {
+ defer close(lines)
+ _ = h.containerService.StreamLogs(ctx, containerID, lines, follow, tail, since, timestamps)
+ }()
+ go ws.ForwardLines(ctx, ls.hub, lines)
+ })
+
+ return ls.hub
+}
+
+// GET /api/containers/:id/logs/ws
+// /api/environments/:envId/containers/:containerId/logs/ws
+func (h *ContainerHandler) GetLogsWS(c *gin.Context) {
+ containerID := c.Param("containerId")
+ if containerID == "" {
+ containerID = c.Param("id")
+ }
+ if strings.TrimSpace(containerID) == "" {
+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Container ID is required"}})
+ return
+ }
+
+ follow := c.DefaultQuery("follow", "true") == "true"
+ tail := c.DefaultQuery("tail", "100")
+ since := c.Query("since")
+ timestamps := c.DefaultQuery("timestamps", "false") == "true"
+
+ conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
+ if err != nil {
+ return
+ }
+ hub := h.getOrStartContainerLogHub(containerID, follow, tail, since, timestamps)
+ ws.ServeClient(context.Background(), hub, conn)
+}
+
func (h *ContainerHandler) PullImage(c *gin.Context) {
id := c.Param("containerId")
@@ -499,56 +557,3 @@ func (h *ContainerHandler) GetStatsStream(c *gin.Context) {
}
})
}
-
-func (h *ContainerHandler) GetLogsStream(c *gin.Context) {
- containerID := c.Param("containerId")
- if containerID == "" {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "data": gin.H{"error": "Container ID is required"},
- })
- return
- }
-
- follow := c.DefaultQuery("follow", "true") == "true"
- tail := c.DefaultQuery("tail", "100")
- since := c.Query("since")
- timestamps := c.DefaultQuery("timestamps", "false") == "true"
-
- c.Header("Content-Type", "text/event-stream")
- c.Header("Cache-Control", "no-cache")
- c.Header("Connection", "keep-alive")
- c.Header("Access-Control-Allow-Origin", "*")
-
- logsChan := make(chan string, 10)
- errChan := make(chan error, 1)
-
- go func() {
- defer close(logsChan)
- defer close(errChan)
-
- if err := h.containerService.StreamLogs(c.Request.Context(), containerID, logsChan, follow, tail, since, timestamps); err != nil {
- errChan <- err
- }
- }()
-
- c.Stream(func(w io.Writer) bool {
- select {
- case logLine, ok := <-logsChan:
- if !ok {
- return false
- }
- c.SSEvent("log", gin.H{"data": logLine, "timestamp": time.Now()})
- return true
- case err, ok := <-errChan:
- if !ok || err == nil {
- // graceful shutdown or no error; stop streaming
- return false
- }
- c.SSEvent("error", gin.H{"error": err.Error()})
- return false
- case <-c.Request.Context().Done():
- return false
- }
- })
-}
diff --git a/backend/internal/api/environment_handler.go b/backend/internal/api/environment_handler.go
index e70077a90..34d42b96c 100644
--- a/backend/internal/api/environment_handler.go
+++ b/backend/internal/api/environment_handler.go
@@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
+ "path"
"strings"
"time"
@@ -15,6 +17,8 @@ import (
"github.com/ofkm/arcane-backend/internal/models"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+
+ wsutil "github.com/ofkm/arcane-backend/internal/utils/ws"
)
const LOCAL_DOCKER_ENVIRONMENT_ID = "0"
@@ -61,6 +65,7 @@ func NewEnvironmentHandler(
}
apiGroup := group.Group("/environments")
+
apiGroup.Use(authMiddleware.WithAdminNotRequired().Add())
{
apiGroup.GET("", handler.ListEnvironments)
@@ -81,9 +86,9 @@ func NewEnvironmentHandler(
apiGroup.POST("/:id/containers/:containerId/restart", handler.RestartContainer)
apiGroup.DELETE("/:id/containers/:containerId", handler.RemoveContainer)
apiGroup.GET("/:id/containers/:containerId/logs", handler.GetContainerLogs)
- apiGroup.GET("/:id/containers/:containerId/logs/stream", handler.GetContainerLogsStream)
apiGroup.GET("/:id/containers/:containerId/stats", handler.GetContainerStats)
apiGroup.GET("/:id/containers/:containerId/stats/stream", handler.GetContainerStatsStream)
+ apiGroup.GET("/:id/containers/:containerId/logs/ws", handler.GetContainerLogsWS)
apiGroup.GET("/:id/images", handler.GetImages)
apiGroup.GET("/:id/images/:imageId", handler.GetImage)
@@ -118,8 +123,7 @@ func NewEnvironmentHandler(
apiGroup.POST("/:id/stacks/:stackId/redeploy", handler.RedeployStack)
apiGroup.POST("/:id/stacks/:stackId/down", handler.DownStack)
apiGroup.DELETE("/:id/stacks/:stackId/destroy", handler.DestroyStack)
- apiGroup.GET("/:id/stacks/:stackId/logs/stream", handler.GetStackLogsStream)
- apiGroup.POST("/:id/stacks/convert", handler.ConvertDockerRun)
+ apiGroup.GET("/:id/stacks/:stackId/logs/ws", handler.GetStackLogsWS)
apiGroup.GET("/:id/image-updates/check", handler.CheckImageUpdate)
apiGroup.GET("/:id/image-updates/check/:imageId", handler.CheckImageUpdateByID)
@@ -314,8 +318,8 @@ func (h *EnvironmentHandler) handleContainerEndpoints(c *gin.Context, endpoint s
case strings.HasPrefix(endpoint, "/containers/") && strings.HasSuffix(endpoint, "/pull"):
containerHandler.PullImage(c)
return true
- case strings.HasPrefix(endpoint, "/containers/") && strings.HasSuffix(endpoint, "/logs/stream"):
- containerHandler.GetLogsStream(c)
+ case strings.HasPrefix(endpoint, "/containers/") && strings.HasSuffix(endpoint, "/logs/ws"):
+ containerHandler.GetLogsWS(c)
return true
case strings.HasPrefix(endpoint, "/containers/") && strings.HasSuffix(endpoint, "/logs"):
containerHandler.GetLogs(c)
@@ -457,8 +461,8 @@ func (h *EnvironmentHandler) handleStackEndpoints(c *gin.Context, endpoint strin
case strings.HasPrefix(endpoint, "/stacks/") && strings.HasSuffix(endpoint, "/destroy"):
stackHandler.DestroyStack(c)
return true
- case strings.HasPrefix(endpoint, "/stacks/") && strings.HasSuffix(endpoint, "/logs/stream"):
- stackHandler.GetStackLogsStream(c)
+ case strings.HasSuffix(endpoint, "/logs/ws"):
+ stackHandler.GetStackLogsWS(c)
return true
case strings.HasPrefix(endpoint, "/stacks/") && c.Request.Method == http.MethodGet:
stackHandler.GetStack(c)
@@ -892,11 +896,6 @@ func (h *EnvironmentHandler) GetContainerStatsStream(c *gin.Context) {
h.routeRequest(c, "/containers/"+containerID+"/stats/stream")
}
-func (h *EnvironmentHandler) GetContainerLogsStream(c *gin.Context) {
- containerID := c.Param("containerId")
- h.routeRequest(c, "/containers/"+containerID+"/logs/stream")
-}
-
// End Containers
// Images
@@ -1012,11 +1011,90 @@ func (h *EnvironmentHandler) DestroyStack(c *gin.Context) {
h.routeRequest(c, "/stacks/"+stackId+"/destroy")
}
-func (h *EnvironmentHandler) GetStackLogsStream(c *gin.Context) {
+func (h *EnvironmentHandler) GetStackLogsWS(c *gin.Context) {
+ envID := c.Param("id")
stackId := c.Param("stackId")
- h.routeRequest(c, "/stacks/"+stackId+"/logs/stream")
+
+ if envID == LOCAL_DOCKER_ENVIRONMENT_ID {
+ h.routeRequest(c, "/stacks/"+stackId+"/logs/ws")
+ return
+ }
+
+ environment, err := h.environmentService.GetEnvironmentByID(c.Request.Context(), envID)
+ if err != nil || environment == nil || !environment.Enabled {
+ c.JSON(http.StatusNotFound, gin.H{"success": false, "data": gin.H{"error": "Environment not found or disabled"}})
+ return
+ }
+
+ u, err := url.Parse(strings.TrimRight(environment.ApiUrl, "/"))
+ if err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Invalid environment URL"}})
+ return
+ }
+ if u.Scheme == "https" {
+ u.Scheme = "wss"
+ } else {
+ u.Scheme = "ws"
+ }
+ u.Path = path.Join(u.Path, "/api/environments/"+LOCAL_DOCKER_ENVIRONMENT_ID+"/stacks/"+stackId+"/logs/ws")
+ u.RawQuery = c.Request.URL.RawQuery
+
+ hdr := http.Header{}
+ // Forward auth if present
+ if auth := c.GetHeader("Authorization"); auth != "" {
+ hdr.Set("Authorization", auth)
+ } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {
+ hdr.Set("Authorization", "Bearer "+cookieToken)
+ }
+ // Agent token
+ if environment.AccessToken != nil && *environment.AccessToken != "" {
+ hdr.Set("X-Arcane-Agent-Token", *environment.AccessToken)
+ }
+
+ _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)
}
func (h *EnvironmentHandler) ConvertDockerRun(c *gin.Context) {
h.routeRequest(c, "/stacks/convert")
}
+
+func (h *EnvironmentHandler) GetContainerLogsWS(c *gin.Context) {
+ envID := c.Param("id")
+ containerID := c.Param("containerId")
+
+ if envID == LOCAL_DOCKER_ENVIRONMENT_ID {
+ h.routeRequest(c, "/containers/"+containerID+"/logs/ws")
+ return
+ }
+
+ environment, err := h.environmentService.GetEnvironmentByID(c.Request.Context(), envID)
+ if err != nil || environment == nil || !environment.Enabled {
+ c.JSON(http.StatusNotFound, gin.H{"success": false, "data": gin.H{"error": "Environment not found or disabled"}})
+ return
+ }
+
+ u, err := url.Parse(strings.TrimRight(environment.ApiUrl, "/"))
+ if err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Invalid environment URL"}})
+ return
+ }
+ if u.Scheme == "https" {
+ u.Scheme = "wss"
+ } else {
+ u.Scheme = "ws"
+ }
+ u.Path = path.Join(u.Path, "/api/environments/"+LOCAL_DOCKER_ENVIRONMENT_ID+"/containers/"+containerID+"/logs/ws")
+ u.RawQuery = c.Request.URL.RawQuery
+
+ hdr := http.Header{}
+ if auth := c.GetHeader("Authorization"); auth != "" {
+ hdr.Set("Authorization", auth)
+ } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {
+ hdr.Set("Authorization", "Bearer "+cookieToken)
+ }
+ if environment.AccessToken != nil && *environment.AccessToken != "" {
+ hdr.Set("X-Arcane-Agent-Token", *environment.AccessToken)
+ }
+
+ _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)
+}
diff --git a/backend/internal/api/stack_handler.go b/backend/internal/api/stack_handler.go
index 037558c5b..76e606e1c 100644
--- a/backend/internal/api/stack_handler.go
+++ b/backend/internal/api/stack_handler.go
@@ -4,20 +4,23 @@ import (
"context"
"errors"
"fmt"
- "io"
"net/http"
"strings"
+ "sync"
"time"
"github.com/gin-gonic/gin"
+ "github.com/gorilla/websocket"
"github.com/ofkm/arcane-backend/internal/dto"
"github.com/ofkm/arcane-backend/internal/middleware"
"github.com/ofkm/arcane-backend/internal/services"
"github.com/ofkm/arcane-backend/internal/utils"
+ ws "github.com/ofkm/arcane-backend/internal/utils/ws"
)
type StackHandler struct {
stackService *services.StackService
+ logStreams sync.Map // map[string]*logStream
}
func NewStackHandler(group *gin.RouterGroup, stackService *services.StackService, authMiddleware *middleware.AuthMiddleware) {
@@ -38,10 +41,69 @@ func NewStackHandler(group *gin.RouterGroup, stackService *services.StackService
apiGroup.POST("/:id/redeploy", handler.RedeployStack)
apiGroup.POST("/:id/down", handler.DownStack)
apiGroup.DELETE("/:id/destroy", handler.DestroyStack)
- apiGroup.GET("/:id/logs/stream", handler.GetStackLogsStream)
+ apiGroup.GET("/:id/logs/ws", handler.GetStackLogsWS)
}
}
+var wsUpgrader = websocket.Upgrader{
+ CheckOrigin: func(r *http.Request) bool { return true },
+}
+
+type projectLogStream struct {
+ hub *ws.Hub
+ once sync.Once
+ cancel context.CancelFunc
+}
+
+func (h *StackHandler) getOrStartStackLogHub(stackID string, follow bool, tail, since string, timestamps bool) *ws.Hub {
+ v, _ := h.logStreams.LoadOrStore(stackID, &projectLogStream{
+ hub: ws.NewHub(1024),
+ })
+ ls := v.(*projectLogStream)
+
+ ls.once.Do(func() {
+ ctx, cancel := context.WithCancel(context.Background())
+ ls.cancel = cancel
+
+ go ls.hub.Run(ctx)
+
+ lines := make(chan string, 256)
+ go func() {
+ defer close(lines)
+ _ = h.stackService.StreamStackLogs(ctx, stackID, lines, follow, tail, since, timestamps)
+ }()
+ go ws.ForwardLines(ctx, ls.hub, lines)
+ })
+
+ return ls.hub
+}
+
+// WebSocket endpoint: /api/stacks/:id/logs/ws and /api/environments/:id/stacks/:stackId/logs/ws
+func (h *StackHandler) GetStackLogsWS(c *gin.Context) {
+ stackID := c.Param("stackId")
+ if stackID == "" {
+ stackID = c.Param("id")
+ }
+ if strings.TrimSpace(stackID) == "" {
+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "Stack ID is required"})
+ return
+ }
+
+ follow := c.DefaultQuery("follow", "true") == "true"
+ tail := c.DefaultQuery("tail", "100")
+ since := c.Query("since")
+ timestamps := c.DefaultQuery("timestamps", "false") == "true"
+
+ conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
+ if err != nil {
+ return
+ }
+ hub := h.getOrStartStackLogHub(stackID, follow, tail, since, timestamps)
+
+ // don't use the request context here; it is canceled when handler returns.
+ ws.ServeClient(context.Background(), hub, conn)
+}
+
func (h *StackHandler) ListStacks(c *gin.Context) {
var req utils.SortedPaginationRequest
if err := c.ShouldBindQuery(&req); err != nil {
@@ -510,102 +572,6 @@ func (h *StackHandler) PullImages(c *gin.Context) {
}
}
-func (h *StackHandler) GetStackLogsStream(c *gin.Context) {
- stackID := c.Param("stackId")
- if stackID == "" {
- c.JSON(http.StatusBadRequest, gin.H{
- "success": false,
- "error": "Stack ID is required",
- })
- return
- }
-
- follow := c.DefaultQuery("follow", "true") == "true"
- tail := c.DefaultQuery("tail", "100")
- since := c.Query("since")
- timestamps := c.DefaultQuery("timestamps", "false") == "true"
-
- c.Header("Content-Type", "text/event-stream")
- c.Header("Cache-Control", "no-cache")
- c.Header("Connection", "keep-alive")
- c.Header("Access-Control-Allow-Origin", "*")
-
- logsChan := make(chan string, 100)
- errChan := make(chan error, 1)
-
- ctx, cancel := context.WithCancel(c.Request.Context())
- defer cancel()
-
- go func() {
- defer close(logsChan)
- defer close(errChan)
-
- if err := h.stackService.StreamStackLogs(ctx, stackID, logsChan, follow, tail, since, timestamps); err != nil {
- errChan <- err
- }
- }()
-
- c.Stream(func(w io.Writer) bool {
- select {
- case logLine, ok := <-logsChan:
- if !ok {
- return false
- }
- parsedLog := h.parseStackLogLine(logLine)
- c.SSEvent("log", parsedLog)
- return true
- case err, ok := <-errChan:
- if !ok || err == nil {
- return false
- }
- c.SSEvent("error", gin.H{"error": err.Error()})
- return false
- case <-ctx.Done():
- return false
- case <-time.After(30 * time.Second):
- c.SSEvent("ping", gin.H{"message": "keepalive"})
- return true
- }
- })
-}
-
-func (h *StackHandler) parseStackLogLine(logLine string) gin.H {
- var service, message, timestamp string
- var level = "info"
-
- if strings.HasPrefix(logLine, "[STDERR] ") {
- level = "stderr"
- logLine = strings.TrimPrefix(logLine, "[STDERR] ")
- }
-
- parts := strings.SplitN(logLine, " ", 2)
- if len(parts) == 2 && strings.Contains(parts[0], "T") && strings.Contains(parts[0], "Z") {
- timestamp = parts[0]
- logLine = parts[1]
- } else {
- timestamp = time.Now().Format(time.RFC3339Nano)
- }
-
- if strings.Contains(logLine, " | ") {
- serviceParts := strings.SplitN(logLine, " | ", 2)
- if len(serviceParts) == 2 {
- service = strings.TrimSpace(serviceParts[0])
- message = serviceParts[1]
- } else {
- message = logLine
- }
- } else {
- message = logLine
- }
-
- return gin.H{
- "level": level,
- "message": message,
- "timestamp": timestamp,
- "service": service,
- }
-}
-
func (h *StackHandler) GetProjectStatusCounts(c *gin.Context) {
_, running, stopped, total, err := h.stackService.GetProjectStatusCounts(c.Request.Context())
if err != nil {
diff --git a/backend/internal/utils/ws/bridge.go b/backend/internal/utils/ws/bridge.go
new file mode 100644
index 000000000..52e7b2e07
--- /dev/null
+++ b/backend/internal/utils/ws/bridge.go
@@ -0,0 +1,52 @@
+package ws
+
+import (
+ "context"
+ "encoding/json"
+ "time"
+)
+
+type LogMessage struct {
+ Level string `json:"level,omitempty"`
+ Message string `json:"message"`
+ Timestamp time.Time `json:"timestamp"`
+ Service string `json:"service,omitempty"`
+ ContainerID string `json:"containerId,omitempty"`
+}
+
+// ForwardLines forwards plain text lines to the hub.
+func ForwardLines(ctx context.Context, hub *Hub, lines <-chan string) {
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case line, ok := <-lines:
+ if !ok {
+ return
+ }
+ hub.Broadcast([]byte(line))
+ }
+ }
+}
+
+// ForwardLogJSON marshals LogMessage and forwards JSON to the hub.
+func ForwardLogJSON(ctx context.Context, hub *Hub, logs <-chan LogMessage) {
+ enc := json.NewEncoder(nil) // no reuse, just use json.Marshal below for simplicity
+ _ = enc // avoid linter; using Marshal for perf/control
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case m, ok := <-logs:
+ if !ok {
+ return
+ }
+ if m.Timestamp.IsZero() {
+ m.Timestamp = time.Now()
+ }
+ if b, err := json.Marshal(m); err == nil {
+ hub.Broadcast(b)
+ }
+ }
+ }
+}
diff --git a/backend/internal/utils/ws/client.go b/backend/internal/utils/ws/client.go
new file mode 100644
index 000000000..b86513b14
--- /dev/null
+++ b/backend/internal/utils/ws/client.go
@@ -0,0 +1,98 @@
+package ws
+
+import (
+ "context"
+ "log/slog"
+ "time"
+
+ "github.com/gorilla/websocket"
+)
+
+const (
+ writeWait = 10 * time.Second
+ pongWait = 60 * time.Second
+ pingPeriod = pongWait * 9 / 10
+
+ maxMessageSize = 1024
+)
+
+// Client represents a single WebSocket connection.
+type Client struct {
+ conn *websocket.Conn
+ send chan []byte
+}
+
+func NewClient(conn *websocket.Conn, sendBuffer int) *Client {
+ return &Client{
+ conn: conn,
+ send: make(chan []byte, sendBuffer),
+ }
+}
+
+// ServeClient registers the client with the hub and starts read/write pumps.
+// Caller is responsible for creating/closing the websocket.Conn.
+func ServeClient(ctx context.Context, hub *Hub, conn *websocket.Conn) {
+ c := NewClient(conn, 256)
+ hub.register <- c
+
+ go c.writePump(ctx, hub)
+ go c.readPump(ctx, hub)
+}
+
+func (c *Client) readPump(ctx context.Context, hub *Hub) {
+ // Ensure client is removed from hub without sending on a potentially
+ // unserviced channel. Use hub.remove which is safe when the hub has exited.
+ defer hub.remove(c)
+
+ c.conn.SetReadLimit(maxMessageSize)
+ _ = c.conn.SetReadDeadline(time.Now().Add(pongWait))
+ c.conn.SetPongHandler(func(string) error {
+ _ = c.conn.SetReadDeadline(time.Now().Add(pongWait))
+ return nil
+ })
+
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ default:
+ // We ignore application messages; reads are only for control frames (close/pong).
+ if _, _, err := c.conn.ReadMessage(); err != nil {
+ slog.Debug("websocket readPump end", "err", err)
+ return
+ }
+ }
+ }
+}
+
+func (c *Client) writePump(ctx context.Context, hub *Hub) {
+ ticker := time.NewTicker(pingPeriod)
+ // Stop ticker and ensure client is removed from hub without writing to hub.unregister.
+ defer func() {
+ ticker.Stop()
+ hub.remove(c)
+ }()
+
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case msg, ok := <-c.send:
+ _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
+ if !ok {
+ // Hub closed the channel.
+ _ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
+ return
+ }
+ if err := c.conn.WriteMessage(websocket.TextMessage, msg); err != nil {
+ slog.Debug("websocket write error", "err", err)
+ return
+ }
+ case <-ticker.C:
+ _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
+ if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
+ return
+ }
+ }
+ }
+}
diff --git a/backend/internal/utils/ws/hub.go b/backend/internal/utils/ws/hub.go
new file mode 100644
index 000000000..d187e7984
--- /dev/null
+++ b/backend/internal/utils/ws/hub.go
@@ -0,0 +1,80 @@
+package ws
+
+import (
+ "context"
+ "log/slog"
+ "sync"
+)
+
+type Hub struct {
+ mu sync.RWMutex
+ clients map[*Client]struct{}
+ register chan *Client
+ unregister chan *Client
+ broadcast chan []byte
+}
+
+func NewHub(buffer int) *Hub {
+ return &Hub{
+ clients: make(map[*Client]struct{}),
+ register: make(chan *Client),
+ unregister: make(chan *Client),
+ broadcast: make(chan []byte, buffer),
+ }
+}
+
+func (h *Hub) Run(ctx context.Context) {
+ for {
+ select {
+ case <-ctx.Done():
+ h.closeAll()
+ return
+ case c := <-h.register:
+ h.mu.Lock()
+ h.clients[c] = struct{}{}
+ h.mu.Unlock()
+ case c := <-h.unregister:
+ h.remove(c)
+ case msg := <-h.broadcast:
+ h.mu.RLock()
+ for c := range h.clients {
+ select {
+ case c.send <- msg:
+ default:
+ // backpressure: drop slow client
+ go h.remove(c)
+ }
+ }
+ h.mu.RUnlock()
+ }
+ }
+}
+
+func (h *Hub) Broadcast(msg []byte) {
+ select {
+ case h.broadcast <- msg:
+ default:
+ // prevent global stall if hub buffer fills
+ slog.Warn("websocket hub broadcast buffer full; dropping message")
+ }
+}
+
+func (h *Hub) remove(c *Client) {
+ h.mu.Lock()
+ if _, ok := h.clients[c]; ok {
+ delete(h.clients, c)
+ close(c.send)
+ _ = c.conn.Close()
+ }
+ h.mu.Unlock()
+}
+
+func (h *Hub) closeAll() {
+ h.mu.Lock()
+ for c := range h.clients {
+ close(c.send)
+ _ = c.conn.Close()
+ delete(h.clients, c)
+ }
+ h.mu.Unlock()
+}
diff --git a/backend/internal/utils/ws/proxy.go b/backend/internal/utils/ws/proxy.go
new file mode 100644
index 000000000..a5566af18
--- /dev/null
+++ b/backend/internal/utils/ws/proxy.go
@@ -0,0 +1,71 @@
+package ws
+
+import (
+ "io"
+ "net/http"
+ "time"
+
+ "github.com/gorilla/websocket"
+)
+
+var upgrader = websocket.Upgrader{
+ CheckOrigin: func(r *http.Request) bool { return true },
+}
+
+// ProxyHTTP upgrades the incoming client connection and bridges it to remoteWS.
+func ProxyHTTP(w http.ResponseWriter, r *http.Request, remoteWS string, header http.Header) error {
+ clientConn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ return err
+ }
+ defer clientConn.Close()
+
+ dialer := websocket.Dialer{
+ Proxy: http.ProxyFromEnvironment,
+ HandshakeTimeout: 45 * time.Second,
+ }
+ remoteConn, resp, err := dialer.Dial(remoteWS, header)
+ // Ensure the response body is drained & closed to avoid leaking resources.
+ if resp != nil {
+ _, _ = io.Copy(io.Discard, resp.Body)
+ _ = resp.Body.Close()
+ }
+ if err != nil {
+ _ = clientConn.WriteMessage(websocket.CloseMessage, []byte{})
+ return err
+ }
+ defer remoteConn.Close()
+
+ errc := make(chan struct{}, 2)
+
+ // client -> remote
+ go func() {
+ defer func() { errc <- struct{}{} }()
+ for {
+ mt, msg, err := clientConn.ReadMessage()
+ if err != nil {
+ return
+ }
+ if err := remoteConn.WriteMessage(mt, msg); err != nil {
+ return
+ }
+ }
+ }()
+
+ // remote -> client
+ go func() {
+ defer func() { errc <- struct{}{} }()
+ for {
+ mt, msg, err := remoteConn.ReadMessage()
+ if err != nil {
+ return
+ }
+ if err := clientConn.WriteMessage(mt, msg); err != nil {
+ return
+ }
+ }
+ }()
+
+ <-errc
+ return nil
+}
diff --git a/frontend/src/lib/components/log-viewer.svelte b/frontend/src/lib/components/log-viewer.svelte
index 272b40f39..e9a45fd45 100644
--- a/frontend/src/lib/components/log-viewer.svelte
+++ b/frontend/src/lib/components/log-viewer.svelte
@@ -46,6 +46,12 @@
let isStreaming = $state(false);
let error: string | null = $state(null);
let eventSource: EventSource | null = null;
+ let ws: WebSocket | null = null;
+ let currentStreamKey: string | null = null;
+ function streamKey() {
+ return type === 'stack' ? (stackId ? `stack:${stackId}` : null) : containerId ? `ctr:${containerId}` : null;
+ }
+
const humanType = type === 'stack' ? m.common_stack() : m.common_container();
const DOCKER_TS_ISO_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z?\s*/;
@@ -66,6 +72,21 @@
return `${baseEndpoint}?follow=true&tail=100&timestamps=${showTimestamps}`;
}
+ function buildWebSocketEndpoint(path: string): string {
+ const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
+ return `${protocol}://${window.location.host}${path}`;
+ }
+
+ async function buildLogWsEndpoint(): Promise<string> {
+ const currentEnv = get(environmentStore.selected);
+ const envId = currentEnv?.id || 'local';
+ const basePath =
+ type === 'stack'
+ ? `/api/environments/${envId}/stacks/${stackId}/logs/ws`
+ : `/api/environments/${envId}/containers/${containerId}/logs/ws`;
+ return buildWebSocketEndpoint(`${basePath}?follow=true&tail=100&timestamps=${showTimestamps}`);
+ }
+
export async function startLogStream() {
const targetId = type === 'stack' ? stackId : containerId;
@@ -75,92 +96,67 @@
isStreaming = true;
error = null;
onStart?.();
+ await startWebSocketStream();
+ return;
+ } catch (err) {
+ console.error('Failed to start log stream:', err);
+ error = m.log_stream_failed_connect({ type: humanType });
+ isStreaming = false;
+ }
+ }
- const endpoint = await buildLogStreamEndpoint();
-
- eventSource = new EventSource(endpoint);
-
- eventSource.addEventListener('log', (event) => {
- try {
- const logData = JSON.parse(event.data);
-
- if (logData.message !== undefined) {
- addLogEntry({
- level: logData.level || 'info',
- message: logData.message,
- timestamp: logData.timestamp || new Date().toISOString(),
- service: logData.service,
- containerId: logData.containerId
- });
- } else if (logData.data !== undefined) {
- addLogEntry({
- level: logData.data.includes('[STDERR]') ? 'stderr' : 'stdout',
- message: logData.data.replace('[STDERR] ', ''),
- timestamp: logData.timestamp || new Date().toISOString(),
- service: logData.service,
- containerId: logData.containerId
- });
- }
- } catch (parseError) {
- console.error('Failed to parse log event data:', parseError, 'Raw data:', event.data);
- addLogEntry({
- level: 'info',
- message: event.data,
- timestamp: new Date().toISOString()
- });
- }
- });
+ async function startWebSocketStream() {
+ const url = await buildLogWsEndpoint();
+ ws = new WebSocket(url);
- eventSource.onmessage = (event) => {
- try {
- const logData = JSON.parse(event.data);
-
- if (logData.data) {
- addLogEntry({
- level: logData.data.includes('[STDERR]') ? 'stderr' : 'stdout',
- message: logData.data.replace('[STDERR] ', ''),
- timestamp: logData.timestamp || new Date().toISOString(),
- service: logData.service,
- containerId: logData.containerId
- });
- } else {
- addLogEntry({
- level: logData.level || 'info',
- message: logData.message || logData.data || event.data,
- timestamp: logData.timestamp || new Date().toISOString(),
- service: logData.service,
- containerId: logData.containerId
- });
- }
- } catch (parseError) {
- console.error('Failed to parse log data:', parseError, 'Raw data:', event.data);
- addLogEntry({
- level: 'info',
- message: event.data,
- timestamp: new Date().toISOString()
- });
- }
- };
+ ws.onopen = () => {
+ if (dev) console.log(m.log_viewer_connected({ type: humanType }));
+ error = null;
+ isStreaming = true;
+ };
- eventSource.onopen = () => {
- if (dev) console.log(m.log_viewer_connected({ type: humanType }));
- error = null;
- };
+ ws.onmessage = (evt) => {
+ const data = typeof evt.data === 'string' ? evt.data : '';
+ if (!data) return;
+ for (const line of data.split('\n')) {
+ if (!line.trim()) continue;
+ handleIncomingLine(line);
+ }
+ };
- eventSource.onerror = (event) => {
- console.error('Log stream error:', event);
- error = m.log_stream_connection_lost({ type: humanType });
- isStreaming = false;
+ ws.onerror = (evt) => {
+ console.error('WebSocket log stream error:', evt);
+ error = m.log_stream_connection_lost({ type: humanType });
+ };
- if (eventSource?.readyState === EventSource.CLOSED) {
- error = m.log_stream_closed_by_server({ type: humanType });
- }
- };
- } catch (err) {
- console.error('Failed to start log stream:', err);
- error = m.log_stream_failed_connect({ type: humanType });
+ ws.onclose = () => {
isStreaming = false;
+ if (!error) {
+ error = m.log_stream_closed_by_server({ type: humanType });
+ }
+ };
+ }
+
+ function handleIncomingLine(raw: string) {
+ let level: LogEntry['level'] = raw.startsWith('[STDERR] ') ? 'stderr' : 'stdout';
+ let line = raw.replace('[STDERR] ', '');
+
+ // Try to extract "service | message" if present
+ let service: string | undefined;
+ if (line.includes(' | ')) {
+ const parts = line.split(' | ', 2);
+ if (parts.length === 2) {
+ service = parts[0].trim();
+ line = parts[1];
+ }
}
+
+ addLogEntry({
+ level,
+ message: line,
+ timestamp: new Date().toISOString(),
+ service
+ });
}
export function stopLogStream() {
@@ -169,6 +165,18 @@
eventSource.close();
eventSource = null;
}
+ if (ws) {
+ try {
+ ws.onerror = null;
+ ws.onclose = null;
+ if (ws.readyState === WebSocket.CONNECTING) {
+ ws.addEventListener('open', () => ws?.close(), { once: true });
+ } else {
+ ws.close();
+ }
+ } catch {}
+ ws = null;
+ }
isStreaming = false;
onStop?.();
}
@@ -239,24 +247,15 @@
}
}
- onMount(() => {
- const targetId = type === 'stack' ? stackId : containerId;
- if (targetId) {
- startLogStream();
- }
- });
-
- onDestroy(() => {
- stopLogStream();
- });
-
$effect(() => {
- const targetId = type === 'stack' ? stackId : containerId;
- if (targetId && browser) {
- stopLogStream();
- logs = [];
- startLogStream();
- }
+ if (!browser) return;
+ const key = streamKey();
+ if (!key) return;
+ if (key === currentStreamKey && isStreaming) return;
+ if (currentStreamKey) stopLogStream();
+ logs = [];
+ currentStreamKey = key;
+ startLogStream();
});
</script>
@@ -317,8 +316,8 @@
</div>
</div>
-<style>
+<!-- <style>
.log-viewer {
font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
}
-</style>
+</style> -->
diff --git a/frontend/src/lib/services/api/environment-api-service.ts b/frontend/src/lib/services/api/environment-api-service.ts
index 3c3886dd2..e7f7082d7 100644
--- a/frontend/src/lib/services/api/environment-api-service.ts
+++ b/frontend/src/lib/services/api/environment-api-service.ts
@@ -273,22 +273,6 @@ export class EnvironmentAPIService extends BaseAPIService {
return this.handleResponse(this.api.post(`/environments/${envId}/stacks/${projectId}/restart`));
}
- async getProjectLogs(projectId: string): Promise<string> {
- const envId = await this.getCurrentEnvironmentId();
- const response = await this.handleResponse<{ logs?: string }>(
- this.api.get(`/environments/${envId}/stacks/${projectId}/logs`)
- );
- return response.logs || '';
- }
-
- async deployProjectWithOptions(
- projectName: string,
- options?: { profiles?: string[]; envOverrides?: Record<string, string> }
- ): Promise<Project> {
- const envId = await this.getCurrentEnvironmentId();
- return this.handleResponse(this.api.post(`/environments/${envId}/stacks/${projectName}/deploy`, options || {}));
- }
-
async downProject(projectName: string): Promise<Project> {
const envId = await this.getCurrentEnvironmentId();
return this.handleResponse(this.api.post(`/environments/${envId}/stacks/${projectName}/down`));
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index 0f67fdf29..d9990e0f1 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -21,7 +21,9 @@ export default defineConfig({
host: process.env.HOST,
proxy: {
'/api': {
- target: process.env.DEV_BACKEND_URL || 'http://localhost:3552'
+ target: process.env.DEV_BACKEND_URL || 'http://localhost:3552',
+ changeOrigin: true,
+ ws: true
}
}
}