middleware

package
v0.2.19 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 35 Imported by: 2

Documentation

Overview

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware contains generated code by adptool.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Package middleware implements the functions, types, and contracts for the module.

Index

Constants

View Source
const (
	// CategoryMiddleware is the category for middleware components.
	CategoryMiddleware component.Category = "middleware"
	// CategoryLogger is the category for logger components.
	CategoryLogger component.Category = "logger"

	// ServerScope is the standard scope for server-side components.
	ServerScope component.Scope = "server"
	// ClientScope is the standard scope for client-side components.
	ClientScope component.Scope = "client"
)
View Source
const (
	// RequirementOption is the purpose for gathering creation options.
	RequirementOption = "option"
	// RequirementConfig is the purpose for gathering configuration.
	RequirementConfig = "config"
	// RequirementCarrier is the purpose for gathering other components in the same scope.
	RequirementCarrier = "carrier"
)
View Source
const Type = "middleware"

Variables

View Source
var ClientProvider component.Provider = func(ctx context.Context, h component.Handle) (any, error) {
	if h.Scope() != ClientScope {
		return nil, nil
	}
	cfg, opts, err := collectOptions(h)
	if err != nil {
		return nil, err
	}
	m, ok := NewClient(cfg, opts...)
	if !ok {
		return nil, nil
	}
	return m, nil
}

ClientProvider is the engine-compatible provider for client-side middleware components.

View Source
var DefaultProvider component.Provider = func(ctx context.Context, h component.Handle) (any, error) {
	cfg, opts, err := collectOptions(h)
	if err != nil {
		return nil, err
	}
	if h.Scope() == ClientScope {
		m, ok := NewClient(cfg, opts...)
		if ok {
			return m, nil
		}
	} else {
		m, ok := NewServer(cfg, opts...)
		if ok {
			return m, nil
		}
	}
	return nil, nil
}

DefaultProvider acts as a catch-all dispatcher.

View Source
var ServerProvider component.Provider = func(ctx context.Context, h component.Handle) (any, error) {
	if h.Scope() != ServerScope {
		return nil, nil
	}
	cfg, opts, err := collectOptions(h)
	if err != nil {
		return nil, err
	}
	m, ok := NewServer(cfg, opts...)
	if !ok {
		return nil, nil
	}
	return m, nil
}

ServerProvider is the engine-compatible provider for server-side middleware components.

Functions

func GetMiddlewares added in v0.2.19

func GetMiddlewares(ctx context.Context, locator component.Locator) (map[string]KMiddleware, error)

GetMiddlewares collects all available middlewares from the given locator as a map.

func GetSigningMethod added in v0.2.13

func GetSigningMethod(alg string) jwt.SigningMethod

GetSigningMethod returns the jwt.SigningMethod based on the provided string. Exported for testing purposes.

func KChain added in v0.2.6

func NewClaimsFactory added in v0.2.13

func NewClaimsFactory(cfg *jwtv1.JWT, opts *Options) func() jwt.Claims

NewClaimsFactory is a higher-order function that takes JWT configuration and returns a `func() jwt.Claims`. This returned function, when called, generates a new set of claims based on the provided configuration. It automatically handles issuer, lifetime, and generates a random UUID for the subject. The implementation is optimized to avoid recreating static configuration on each call. Exported for testing purposes.

func RegisterFactory added in v0.2.13

func RegisterFactory(name Name, factory Factory)

func Resolve added in v0.2.19

func Resolve(ctx context.Context, source any, opts *component.LoadOptions) (*component.ModuleConfig, error)

Resolve resolves the middleware configuration.

Types

type Builder added in v0.2.0

type Builder struct {
	builder.Registry[Factory]
}

Builder is a builder for creating middleware chains.

func NewBuilder added in v0.2.0

func NewBuilder() *Builder

func (*Builder) BuildClientMiddlewares added in v0.2.7

func (b *Builder) BuildClientMiddlewares(cfg *middlewarev1.Middlewares, opts ...Option) []KMiddleware

BuildClientMiddlewares builds the client middleware chain

func (*Builder) BuildServerMiddlewares added in v0.2.7

func (b *Builder) BuildServerMiddlewares(cfg *middlewarev1.Middlewares, opts ...Option) []KMiddleware

BuildServerMiddlewares builds the server middleware chain

type Carrier added in v0.2.6

type Carrier struct {
	Clients map[string]KMiddleware
	Servers map[string]KMiddleware
}

Carrier is a struct that holds the middlewares for client and server.

type Factory added in v0.2.0

type Factory interface {
	// NewMiddlewareClient builds a client-side middleware.
	NewMiddlewareClient(*middlewarev1.Middleware, ...Option) (KMiddleware, bool)
	// NewMiddlewareServer builds a server-side middleware.
	NewMiddlewareServer(*middlewarev1.Middleware, ...Option) (KMiddleware, bool)
}

Factory is an interface that defines a method for creating a new buildImpl. It receives the middleware-specific Protobuf configuration and the generic Option slice. Each factory is responsible for parsing the options it cares about (e.g., by using log.FromOptions).

type KHandler added in v0.1.18

type KHandler = middleware.Handler

type KMiddleware added in v0.1.18

type KMiddleware = middleware.Middleware

func BuildClients added in v0.2.13

func BuildClients(cfg *middlewarev1.Middlewares, opts ...Option) []KMiddleware

func BuildServers added in v0.2.13

func BuildServers(cfg *middlewarev1.Middlewares, opts ...Option) []KMiddleware

func GetMiddlewareList added in v0.2.19

func GetMiddlewareList(ctx context.Context, locator component.Locator) ([]KMiddleware, error)

GetMiddlewareList collects all middlewares from the given locator as a slice.

func JwtClient added in v0.1.15

func JwtClient(cfg *jwtv1.JWT, opts *Options) (KMiddleware, bool)

JwtClient creates a Kratos client middleware for JWT token generation and injection. This middleware dynamically creates a new `authjwt.Client` instance for each request, ensuring that the JWT is generated based on the claims present in the request's context.

func JwtServer added in v0.1.15

func JwtServer(cfg *jwtv1.JWT, opts *Options) (KMiddleware, bool)

JwtServer creates a Kratos server middleware for JWT authentication. It uses the provided JWT configuration to validate incoming tokens.

func NewClient

func NewClient(cfg *middlewarev1.Middleware, opts ...Option) (KMiddleware, bool)

NewClient creates a single client-side middleware instance.

func NewServer

func NewServer(cfg *middlewarev1.Middleware, opts ...Option) (KMiddleware, bool)

NewServer creates a single server-side middleware instance.

func Noop added in v0.2.13

func Noop() KMiddleware

Noop returns an empty middleware.

func SelectorClient added in v0.1.15

func SelectorClient(cfg *selectorv1.Selector, matchFunc selector.MatchFunc, middlewares ...KMiddleware) KMiddleware

SelectorClient creates a selector middleware for client-side.

func SelectorServer added in v0.1.15

func SelectorServer(cfg *selectorv1.Selector, matchFunc selector.MatchFunc, middlewares ...KMiddleware) KMiddleware

SelectorServer creates a selector middleware for server-side.

func Validate deprecated

func Validate(ms []KMiddleware, validator *validatorv1.Validator) []KMiddleware

Deprecated: use ValidateServer

func ValidateServer added in v0.0.12

func ValidateServer(ms []KMiddleware, validator *validatorv1.Validator) []KMiddleware

type Name added in v0.2.6

type Name string

Name is the name of a middleware.

const (
	Jwt                 Name = "jwt"
	CircuitBreaker      Name = "circuit_breaker"
	Logging             Name = "logging"
	Metadata            Name = "metadata"
	RateLimiter         Name = "rate_limiter"
	Tracing             Name = "tracing"
	Validator           Name = "validator"
	Optimize            Name = "optimize"
	Recovery            Name = "recovery"
	Selector            Name = "selector"
	DeclarativeSecurity Name = "declarative_security"
	Cors                Name = "cors"
)

Middleware names.

type Option added in v0.1.0

type Option = options.Option

Option is a functional option for configuring middleware options.

func WithCarrier added in v0.2.6

func WithCarrier(carrier *Carrier) Option

WithCarrier sets the full Carrier for the middleware options.

func WithClaimsFactory added in v0.2.13

func WithClaimsFactory(factory func() jwt.Claims) Option

WithClaimsFactory provides a function that generates JWT claims. This is the recommended way to create dynamic claims for each token.

func WithClientCarrier added in v0.2.13

func WithClientCarrier(clientMiddlewares map[string]KMiddleware) Option

WithClientCarrier sets the client middlewares map in the Carrier.

func WithMatchFunc added in v0.1.29

func WithMatchFunc(matchFunc selector.MatchFunc) Option

WithMatchFunc sets the MatchFunc for the selector middleware.

func WithServerCarrier added in v0.2.13

func WithServerCarrier(serverMiddlewares map[string]KMiddleware) Option

WithServerCarrier sets the server middlewares map in the Carrier.

func WithSigningMethod added in v0.2.13

func WithSigningMethod(method jwt.SigningMethod) Option

WithSigningMethod sets the signing method to be used for JWT tokens. If not provided, the default from the configuration will be used.

func WithSubjectFactory added in v0.2.13

func WithSubjectFactory(factory func() string) Option

WithSubjectFactory provides a function that generates the JWT 'subject' (sub) claim. This is the recommended way to provide a meaningful user identifier for the token.

type Options added in v0.1.56

type Options struct {
	Logger         log.Logger
	MatchFunc      selector.MatchFunc
	Carrier        *Carrier
	Options        []Option
	ClaimsFactory  func() jwt.Claims
	SubjectFactory func() string
	SigningMethod  jwt.SigningMethod
}

Options holds common options that have been resolved once at the top level. These options are then passed down to individual middleware factory functions.

func FromOptions added in v0.2.6

func FromOptions(opts ...Option) *Options

FromOptions resolves common options from a slice of generic Option.

func (*Options) GetLogger added in v0.2.19

func (o *Options) GetLogger(module string) *log.Helper

GetLogger returns a log.Helper with the specified module name.

type Provider added in v0.2.19

type Provider interface {
	Middleware(name string) (KMiddleware, error)
}

Provider defines the interface for a middleware service provider.

func NewProvider added in v0.2.19

func NewProvider(locator component.Locator, scope component.Scope) Provider

NewProvider creates a new middleware provider instance for a specific scope.

Directories

Path Synopsis
Package cors implements CORS middleware for the framework.
Package cors implements CORS middleware for the framework.
Package validate implements the functions, types, and contracts for the module.
Package validate implements the functions, types, and contracts for the module.

Jump to

Keyboard shortcuts

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