Public source validation / validate (push) Failing after 3m8s
58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package reverseproxyapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/itworx/pulse/internal/auth"
|
|
"github.com/itworx/pulse/internal/problem"
|
|
"github.com/itworx/pulse/internal/reverseproxy"
|
|
)
|
|
|
|
type Handler struct {
|
|
Provider reverseproxy.Provider
|
|
}
|
|
|
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/reverse-proxy" {
|
|
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 reverse-proxy routes.", nil)
|
|
return
|
|
}
|
|
if err := r.Context().Err(); err != 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, "REVERSE_PROXY_UNAVAILABLE", "Reverse proxy not available", "De reverse-proxygegevens konden niet worden gelezen.", nil)
|
|
return
|
|
}
|
|
if len(snapshot.Routes) > 150 {
|
|
snapshot.Routes = snapshot.Routes[:150]
|
|
snapshot.Total = len(snapshot.Routes)
|
|
}
|
|
writeJSON(w, snapshot)
|
|
}
|
|
|
|
func (h Handler) snapshot(r *http.Request) (reverseproxy.Snapshot, error) {
|
|
if h.Provider == nil {
|
|
return reverseproxy.DisabledSnapshot(time.Now().UTC(), "reverse-proxy", "connector", "connector_disabled"), 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)
|
|
}
|