mirror of
https://github.com/hanzoai/status.git
synced 2026-10-30 18:21:48 +00:00
zip (a fiber v3 fork) is the fleet-wide Go web framework. Handlers are now func(c *zip.Ctx) error; routes, groups and middleware move to the zip surface. The published API is byte-for-byte unchanged. All 20 declared routes keep their method, path, status codes and response bodies; the route table was dumped from both trees and diffed to prove it, and is now pinned by declaredRoutes in api/routes_test.go so no future refactor can move it silently. Three deltas the framework bump would otherwise have shipped, and how each is held: - fiber v3's c.JSON() tags bodies "application/json; charset=utf-8"; v2 sent bare "application/json". api.writeJSON restores it — one helper, six sites. - fiber v2 listed auto-HEAD mirrors as routes; v3 generates them lazily at first serve. HEAD still answers 200 on every GET route, pinned by a new test. - zip.Config exposes no Immutable knob, so request-scoped strings are no longer copied on read. Audited every param/query/header/cookie read: the only value that outlives its handler is the external-endpoint error string, which lands in the store, so it is now explicitly cloned. Every other key flows into a map lookup or a SQL arg and is never retained. Bridges rather than rewrites, so nothing is reimplemented badly: - zipx.Wrap is the single fiber.Handler -> zip.Handler adapter, used for the cors/recover/compress/redirect/static middleware and for basicauth. - zip.AdaptNetHTTP fronts the Prometheus metrics handler; AdaptNetHTTPFunc the OIDC callback; AdaptNetHTTPMiddleware the g8 session gate. - security.Router is the narrow slice of zip's router surface that package needs, so *zip.App and a Group both satisfy it. - controller.Handle listens via app.Fiber().Listen(addr, fiber.ListenConfig) — TLS as a value in one listen path, because zip.Listen carries no TLS or dual-stack knob. This is the only escape from the zip surface. Route-level coverage added where there was none: the pinned route table, a walk asserting every route dispatches to a handler, HEAD mirroring, and the 404 tail of the static fallback chain. client, config/endpoint and metrics fail identically before and after this change (network-dependent tests and a pre-existing metrics_test build break); verified by stashing and re-running. Co-authored-by: Hanzo Dev <[email protected]>
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
fiber "github.com/zap-proto/fiber/v3"
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
const (
|
|
// DefaultPage is the default page to use if none is specified or an invalid value is provided
|
|
DefaultPage = 1
|
|
|
|
// DefaultPageSize is the default page size to use if none is specified or an invalid value is provided
|
|
DefaultPageSize = 50
|
|
)
|
|
|
|
// writeJSON is the one way this API answers with JSON. zip.Ctx.JSON would tag
|
|
// the body "application/json; charset=utf-8"; every published response has
|
|
// always carried bare "application/json", and clients match on it exactly.
|
|
func writeJSON(c *zip.Ctx, statusCode int, value any) error {
|
|
c.Status(statusCode)
|
|
return c.Fiber().JSON(value, fiber.MIMEApplicationJSON)
|
|
}
|
|
|
|
func extractPageAndPageSizeFromRequest(c *zip.Ctx, maximumNumberOfResults int) (page, pageSize int) {
|
|
var err error
|
|
if pageParameter := c.Query("page"); len(pageParameter) == 0 {
|
|
page = DefaultPage
|
|
} else {
|
|
page, err = strconv.Atoi(pageParameter)
|
|
if err != nil {
|
|
page = DefaultPage
|
|
}
|
|
if page < 1 {
|
|
page = DefaultPage
|
|
}
|
|
}
|
|
if pageSizeParameter := c.Query("pageSize"); len(pageSizeParameter) == 0 {
|
|
pageSize = DefaultPageSize
|
|
} else {
|
|
pageSize, err = strconv.Atoi(pageSizeParameter)
|
|
if err != nil {
|
|
pageSize = DefaultPageSize
|
|
}
|
|
}
|
|
if page == 1 && pageSize > maximumNumberOfResults {
|
|
// If the page is 1 and the page size is greater than the maximum number of results, return
|
|
// no more than the maximum number of results
|
|
pageSize = maximumNumberOfResults
|
|
} else if pageSize < 1 {
|
|
pageSize = DefaultPageSize
|
|
}
|
|
return
|
|
}
|