Documentation
¶
Overview ¶
Package middleware populates request context and normalizes errors at the two edges OpenPay serves: the gRPC server and the grpc-gateway HTTP mux.
Both edges exist because the HTTP path registers the service in-process (RegisterOpenPayHandlerServer), which bypasses gRPC interceptors entirely. Anything that must apply to every request therefore needs an implementation on each side; keeping them in one package is what stops them drifting.
Index ¶
- func CallerMiddleware(authenticator Authenticator) runtime.Middleware
- func CallerUnaryInterceptor(authenticator Authenticator) grpc.UnaryServerInterceptor
- func ErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, ...)
- func ErrorUnaryInterceptor() grpc.UnaryServerInterceptor
- func IncomingHeaderMatcher(key string) (string, bool)
- func OutgoingHeaderMatcher(key string) (string, bool)
- func RateLimitMiddleware(limiter RateLimiter) runtime.Middleware
- func RateLimitUnaryInterceptor(limiter RateLimiter) grpc.UnaryServerInterceptor
- func TraceMiddleLayer(ctx context.Context, msg string, fields *logger.Fields) (context.Context, string, *logger.Fields)
- type Authenticator
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallerMiddleware ¶
func CallerMiddleware(authenticator Authenticator) runtime.Middleware
CallerMiddleware authenticates and populates the caller for requests arriving over HTTP.
It reads the raw headers rather than gRPC metadata because gateway middlewares run before the request is annotated, and it echoes the request id back so a caller can quote it in a support ticket.
func CallerUnaryInterceptor ¶
func CallerUnaryInterceptor(authenticator Authenticator) grpc.UnaryServerInterceptor
CallerUnaryInterceptor authenticates and populates the caller for gRPC requests.
func ErrorHandler ¶
func ErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error)
ErrorHandler renders errors as {code, message, request_id} instead of the gateway's default {code: <number>, message, details}. A numeric gRPC code is not a useful contract for a client; a stable string is.
func ErrorUnaryInterceptor ¶
func ErrorUnaryInterceptor() grpc.UnaryServerInterceptor
ErrorUnaryInterceptor logs the full detail of a failure and returns the caller-safe form. Internal errors keep their SQL and driver text in the logs and send a generic message on the wire (apperrors.Error.Message).
func IncomingHeaderMatcher ¶
IncomingHeaderMatcher forwards OpenPay's headers and otherwise defers to the gateway's default behaviour.
func OutgoingHeaderMatcher ¶
OutgoingHeaderMatcher sends response metadata a browser must see as a real header (an export's file name) under its own name, and everything else the gateway's default way, prefixed Grpc-Metadata-.
func RateLimitMiddleware ¶
func RateLimitMiddleware(limiter RateLimiter) runtime.Middleware
RateLimitMiddleware is the HTTP twin of RateLimitUnaryInterceptor, and must likewise come after CallerMiddleware.
func RateLimitUnaryInterceptor ¶
func RateLimitUnaryInterceptor(limiter RateLimiter) grpc.UnaryServerInterceptor
RateLimitUnaryInterceptor refuses gRPC requests over the caller's limit. It must run after CallerUnaryInterceptor: the limit is per credential, so it needs to know whose request this is.
func TraceMiddleLayer ¶
func TraceMiddleLayer(ctx context.Context, msg string, fields *logger.Fields) (context.Context, string, *logger.Fields)
TraceMiddleLayer adds trace_id and span_id to every log line emitted inside a span. Without it, traces and logs are two separate investigations: you can see a request was slow, or read what it logged, but not join them.
Register it alongside logger.RequestMiddleLayer at startup.
Types ¶
type Authenticator ¶
type Authenticator interface {
Authenticate(ctx context.Context, get func(key string) string) (appcontext.Caller, error)
}
Authenticator resolves a caller from request headers.
type RateLimiter ¶
type RateLimiter interface {
Allow(caller appcontext.Caller) error
}
RateLimiter decides whether an authenticated caller may proceed.