feat: add server log endpoint and streaming

This commit is contained in:
2026-03-22 13:38:00 +01:00
parent 85c5874bc7
commit e637b5e649
10 changed files with 163 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import (
"bufio"
"context"
"io"
"strconv"
"sync"
"github.com/docker/docker/api/types"
@@ -72,3 +73,34 @@ func (s *DockerLogStreamer) Stop(serverID uuid.UUID) {
v.(context.CancelFunc)()
}
}
func (s *DockerLogStreamer) GetLastLines(ctx context.Context, server *model.Server, n int) ([]string, error) {
if n <= 0 || server.ContainerID == "" {
return []string{}, nil
}
rc, err := s.client.ContainerLogs(ctx, server.ContainerID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Tail: strconv.Itoa(n),
})
if err != nil {
return nil, err
}
defer rc.Close()
pr, pw := io.Pipe()
go func() {
defer pw.Close()
if _, err := stdcopy.StdCopy(pw, pw, rc); err != nil && err != io.EOF {
logging.Warn("DockerLogStreamer.GetLastLines: stdcopy error for %s: %v", server.ServiceName, err)
}
}()
var lines []string
scanner := bufio.NewScanner(pr)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}