Documentation
¶
Overview ¶
Package gateway makes a grpc-gateway a first-class transport: it dials the local gRPC server (matching its transport security) and serves the generated REST handlers, either mounted on an existing HTTP server (New) or as its own controller-managed HTTP server on the "server.gateway" config block (Register).
Index ¶
Constants ¶
const ConfigPrefix = "server.gateway"
ConfigPrefix is the config block a gateway server reads (port, TLS) when run as its own service via Register. TLS falls back to the shared "server.tls".
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(ctx context.Context, cfg config.Containable, register RegisterFunc, opts ...Option) (http.Handler, error)
New builds a grpc-gateway handler ready to mount on an existing HTTP server. It dials the local gRPC server via grpc.DialLocal (so transport security matches the server's own config) and applies register to wire the handlers.
The returned handler is a *runtime.ServeMux (or, with WithMiddleware, that mux wrapped by the chain). The underlying gRPC connection lives for the process; it is the standard pattern for an in-process gateway.
func Register ¶
func Register(ctx context.Context, id string, controller controls.Controllable, cfg config.Containable, logger logger.Logger, register RegisterFunc, opts ...Option) (*http.Server, error)
Register runs the gateway as its own controller-managed HTTP server on the "server.gateway" config block (TLS falling back to the shared "server.tls"), dialing the local gRPC server. It is the first-class form of New, a peer of grpc.Register and http.Register. Pass WithMiddleware to wrap the REST surface with a middleware chain (health endpoints stay outside it).
Types ¶
type Option ¶
type Option func(*options)
Option configures the gateway.
func WithDialOptions ¶
func WithDialOptions(opts ...grpc.DialOption) Option
WithDialOptions passes extra grpc.DialOption values to the connection the gateway opens to the gRPC server (transport security is set automatically).
func WithMiddleware ¶ added in v0.23.0
WithMiddleware wraps the gateway's REST surface with an HTTP middleware chain (logging, security headers, rate limiting, auth, …). The gateway handler is an ordinary http.Handler, so the standard pkg/http Chain applies to it.
On the New path the chain wraps the returned handler directly. On the Register path it is threaded to the managed server so health endpoints (/healthz, /livez, /readyz) remain outside the chain, exactly as with http.Register.
func WithMuxOptions ¶
func WithMuxOptions(opts ...runtime.ServeMuxOption) Option
WithMuxOptions passes runtime.ServeMuxOption values to the gateway mux (e.g. a custom error handler or header matcher).
type RegisterFunc ¶
RegisterFunc registers the generated gateway handlers onto the mux, using a client connection to the gRPC server. It is the only gateway-specific code a caller writes, e.g.:
func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return widgetv1.RegisterWidgetServiceHandler(ctx, mux, conn)
}