mirror of
https://github.com/zap-proto/http.git
synced 2026-10-04 03:07:19 +00:00
Half of net.go was me explaining the design at length. A reader needs two facts: these are net/http's own types, and the net/http shapes allocate where fasthttp does not. Everything else was restating the code above it. 63 lines to 39, same behaviour, same tests. Co-authored-by: Hanzo Dev <[email protected]>
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
// Copyright © 2026. MIT License.
|
|
|
|
package http
|
|
|
|
// The net/http face of this package: an existing handler runs unchanged, over
|
|
// ZAP frames instead of HTTP/1.1.
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/valyala/fasthttp/fasthttpadaptor"
|
|
)
|
|
|
|
// Aliases, not lookalikes — a struct with the same fields would be a different
|
|
// type, and every caller would need a conversion.
|
|
type (
|
|
Request = http.Request
|
|
Response = http.Response
|
|
Header = http.Header
|
|
Handler = http.Handler
|
|
HandlerFunc = http.HandlerFunc
|
|
ResponseWriter = http.ResponseWriter
|
|
ServeMux = http.ServeMux
|
|
)
|
|
|
|
func NewServeMux() *ServeMux { return http.NewServeMux() }
|
|
|
|
// ListenAndServe serves h over ZAP. A caller holding a fasthttp handler builds
|
|
// &Server{Addr, Handler} instead.
|
|
func ListenAndServe(addr string, h Handler) error {
|
|
return (&Server{Addr: addr, Handler: Adapt(h)}).ListenAndServe()
|
|
}
|
|
|
|
// Adapt bridges to the fasthttp handler Server takes. Exported so the cost is
|
|
// visible where it lands: a net/http Request allocates, fasthttp's does not.
|
|
func Adapt(h Handler) fasthttp.RequestHandler {
|
|
return fasthttpadaptor.NewFastHTTPHandler(h)
|
|
}
|