Publish ITWorx Pulse source
Public source validation / validate (push) Failing after 3m8s

This commit is contained in:
ITWorx Pulse release export
2026-09-03 02:09:19 +02:00
commit bd774932d5
614 changed files with 77116 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
package applicationapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
"github.com/itworx/pulse/internal/application"
"github.com/itworx/pulse/internal/auth"
"github.com/itworx/pulse/internal/problem"
)
type Handler struct {
Provider interface {
Snapshot(context.Context) (application.Snapshot, error)
}
}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || (r.URL.Path != "/api/v1/applications" && !strings.HasPrefix(r.URL.Path, "/api/v1/applications/")) {
http.NotFound(w, r)
return
}
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read applications.", nil)
return
}
snapshot, err := h.snapshot(r)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return
}
problem.Write(w, r, http.StatusServiceUnavailable, "APPLICATIONS_UNAVAILABLE", "Applications not available", "Applicatiegegevens konden niet worden gelezen.", nil)
return
}
if r.URL.Path != "/api/v1/applications" {
id := strings.TrimPrefix(r.URL.Path, "/api/v1/applications/")
for _, item := range snapshot.Applications {
if item.ID == id {
writeJSON(w, struct {
Source application.Source `json:"source"`
Application application.Application `json:"application"`
}{Source: snapshot.Source, Application: item})
return
}
}
http.NotFound(w, r)
return
}
if len(snapshot.Applications) > 100 {
snapshot.Applications = snapshot.Applications[:100]
}
writeJSON(w, snapshot)
}
func (h Handler) snapshot(r *http.Request) (application.Snapshot, error) {
if h.Provider == nil {
return application.UnknownSnapshot(time.Now().UTC(), "applications", "agent", "source_unavailable"), nil
}
return h.Provider.Snapshot(r.Context())
}
func writeJSON(w http.ResponseWriter, value any) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "private, max-age=5")
_ = json.NewEncoder(w).Encode(value)
}
+46
View File
@@ -0,0 +1,46 @@
package applicationapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/itworx/pulse/internal/application"
"github.com/itworx/pulse/internal/auth"
)
type provider struct{ value application.Snapshot }
func (p provider) Snapshot(context.Context) (application.Snapshot, error) { return p.value, nil }
func request(method, path string) *http.Request {
r := httptest.NewRequest(method, path, nil)
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
}
func TestHandlerListDetailAndAuth(t *testing.T) {
snapshot := application.Snapshot{ContractVersion: application.ContractVersion, Applications: []application.Application{{ID: "app-1", Name: "Pulse", Status: application.StateHealthy}}}
handler := Handler{Provider: provider{value: snapshot}}
list := httptest.NewRecorder()
handler.ServeHTTP(list, request(http.MethodGet, "/api/v1/applications"))
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), "\"name\":\"Pulse\"") {
t.Fatalf("list status=%d body=%s", list.Code, list.Body.String())
}
detail := httptest.NewRecorder()
handler.ServeHTTP(detail, request(http.MethodGet, "/api/v1/applications/app-1"))
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), "\"id\":\"app-1\"") {
t.Fatalf("detail status=%d body=%s", detail.Code, detail.Body.String())
}
mutation := httptest.NewRecorder()
handler.ServeHTTP(mutation, request(http.MethodPost, "/api/v1/applications/app-1"))
if mutation.Code != http.StatusNotFound {
t.Fatalf("mutation status=%d", mutation.Code)
}
}
func TestHandlerRequiresAuthentication(t *testing.T) {
response := httptest.NewRecorder()
Handler{}.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/applications", nil))
if response.Code != http.StatusUnauthorized {
t.Fatalf("status=%d", response.Code)
}
}