Skip to content

Commit a309267

Browse files
authored
Fix issue with log endpoints (#871)
The way the log endpoint was mounted previously was causing the delete and get endpoints to return 404s.
1 parent 3c2a58e commit a309267

File tree

2 files changed

+36
-66
lines changed

2 files changed

+36
-66
lines changed

pkg/api/v1/logs.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/api/v1/workloads.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type WorkloadRoutes struct {
2727
// @title ToolHive API
2828
// @version 1.0
2929
// @description This is the ToolHive API workload.
30-
// @workloads [ { "url": "http://localhost:8080/api/v1" } ]
31-
// @basePath /api/v1
30+
// @workloads [ { "url": "http://localhost:8080/api/v1beta" } ]
31+
// @basePath /api/v1beta
3232

3333
// WorkloadRouter creates a new WorkloadRoutes instance.
3434
func WorkloadRouter(
@@ -51,10 +51,9 @@ func WorkloadRouter(
5151
r.Get("/{name}", routes.getWorkload)
5252
r.Post("/{name}/stop", routes.stopWorkload)
5353
r.Post("/{name}/restart", routes.restartWorkload)
54+
r.Get("/{name}/logs", routes.getLogsForWorkload)
5455
r.Delete("/{name}", routes.deleteWorkload)
5556

56-
// Mount the logs sub-router
57-
r.Mount("/{name}", LogsRouter(manager))
5857
return r
5958
}
6059

@@ -414,6 +413,39 @@ func (s *WorkloadRoutes) deleteWorkloadsBulk(w http.ResponseWriter, r *http.Requ
414413
w.WriteHeader(http.StatusAccepted)
415414
}
416415

416+
// getLogsForWorkload
417+
//
418+
// @Summary Get logs for a specific workload
419+
// @Description Retrieve at most 100 lines of logs for a specific workload by name.
420+
// @Tags logs
421+
// @Produce text/plain
422+
// @Param name path string true "Workload name"
423+
// @Success 200 {string} string "Logs for the specified workload"
424+
// @Failure 404 {string} string "Not Found"
425+
// @Router /api/v1beta/workloads/{name}/logs [get]
426+
func (s *WorkloadRoutes) getLogsForWorkload(w http.ResponseWriter, r *http.Request) {
427+
ctx := r.Context()
428+
name := chi.URLParam(r, "name")
429+
430+
logs, err := s.manager.GetLogs(ctx, name, false)
431+
if err != nil {
432+
if errors.Is(err, workloads.ErrContainerNotFound) {
433+
http.Error(w, "Workload not found", http.StatusNotFound)
434+
return
435+
}
436+
logger.Errorf("Failed to get logs: %v", err)
437+
http.Error(w, "Failed to get logs", http.StatusInternalServerError)
438+
return
439+
}
440+
w.Header().Set("Content-Type", "text/plain")
441+
_, err = w.Write([]byte(logs))
442+
if err != nil {
443+
logger.Errorf("Failed to write logs response: %v", err)
444+
http.Error(w, "Failed to write logs response", http.StatusInternalServerError)
445+
return
446+
}
447+
}
448+
417449
// Response type definitions.
418450

419451
// workloadListResponse represents the response for listing workloads

0 commit comments

Comments
 (0)