mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
`Mount` meant two unrelated things across the fleet at once: zip/remote.go func Mount(prefix, addr string, decl ...Declaration) (*App, error) 112 subsystems func Mount(app cloud.Router, deps cloud.Deps) error The second is the fleet's registration contract, stated in hanzoai/cloud's package doc and checked by cloud.MountFunc. The first is a leftover: Mount was one of five composition verbs (Listen/Mount/Add/Graft/Use), the other four were dissolved into Use, and this one survived by being the only one that pointed at another process. A reader seeing `Mount` in a diff could not tell which was meant without counting arguments. Renamed to Proxy, which is what it BUILDS. Every route it registers runs the one handler this file calls `proxy`, whose whole body is forward() to the address given; "proxy" is the term of art for a local stand-in whose handlers hand the request to someone else, so it needs no gloss. It is also a noun, which a function returning a value should be — composition is Use's job now, and the doc has said so since the four verbs were dissolved. NOT Remote: [Remote] already exists here and is the CALLER's side, a client executing commands against a service elsewhere. This is the SERVER's side, a definition standing in for that service inside this program's routing table. Two sides of one wire are two things. Also renamed the tests that named it, and mount_test.go -> adapt_net_http_test.go with it: those pin AdaptNetHTTP, a THIRD sense of "mount", and TestMount_* named none of the three. Regression set empty: TestServePathAllocsAreChainDepthInvariant fails identically on the parent commit (an allocation bound, untouched here). Co-authored-by: Hanzo Dev <[email protected]>
61 lines
2.5 KiB
Go
61 lines
2.5 KiB
Go
// Adapters for bringing existing net/http code onto zip
|
|
// without rewriting handlers. These are MIGRATION TOOLS — each adapter
|
|
// costs ~5% perf vs native Fiber dispatch and exists so a service can
|
|
// roll onto zip incrementally. Replace adapted routes with native
|
|
// zip handlers when feasible.
|
|
|
|
package zip
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/zap-proto/fiber/v3/middleware/adaptor"
|
|
)
|
|
|
|
// AdaptNetHTTP wraps an http.Handler so it can be served on a zip router as an
|
|
// ordinary zip.Handler. To front a whole foreign subtree, take a Group for the
|
|
// prefix and register a wildcard on it — this is THE way to bring net/http code
|
|
// onto zip:
|
|
//
|
|
// app.Group("/legacy").All("/*", zip.AdaptNetHTTP(httpHandler))
|
|
//
|
|
// Group is what carries the prefix; Proxy does not do this. Proxy delegates a
|
|
// prefix to a REMOTE address over a transport, so it takes a string, not a
|
|
// handler. A local handler is composed, not delegated.
|
|
//
|
|
// The wildcard stays an ordinary route, so most-specific-wins precedence still
|
|
// applies: a route registered on the group afterwards still beats it for its
|
|
// exact path.
|
|
//
|
|
// g := app.Group("/legacy")
|
|
// g.All("/*", zip.AdaptNetHTTP(httpHandler)) // everything else
|
|
// g.Get("/health", nativeHealth) // still wins
|
|
//
|
|
// A bare func is adapted through http.HandlerFunc, which is itself an
|
|
// http.Handler — there is no separate func-shaped adapter:
|
|
//
|
|
// app.Get("/legacy", zip.AdaptNetHTTP(http.HandlerFunc(myFunc)))
|
|
//
|
|
// Migration tool — costs ~5% perf vs native Fiber. Replace with native
|
|
// zip handlers when feasible.
|
|
func AdaptNetHTTP(h http.Handler) Handler {
|
|
wrapped := adaptor.HTTPHandler(h)
|
|
// TERMINAL: an http.Handler writes the response, so this answers whatever
|
|
// address it is registered at. In Use position it would answer every one of
|
|
// them, which is why the wildcard route above is THE spelling and this is
|
|
// refused at build time — see [Terminal].
|
|
return Terminal("zip.AdaptNetHTTP", func(c *Ctx) error { return wrapped(c.fc) })
|
|
}
|
|
|
|
// AdaptNetHTTPMiddleware wraps a stdlib middleware (func(http.Handler) http.Handler)
|
|
// as a zip.Handler — the net/http-middleware bridge. Because it IS middleware,
|
|
// it goes on a Group's Use rather than on a route:
|
|
//
|
|
// app.Group("/legacy").Use(zip.AdaptNetHTTPMiddleware(mw))
|
|
//
|
|
// Migration tool — costs ~5% perf vs native Fiber.
|
|
func AdaptNetHTTPMiddleware(mw func(http.Handler) http.Handler) Handler {
|
|
wrapped := adaptor.HTTPMiddleware(mw)
|
|
return func(c *Ctx) error { return wrapped(c.fc) }
|
|
}
|