Documentation
¶
Index ¶
- Constants
- func DefaultClientMiddlewares() []middleware.Middleware
- func DefaultServerMiddlewares() []middleware.Middleware
- func NewClient(ctx context.Context, httpConfig *httpv1.Client, clientOpts *ClientOptions) (*transhttp.Client, error)
- func NewCorsHandler(config *corsv1.Cors, codeOpts ...CorsOption) (func(http.Handler) http.Handler, error)
- func NewServer(httpConfig *httpv1.Server, serverOpts *ServerOptions) (*transhttp.Server, error)
- func RegisterPprof(srv *transhttp.Server)
- func WithClientMiddlewares(mws map[string]middleware.Middleware) options.Option
- func WithClientOptions(opts ...transhttp.ClientOption) options.Option
- func WithContext(ctx context.Context) options.Option
- func WithContextRegistry(ctx context.Context, registrar transport.HTTPRegistrar) options.Option
- func WithCorsOptions(opts ...CorsOption) options.Option
- func WithDiscoveries(discoveries map[string]registry.Discovery) options.Option
- func WithRegistrar(registrar transport.HTTPRegistrar) options.Option
- func WithServerMiddlewares(mws map[string]middleware.Middleware) options.Option
- func WithServerOptions(opts ...transhttp.ServerOption) options.Option
- type ClientOptions
- type CorsOption
- type ServerOptions
Constants ¶
const Module = "transport.http"
Variables ¶
This section is empty.
Functions ¶
func DefaultClientMiddlewares ¶
func DefaultClientMiddlewares() []middleware.Middleware
DefaultClientMiddlewares provides a default set of client-side middlewares for HTTP services.
func DefaultServerMiddlewares ¶
func DefaultServerMiddlewares() []middleware.Middleware
DefaultServerMiddlewares provides a default set of server-side middlewares for HTTP services. These are essential for ensuring basic stability and observability.
func NewClient ¶ added in v0.2.7
func NewClient(ctx context.Context, httpConfig *httpv1.Client, clientOpts *ClientOptions) (*transhttp.Client, error)
NewClient creates a new concrete HTTP client connection based on the provided configuration. It returns *transhttp.Client, not the generic interfaces.Client.
func NewCorsHandler ¶
func NewCorsHandler(config *corsv1.Cors, codeOpts ...CorsOption) (func(http.Handler) http.Handler, error)
NewCorsHandler creates a Kratos filter for CORS by merging framework defaults, the base configuration from proto, and advanced, code-based functional options.
func NewServer ¶ added in v0.2.7
NewServer creates a new concrete HTTP server instance based on the provided configuration. It returns *transhttp.Server, not the generic interfaces.Server.
func RegisterPprof ¶ added in v0.2.13
RegisterPprof registers the pprof handlers with the HTTP server.
func WithClientMiddlewares ¶ added in v0.2.13
func WithClientMiddlewares(mws map[string]middleware.Middleware) options.Option
WithClientMiddlewares adds a map of named client middlewares to the options.
func WithClientOptions ¶ added in v0.2.13
func WithClientOptions(opts ...transhttp.ClientOption) options.Option
WithClientOptions appends Kratos HTTP client options.
func WithContext ¶ added in v0.2.13
WithContext sets the context.Context for the service.
func WithContextRegistry ¶ added in v0.2.13
WithContextRegistry sets both the context.Context and ServerRegistrar for the service.
func WithCorsOptions ¶
func WithCorsOptions(opts ...CorsOption) options.Option
WithCorsOptions appends advanced, code-based CORS configurations. These will be applied on top of the CORS settings from the proto configuration.
func WithDiscoveries ¶ added in v0.2.13
WithDiscoveries adds a map of named service discovery clients to the options.
func WithRegistrar ¶ added in v0.2.13
func WithRegistrar(registrar transport.HTTPRegistrar) options.Option
WithRegistrar sets the HTTP registrar to use for service registration.
func WithServerMiddlewares ¶ added in v0.2.13
func WithServerMiddlewares(mws map[string]middleware.Middleware) options.Option
WithServerMiddlewares adds a map of named server middlewares to the options.
func WithServerOptions ¶ added in v0.2.13
func WithServerOptions(opts ...transhttp.ServerOption) options.Option
WithServerOptions appends Kratos HTTP server options.
Types ¶
type ClientOptions ¶
type ClientOptions struct {
// ClientOptions allows passing native Kratos HTTP client dial options.
ClientOptions []transhttp.ClientOption
// ClientMiddlewares holds a map of named client middlewares.
ClientMiddlewares map[string]middleware.Middleware
// Discoveries holds a map of named service discovery clients.
Discoveries map[string]registry.Discovery
}
ClientOptions is a container for HTTP client-specific options.
func FromClientOptions ¶
func FromClientOptions(opts []options.Option) *ClientOptions
FromClientOptions creates a new HTTP ClientOptions struct by applying a slice of functional options. It also initializes and includes the common service-level options, ensuring they are applied only once.
type CorsOption ¶
CorsOption defines a functional option for configuring advanced CORS settings in code. It allows developers to override or enhance the base configuration provided by the proto file.
func WithAllowOriginFunc ¶
func WithAllowOriginFunc(f func(origin string) bool) CorsOption
WithAllowOriginFunc sets the `AllowOriginFunc` on the underlying `cors.Options`. This provides dynamic, request-based control over allowed origins, which cannot be defined in a static proto file. Example: `WithAllowOriginFunc(func(origin string) bool { return origin == "https://example.com" })`
func WithAllowOriginVaryRequestFunc ¶
func WithAllowOriginVaryRequestFunc(f func(r *http.Request, origin string) (bool, []string)) CorsOption
WithAllowOriginVaryRequestFunc sets the `AllowOriginVaryRequestFunc` on the underlying `cors.Options`. This allows dynamic origin control based on the full http.Request. Example: `WithAllowOriginVaryRequestFunc(func(r *http.Request, origin string) (bool, []string) { return r.Header.Get("X-Custom") == "true", nil })`
type ServerOptions ¶
type ServerOptions struct {
// ServerOptions allows passing native Kratos HTTP server options.
ServerOptions []transhttp.ServerOption
// CorsOptions allows applying advanced, code-based configurations to the CORS handler.
// These options will be applied *after* the base configuration from the proto file.
CorsOptions []CorsOption
// Registrar is the HTTP registrar to use for service registration.
Registrar transport.HTTPRegistrar
// ServerMiddlewares holds a map of named server middlewares.
ServerMiddlewares map[string]middleware.Middleware
Context context.Context
}
ServerOptions is a container for HTTP server-specific options.
func FromServerOptions ¶
func FromServerOptions(opts []options.Option) *ServerOptions
FromServerOptions creates a new HTTP ServerOptions struct by applying a slice of functional options. It also initializes and includes the common service-level options, ensuring they are applied only once.