Greenfield Go multi-tenant IPFS Pinning Service wire-compatible with the
IPFS Pinning Services API spec. Paired 1:1 with Kubo over localhost RPC,
clustered via embedded NATS JetStream, Postgres source-of-truth with
RLS-enforced tenancy, Fiber + huma v2 for the HTTP surface, Authentik
OIDC for session login with kid-rotated HS256 JWT API tokens.
Feature-complete against the 22-milestone build plan, including the
ship-it v1.0 gap items:
* admin CLIs: drain/uncordon, maintenance, mint-token, rotate-key,
prune-denylist, rebalance --dry-run, cache-stats, cluster-presences
* TTL leader election via NATS KV, fence tokens, JetStream dedup
* rebalancer (plan/apply split), reconciler, requeue sweeper
* ristretto caches with NATS-backed cross-node invalidation
(placements live-nodes + token denylist)
* maintenance watchdog for stuck cluster-pause flag
* Prometheus /metrics with CIDR ACL, HTTP/pin/scheduler/cache gauges
* rate limiting: session (10/min) + anonymous global (120/min)
* integration tests: rebalance, refcount multi-org, RLS belt
* goreleaser (tar + deb/rpm/apk + Alpine Docker) targeting Gitea
Stack: Cobra/Viper, Fiber v2 + huma v2, embedded NATS JetStream,
pgx/sqlc/golang-migrate, ristretto, TypeID, prometheus/client_golang,
testcontainers-go.
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package wsserver
|
|
|
|
import (
|
|
"github.com/gofiber/contrib/websocket"
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"anchorage/internal/pkg/auth"
|
|
"anchorage/internal/pkg/ids"
|
|
)
|
|
|
|
// Register wires the Fiber route for pin status events onto app. This
|
|
// is a plain Fiber handler (not a huma operation) because huma v2
|
|
// doesn't model WebSocket upgrades; the OpenAPI doc references it via
|
|
// externalDocs.
|
|
//
|
|
// Path: GET /v1/events (upgrade)
|
|
//
|
|
// Query params:
|
|
//
|
|
// ?requestid=pin_... — subscribe to events for a single pin
|
|
// (omit) — subscribe to every pin in the authenticated org
|
|
//
|
|
// Auth: the `auth.BearerMiddleware` on the parent app must have run
|
|
// first so the request carries a ClientContext.
|
|
func Register(app *fiber.App, hub *Hub) {
|
|
// Upgrade guard: reject non-WebSocket traffic before we register
|
|
// the upgrade handler so regular probes get a clean 426.
|
|
app.Use("/v1/events", func(c *fiber.Ctx) error {
|
|
if websocket.IsWebSocketUpgrade(c) {
|
|
return c.Next()
|
|
}
|
|
return fiber.ErrUpgradeRequired
|
|
})
|
|
|
|
app.Get("/v1/events", websocket.New(func(c *websocket.Conn) {
|
|
defer c.Close()
|
|
|
|
// The pre-upgrade BearerMiddleware stashed the ClientContext on
|
|
// Fiber Locals; websocket.Conn exposes Locals but not UserContext.
|
|
cc := auth.FromLocals(func(k string) interface{} { return c.Locals(k) })
|
|
if cc == nil {
|
|
_ = c.WriteMessage(websocket.CloseMessage,
|
|
websocket.FormatCloseMessage(websocket.ClosePolicyViolation, "unauthenticated"))
|
|
return
|
|
}
|
|
|
|
var rid *ids.PinID
|
|
if q := c.Query("requestid"); q != "" {
|
|
parsed, err := ids.ParsePin(q)
|
|
if err != nil {
|
|
_ = c.WriteMessage(websocket.CloseMessage,
|
|
websocket.FormatCloseMessage(websocket.ClosePolicyViolation, "bad requestid"))
|
|
return
|
|
}
|
|
rid = &parsed
|
|
}
|
|
|
|
hub.Register(c, cc.Org, rid)
|
|
defer hub.Unregister(c)
|
|
|
|
// Keep the socket alive; drop on client close / error.
|
|
for {
|
|
if _, _, err := c.ReadMessage(); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}))
|
|
}
|