Files
tiny-forge/internal/api/workloads.go
T
alexei.dolgolyov 0acbcda084 feat(workload): /api/workloads /api/containers /api/apps endpoints
Adds the read API surface that the global Containers view (and
the per-workload container panel on project/stack/site detail
pages) consume.

- GET /api/workloads (?kind=)              → workload list
- GET /api/workloads/{id}                  → single workload
- GET /api/workloads/{id}/containers       → workload's containers
- PATCH /api/workloads/{id}/app            → assign/clear app_id (admin)

- GET /api/containers (?workload_id=&kind=&state=&app_id=)
                                           → global index, decorated
                                             with workload + app name
                                             so the table renders
                                             without N+1 fetches
- GET /api/containers/{id}                 → single container row

- GET  /api/apps                           → list
- GET  /api/apps/{id}                      → single
- POST /api/apps                           → create   (admin)
- PUT  /api/apps/{id}                      → update   (admin)
- DELETE /api/apps/{id}                    → delete   (admin) — clears
                                             app_id on owning workloads
                                             but leaves them assigned-to-none

Mutations on projects/stacks/sites still go through the existing
kind-specific endpoints; the new surface is read-only at the
workload layer.
2026-05-09 13:52:31 +03:00

81 lines
2.4 KiB
Go

package api
import (
"errors"
"net/http"
"github.com/alexei/tinyforge/internal/store"
"github.com/go-chi/chi/v5"
)
// listWorkloads handles GET /api/workloads. Optional ?kind=project|stack|site
// filter narrows the result. The shape mirrors the projects/stacks/sites
// listing endpoints — clients use this to render the global Workloads view.
func (s *Server) listWorkloads(w http.ResponseWriter, r *http.Request) {
kind := store.WorkloadKind(r.URL.Query().Get("kind"))
out, err := s.store.ListWorkloads(kind)
if err != nil {
respondError(w, http.StatusInternalServerError, "list workloads")
return
}
respondJSON(w, http.StatusOK, out)
}
// getWorkload handles GET /api/workloads/{id}.
func (s *Server) getWorkload(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
wl, err := s.store.GetWorkloadByID(id)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
respondNotFound(w, "workload")
return
}
respondError(w, http.StatusInternalServerError, "get workload")
return
}
respondJSON(w, http.StatusOK, wl)
}
// listWorkloadContainers handles GET /api/workloads/{id}/containers.
// Returns every Container row owned by this workload, newest first. The
// frontend's <WorkloadContainers> component uses this on every kind-specific
// detail page (project, stack, site) so the table shape is uniform.
func (s *Server) listWorkloadContainers(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
out, err := s.store.ListContainersByWorkload(id)
if err != nil {
respondError(w, http.StatusInternalServerError, "list workload containers")
return
}
respondJSON(w, http.StatusOK, out)
}
// updateWorkloadAppID handles PATCH /api/workloads/{id}/app. Body: {"app_id": "..."}.
// Empty string clears the app assignment. Used by the (optional) Apps UI.
func (s *Server) updateWorkloadAppID(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
var req struct {
AppID string `json:"app_id"`
}
if !decodeJSON(w, r, &req) {
return
}
wl, err := s.store.GetWorkloadByID(id)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
respondNotFound(w, "workload")
return
}
respondError(w, http.StatusInternalServerError, "get workload")
return
}
wl.AppID = req.AppID
if err := s.store.UpdateWorkload(wl); err != nil {
respondError(w, http.StatusInternalServerError, "update workload")
return
}
respondJSON(w, http.StatusOK, wl)
}