Files
protocol/utils/xtwirp/handler.go
Denys Smirnov 331f97dbf4 Add Twirp options to preserve error details and client timeouts (#963)
* Add Twirp client/server options to preserve gRPC error details.

* Pass Twirp client timeouts to the server.
2025-02-06 13:05:18 +02:00

28 lines
643 B
Go

package xtwirp
import "net/http"
// Server is a minimal interface for a Twirp server.
type Server interface {
http.Handler
PathPrefix() string
}
// WrapHandler wraps the Twirp server handler with our custom middleware.
func WrapHandler(s Server) http.Handler {
wrappers := []func(http.Handler) http.Handler{
PassHeadersHandler,
}
var h http.Handler = s
for _, wrapper := range wrappers {
h = wrapper(h)
}
return h
}
// RegisterServer registers Twirp server on an HTTP mux.
// It also calls WrapHandler to add default middleware.
func RegisterServer(mux *http.ServeMux, s Server) {
mux.Handle(s.PathPrefix(), WrapHandler(s))
}