Implement container logs handler

- Add Docker client to handlers for container operations
- Implement HandleAppLogs() to fetch and return container logs
- Support ?tail=N query parameter (default 500 lines)
- Handle missing container gracefully
This commit is contained in:
2025-12-29 15:48:23 +07:00
parent 3f9d83c436
commit daaf00893c
3 changed files with 36 additions and 7 deletions

View File

@@ -331,6 +331,9 @@ func (h *Handlers) HandleAppDeployments() http.HandlerFunc {
}
}
// defaultLogTail is the default number of log lines to fetch.
const defaultLogTail = "500"
// HandleAppLogs returns the container logs handler.
func (h *Handlers) HandleAppLogs() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
@@ -343,16 +346,37 @@ func (h *Handlers) HandleAppLogs() http.HandlerFunc {
return
}
// Container logs fetching not yet implemented
writer.Header().Set("Content-Type", "text/plain")
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
if !application.ContainerID.Valid {
_, _ = writer.Write([]byte("No container running"))
_, _ = writer.Write([]byte("No container running\n"))
return
}
_, _ = writer.Write([]byte("Container logs not implemented yet"))
tail := request.URL.Query().Get("tail")
if tail == "" {
tail = defaultLogTail
}
logs, logsErr := h.docker.ContainerLogs(
request.Context(),
application.ContainerID.String,
tail,
)
if logsErr != nil {
h.log.Error("failed to get container logs",
"error", logsErr,
"app", application.Name,
"container", application.ContainerID.String,
)
_, _ = writer.Write([]byte("Failed to fetch container logs\n"))
return
}
_, _ = writer.Write([]byte(logs))
}
}