Files
homelab-frontend/internal/server/router.go
Admin Bot 2171424220
CI / CI (pull_request) Failing after 21m42s
feat(webhook): Forgejo → Gotify notification bridge
POST /v1/webhooks/forgejo receives Forgejo payloads, verifies HMAC
SHA256 signature, formats event into Gotify push notification.

Supported events: push, pull_request, issues, issue_comment,
pull_request_review_comment, release.

Env vars: GOTIFY_URL, GOTIFY_APP_TOKEN, FORGEJO_WEBHOOK_SECRET
Secret: gotify-webhook-secret in api namespace
2026-09-16 07:49:15 +09:00

81 lines
2.6 KiB
Go

package server
import (
"net/http"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/serviceadapter"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/webhook"
)
// Router implements an HTTP handler that routes health endpoints,
// ServiceAdapter X-Service requests, Temporal workflow endpoints,
// and other requests to upstream handlers.
type Router struct {
healthChecker *HealthChecker
dispatcher *serviceadapter.Dispatcher
temporalHandler http.Handler
upstreamHandler http.Handler
forgejoWebhook *webhook.ForgejoHandler
}
// NewRouter creates a new router with health endpoints.
// Health endpoints (/healthz and /readyz) are handled locally.
// X-Service requests are dispatched via ServiceAdapter CRD.
// Temporal endpoints (/workflow*) are routed to temporalHandler.
// All other paths are passed to the upstream handler.
func NewRouter(healthChecker *HealthChecker, dispatcher *serviceadapter.Dispatcher, temporalHandler http.Handler, upstreamHandler http.Handler) *Router {
return &Router{
healthChecker: healthChecker,
dispatcher: dispatcher,
temporalHandler: temporalHandler,
upstreamHandler: upstreamHandler,
forgejoWebhook: webhook.NewForgejoHandler(),
}
}
// ServeHTTP implements http.Handler.
// Priority order:
// 1. /healthz and /readyz to health handlers
// 2. X-Service header to ServiceAdapter dispatcher (phase 8) - PREFERRED routing method
// 3. /workflow* to temporal handler - DEPRECATED: use X-Service: workflow instead
// 4. All other paths to upstream handler (phase 0-7)
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Health endpoints first
switch req.URL.Path {
case "/healthz":
LivenessHandler(r.healthChecker)(w, req)
return
case "/readyz":
ReadinessHandler(r.healthChecker)(w, req)
return
}
// X-Service (ServiceAdapter) routing - checked before path-based routing
// PREFERRED: All service routing should use X-Service header pattern for consistency,
// auth enforcement, and resource-based access control.
if req.Header.Get("X-Service") != "" {
if r.dispatcher != nil {
r.dispatcher.Dispatch(w, req)
return
}
}
// Forgejo webhook receiver — no auth, HMAC-verified by handler
if req.URL.Path == "/v1/webhooks/forgejo" {
r.forgejoWebhook.ServeHTTP(w, req)
return
}
// Workflow endpoints
// DEPRECATED: Path-based /workflow routing is legacy.
// New clients should use X-Service: workflow header instead for consistent auth.
switch req.URL.Path {
case "/workflow", "/workflow/health", "/workflow/metrics":
r.temporalHandler.ServeHTTP(w, req)
return
}
// Default: upstream handler (all other paths)
r.upstreamHandler.ServeHTTP(w, req)
}