anchorage v1.0 initial tree
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.
This commit is contained in:
91
internal/pkg/httpserver/server.go
Normal file
91
internal/pkg/httpserver/server.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Package httpserver composes the Fiber app and its middleware stack.
|
||||
//
|
||||
// The actual route tree is registered by internal/pkg/openapi through the
|
||||
// humafiber adapter, so this package only owns lifecycle: configuration,
|
||||
// graceful start, graceful shutdown, and basic observability middleware.
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||
|
||||
"anchorage/internal/pkg/metrics"
|
||||
)
|
||||
|
||||
// Options configures a Server.
|
||||
type Options struct {
|
||||
Host string
|
||||
Port int
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
|
||||
// MetricsACLCIDRs is the allowlist for /metrics. Nil → use the
|
||||
// metrics package default (loopback + RFC1918). Explicit empty
|
||||
// slice → no restriction (firewall-only).
|
||||
MetricsACLCIDRs []string
|
||||
}
|
||||
|
||||
// Server wraps *fiber.App with lifecycle helpers.
|
||||
type Server struct {
|
||||
App *fiber.App
|
||||
opts Options
|
||||
}
|
||||
|
||||
// New constructs a Server with the standard anchorage middleware stack.
|
||||
//
|
||||
// The returned *fiber.App is exposed on Server.App so callers can register
|
||||
// routes before Start is called.
|
||||
func New(opts Options) *Server {
|
||||
app := fiber.New(fiber.Config{
|
||||
AppName: "anchorage",
|
||||
DisableStartupMessage: true,
|
||||
ReadTimeout: opts.ReadTimeout,
|
||||
WriteTimeout: opts.WriteTimeout,
|
||||
ErrorHandler: func(c *fiber.Ctx, err error) error {
|
||||
// Fiber default returns an HTML body; JSON is more useful
|
||||
// for our API clients.
|
||||
code := fiber.StatusInternalServerError
|
||||
if fe, ok := err.(*fiber.Error); ok {
|
||||
code = fe.Code
|
||||
}
|
||||
return c.Status(code).JSON(fiber.Map{
|
||||
"error": http.StatusText(code),
|
||||
"message": err.Error(),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
app.Use(recover.New())
|
||||
app.Use(requestid.New())
|
||||
app.Use(accessLog())
|
||||
|
||||
// /metrics lives at root (NOT under /v1) so Prometheus scrapers
|
||||
// find it at the conventional path. Gated by a CIDR ACL — see
|
||||
// metrics.ACL and MetricsConfig.AllowCIDRs.
|
||||
if acl, err := metrics.ACL(opts.MetricsACLCIDRs); err != nil {
|
||||
slog.Error("httpserver: bad metrics ACL; /metrics disabled", "err", err)
|
||||
} else {
|
||||
app.Get("/metrics", acl, metrics.Handler())
|
||||
}
|
||||
|
||||
return &Server{App: app, opts: opts}
|
||||
}
|
||||
|
||||
// Start binds and listens. Returns only on listener error.
|
||||
func (s *Server) Start(_ context.Context) error {
|
||||
addr := fmt.Sprintf("%s:%d", s.opts.Host, s.opts.Port)
|
||||
slog.Info("httpserver: listening", "addr", addr)
|
||||
return s.App.Listen(addr)
|
||||
}
|
||||
|
||||
// Shutdown drains in-flight requests with a bounded grace period.
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
return s.App.ShutdownWithContext(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user