Files
nexarch/connect/internal/statusapi/statusapi.go
T
patrickandClaude Sonnet 5 e18b415ebb
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
Supply-Chain-Scan (npm) / npm-audit (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
API-02: statusapi/connect-app HTTP-Dienst und web/connect-status Oberfläche
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 09:33:34 +02:00

94 lines
3.1 KiB
Go

// Package statusapi is the HTTP binding for the connector status overview
// (API-02). User identity comes, transitionally until a central Core-JWT
// wiring exists for Connect (pattern from workflow/internal/approvalapi),
// from the X-User-Id header; the tenant comes from X-Tenant-Slug, set by
// the deployment's auth layer, never from an unauthenticated body/query
// value.
package statusapi
import (
"encoding/json"
"net/http"
"time"
"gitea.perlbach24.de/scripte/nexarch/connect/internal/syncjob"
)
type Server struct {
jobs *syncjob.Store
}
func NewServer(jobs *syncjob.Store) *Server {
return &Server{jobs: jobs}
}
func (s *Server) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /connectors/status", s.requireIdentity(s.handleList))
}
func (s *Server) requireIdentity(next func(w http.ResponseWriter, r *http.Request, tenantSlug string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-User-Id") == "" {
writeError(w, http.StatusUnauthorized, "X-User-Id fehlt")
return
}
tenant := r.Header.Get("X-Tenant-Slug")
if tenant == "" {
writeError(w, http.StatusUnauthorized, "X-Tenant-Slug fehlt")
return
}
next(w, r, tenant)
}
}
type connectorStatus struct {
JobID string `json:"job_id"`
ConnectorName string `json:"connector_name"`
ConnectorInst string `json:"connector_instance_id"`
IntervalSeconds int `json:"interval_seconds"`
NextRunAt time.Time `json:"next_run_at"`
LastRunID *string `json:"last_run_id,omitempty"`
LastRunStatus *string `json:"last_run_status,omitempty"`
LastRunTrigger *string `json:"last_run_trigger,omitempty"`
LastRunStarted *time.Time `json:"last_run_started_at,omitempty"`
LastRunFinished *time.Time `json:"last_run_finished_at,omitempty"`
LastError *string `json:"last_error,omitempty"`
}
func (s *Server) handleList(w http.ResponseWriter, r *http.Request, tenantSlug string) {
statuses, err := s.jobs.ListForTenant(r.Context(), tenantSlug)
if err != nil {
writeError(w, http.StatusInternalServerError, "verbindungsstatus konnte nicht gelesen werden")
return
}
out := make([]connectorStatus, 0, len(statuses))
for _, js := range statuses {
cs := connectorStatus{
JobID: js.Job.ID,
ConnectorName: js.Job.Ref.ConnectorName,
ConnectorInst: js.Job.Ref.ConnectorInstanceID,
IntervalSeconds: js.Job.IntervalSeconds,
NextRunAt: js.Job.NextRunAt,
}
if js.LastRun != nil {
id, status, trig := js.LastRun.ID, string(js.LastRun.Status), string(js.LastRun.Trigger)
cs.LastRunID, cs.LastRunStatus, cs.LastRunTrigger = &id, &status, &trig
cs.LastRunStarted = &js.LastRun.StartedAt
cs.LastRunFinished = js.LastRun.FinishedAt
cs.LastError = js.LastRun.LastError
}
out = append(out, cs)
}
writeJSON(w, http.StatusOK, out)
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func writeError(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, map[string]string{"error": msg})
}