|
| 1 | +package containers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +// Status describes a single watched container's current image identity. |
| 14 | +type Status struct { |
| 15 | + // Name is the container name. |
| 16 | + Name string `json:"name"` |
| 17 | + // Image is the image reference with tag (e.g. ethpandaops/lighthouse:latest). |
| 18 | + Image string `json:"image"` |
| 19 | + // ImageID is the local image config ID (sha256:...). |
| 20 | + ImageID string `json:"image_id"` |
| 21 | + // Digest is the registry manifest digest the image was pulled from |
| 22 | + // (sha256:...), derived from the image's RepoDigests. It is directly |
| 23 | + // comparable to a registry's Docker-Content-Digest. Empty for locally-built |
| 24 | + // images with no registry reference. |
| 25 | + Digest string `json:"digest"` |
| 26 | +} |
| 27 | + |
| 28 | +// ListFunc returns the current status of all watched containers. |
| 29 | +type ListFunc func(ctx context.Context) ([]Status, error) |
| 30 | + |
| 31 | +// Handler serves the /v1/containers endpoint. |
| 32 | +// |
| 33 | +// It holds the list function and endpoint path for the read-only |
| 34 | +// /v1/containers endpoint. |
| 35 | +type Handler struct { |
| 36 | + list ListFunc // Container status lookup function. |
| 37 | + Path string // API endpoint path (e.g., "/v1/containers"). |
| 38 | +} |
| 39 | + |
| 40 | +// New creates a new containers Handler backed by the given list function. |
| 41 | +// |
| 42 | +// Parameters: |
| 43 | +// - list: Function returning the current status of all watched containers. |
| 44 | +// |
| 45 | +// Returns: |
| 46 | +// - *Handler: Initialized handler serving /v1/containers. |
| 47 | +func New(list ListFunc) *Handler { |
| 48 | + return &Handler{ |
| 49 | + list: list, |
| 50 | + Path: "/v1/containers", |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Handle responds with the JSON status of every watched container. |
| 55 | +// |
| 56 | +// Parameters: |
| 57 | +// - w: HTTP response writer for sending the JSON payload or error status. |
| 58 | +// - r: HTTP request; its context is propagated to the Docker calls. |
| 59 | +func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) { |
| 60 | + logrus.WithFields(logrus.Fields{ |
| 61 | + "method": r.Method, |
| 62 | + "path": r.URL.Path, |
| 63 | + }).Debug("Received HTTP API containers request") |
| 64 | + |
| 65 | + statuses, err := h.list(r.Context()) |
| 66 | + if err != nil { |
| 67 | + logrus.WithError(err).Error("Failed to list containers for API") |
| 68 | + http.Error(w, "failed to list containers", http.StatusInternalServerError) |
| 69 | + |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + response := map[string]any{ |
| 74 | + "containers": statuses, |
| 75 | + "count": len(statuses), |
| 76 | + "timestamp": time.Now().UTC().Format(time.RFC3339), |
| 77 | + "api_version": "v1", |
| 78 | + } |
| 79 | + |
| 80 | + var buf bytes.Buffer |
| 81 | + |
| 82 | + err = json.NewEncoder(&buf).Encode(response) |
| 83 | + if err != nil { |
| 84 | + logrus.WithError(err).Error("Failed to encode containers response") |
| 85 | + http.Error(w, "failed to encode response", http.StatusInternalServerError) |
| 86 | + |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + w.Header().Set("Content-Type", "application/json") |
| 91 | + w.WriteHeader(http.StatusOK) |
| 92 | + |
| 93 | + _, err = w.Write(buf.Bytes()) |
| 94 | + if err != nil { |
| 95 | + logrus.WithError(err).Error("Failed to write containers response") |
| 96 | + } |
| 97 | +} |
0 commit comments