0acbcda084
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.
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/alexei/tinyforge/internal/store"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
// listApps handles GET /api/apps.
|
|
func (s *Server) listApps(w http.ResponseWriter, r *http.Request) {
|
|
out, err := s.store.ListApps()
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "list apps")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
// getApp handles GET /api/apps/{id}.
|
|
func (s *Server) getApp(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
a, err := s.store.GetAppByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "app")
|
|
return
|
|
}
|
|
respondError(w, http.StatusInternalServerError, "get app")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, a)
|
|
}
|
|
|
|
// createApp handles POST /api/apps. Body: {"name": "...", "description": "..."}.
|
|
func (s *Server) createApp(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
req.Name = strings.TrimSpace(req.Name)
|
|
if req.Name == "" {
|
|
respondError(w, http.StatusBadRequest, "name is required")
|
|
return
|
|
}
|
|
|
|
a, err := s.store.CreateApp(store.App{
|
|
Name: req.Name, Description: req.Description,
|
|
})
|
|
if err != nil {
|
|
respondError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusCreated, a)
|
|
}
|
|
|
|
// updateApp handles PUT /api/apps/{id}.
|
|
func (s *Server) updateApp(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
if !decodeJSON(w, r, &req) {
|
|
return
|
|
}
|
|
a, err := s.store.GetAppByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "app")
|
|
return
|
|
}
|
|
respondError(w, http.StatusInternalServerError, "get app")
|
|
return
|
|
}
|
|
if name := strings.TrimSpace(req.Name); name != "" {
|
|
a.Name = name
|
|
}
|
|
a.Description = req.Description
|
|
if err := s.store.UpdateApp(a); err != nil {
|
|
respondError(w, http.StatusInternalServerError, "update app")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, a)
|
|
}
|
|
|
|
// deleteApp handles DELETE /api/apps/{id}. Workloads previously assigned to
|
|
// this app become unassigned (app_id cleared), they are NOT deleted.
|
|
func (s *Server) deleteApp(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
if err := s.store.DeleteApp(id); err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
respondNotFound(w, "app")
|
|
return
|
|
}
|
|
respondError(w, http.StatusInternalServerError, "delete app")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|