middleware

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthHTTP added in v0.5.0

func AuthHTTP(opts ...AuthOption) func(http.Handler) http.Handler

AuthHTTP returns an HTTP middleware that:

  1. Skips authentication for paths registered via WithAuthSkipPaths.
  2. Extracts the "Authorization" header from the HTTP request.
  3. Validates the Bearer scheme and strips the prefix.
  4. Delegates to the client-provided validator for token verification.
  5. Passes the enriched context to downstream handlers.

Error responses: returns 401 JSON if token is missing, invalid scheme, or validator rejects.

func AuthInterceptor added in v0.5.0

func AuthInterceptor(opts ...AuthOption) grpc.UnaryServerInterceptor

AuthInterceptor returns a gRPC UnaryServerInterceptor that:

  1. Skips authentication for methods registered via WithAuthSkipPaths.
  2. Extracts the "authorization" value from incoming gRPC metadata.
  3. Validates the Bearer scheme and strips the prefix.
  4. Delegates to the client-provided validator for token verification.
  5. Passes the enriched context to downstream handlers.

Error passthrough: if the validator returns a *merr.Error or any error with a GRPCStatus() method, it is passed directly to the client. Otherwise, the error is wrapped as codes.Unauthenticated.

func AuthPlugin added in v0.5.0

func AuthPlugin(opts ...AuthOption) minato.Plugin

AuthPlugin bundles AuthHTTP (HTTP) and AuthInterceptor (gRPC) into a single minato.Plugin for dual-transport registration via srv.UsePlugin().

This is the recommended entry point when your server handles both pure HTTP and gRPC traffic.

Example:

v := auth.NewTokenValidator(publicKey, blacklist)
srv.UsePlugin(middleware.AuthPlugin(
    middleware.WithAuthSkipPaths(
        "/auth.v1.AuthService/Login",     // gRPC
        "/api/v1/auth/login",             // HTTP
    ),
    middleware.WithAuthValidator(v.Validate),
))

func CORS

func CORS(opts ...CORSOption) func(http.Handler) http.Handler

func Logger

func Logger(opts ...LoggerOption) func(http.Handler) http.Handler

Logger returns a middleware that logs requests.

func LoggerInterceptor added in v0.2.0

func LoggerInterceptor(opts ...LoggerOption) grpc.UnaryServerInterceptor

LoggerInterceptor is the gRPC unary interceptor form of Logger.

func LoggerPlugin added in v0.2.0

func LoggerPlugin(opts ...LoggerOption) minato.Plugin

LoggerPlugin bundles Logger (HTTP) and LoggerInterceptor (gRPC).

func Recovery

func Recovery(opts ...RecoveryOption) func(http.Handler) http.Handler

func RecoveryInterceptor added in v0.2.0

func RecoveryInterceptor(opts ...RecoveryOption) grpc.UnaryServerInterceptor

RecoverInterceptor is the gRPC unary interceptor form of recovery.

func RecoveryPlugin added in v0.2.0

func RecoveryPlugin(opts ...RecoveryOption) minato.Plugin

RecoveryPlugin bundles Recovery (HTTP) and RecoveryInterceptor (gRPC).

func RequestID

func RequestID() func(http.Handler) http.Handler

RequestID returns a middleware that generates a unique request ID for each request.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the request ID from the context.

func RequestIDInterceptor added in v0.2.0

func RequestIDInterceptor() grpc.UnaryServerInterceptor

RequestIDInterceptor is the gRPC unary interceptor form of RequestID It reads x-request-id from incoming gRPC metadata ; generates a UUID if absent;

func RequestIDPlugin added in v0.2.0

func RequestIDPlugin() minato.Plugin

RequestIDPlugin bundles RequestID (HTTP) and RequestIDInterceptor (gRPC).

Types

type AuthOption added in v0.5.0

type AuthOption func(*authConfig)

AuthOption is a functional option for configuring the Auth middleware.

func WithAuthSkipPaths added in v0.5.0

func WithAuthSkipPaths(paths ...string) AuthOption

WithAuthSkipPaths registers paths that do NOT require authentication.

For gRPC: use the full method path, e.g. "/auth.v1.AuthService/Login". For HTTP: use the URL path, e.g. "/api/v1/auth/login".

Paths are checked with an O(1) map lookup — no regex or prefix matching overhead.

func WithAuthValidator added in v0.5.0

func WithAuthValidator(fn AuthValidatorFunc) AuthOption

WithAuthValidator registers the token validation function.

The function receives:

  • ctx: the current request context
  • token: the raw token string (e.g. "eyJhbGciOi..."), Bearer prefix already stripped

It must return:

  • A (possibly enriched) context — e.g. with user claims injected via context.WithValue
  • An error if the token is invalid/expired/revoked

Performance note: heavy initialization (JWT parser allocation, key loading, connection pooling) should be done OUTSIDE this function and captured via closure.

type AuthValidatorFunc added in v0.5.0

type AuthValidatorFunc func(ctx context.Context, token string) (context.Context, error)

AuthValidatorFunc is the signature that client business logic must implement. It receives the raw JWT token string (Bearer prefix already stripped). It must return an enriched context (e.g. with user claims) or an error.

type CORSOption

type CORSOption func(*corsConfig)

func WithAllowedHeaders

func WithAllowedHeaders(headers ...string) CORSOption

func WithAllowedMethods

func WithAllowedMethods(methods ...string) CORSOption

func WithAllowedOrigins

func WithAllowedOrigins(origins ...string) CORSOption

type LogPrinter added in v0.1.1

type LogPrinter interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

LogPrinter defines the logging methods required by middlewares.

type LoggerOption

type LoggerOption func(*loggerConfig)

func WithBodyLogging

func WithBodyLogging(enabled bool) LoggerOption

WithBodyLogging enables logging of request and response bodies.

func WithLogPrinter added in v0.1.1

func WithLogPrinter(p LogPrinter) LoggerOption

WithLogPrinter allows injecting a custom logger into the middleware.

type RecoveryOption added in v0.1.1

type RecoveryOption func(*recoveryConfig)

func WithRecoveryPrinter added in v0.1.1

func WithRecoveryPrinter(p LogPrinter) RecoveryOption

WithRecoveryPrinter allows injecting a custom logger for panic reporting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL