Documentation
¶
Index ¶
- Constants
- func DefaultClientMiddlewares() []middleware.Middleware
- func DefaultServerMiddlewares() []middleware.Middleware
- func NewClient(ctx context.Context, httpConfig *transportv1.HttpClientConfig, ...) (*transhttp.Client, error)
- func NewCorsHandler(config *corsv1.Cors, codeOpts ...CorsOption) (func(http.Handler) http.Handler, error)
- func NewServer(httpConfig *transportv1.HttpServerConfig, serverOpts *ServerOptions) (*transhttp.Server, error)
- func WithCorsOptions(opts ...CorsOption) options.Option
- func WithHttpClientOptions(opts ...transhttp.ClientOption) options.Option
- func WithHttpServerOptions(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 *transportv1.HttpClientConfig, 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
func NewServer(httpConfig *transportv1.HttpServerConfig, serverOpts *ServerOptions) (*transhttp.Server, error)
NewServer creates a new concrete HTTP server instance based on the provided configuration. It returns *transhttp.Server, not the generic interfaces.Server.
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 WithHttpClientOptions ¶
func WithHttpClientOptions(opts ...transhttp.ClientOption) options.Option
WithHttpClientOptions appends Kratos HTTP client options.
func WithHttpServerOptions ¶
func WithHttpServerOptions(opts ...transhttp.ServerOption) options.Option
WithHttpServerOptions appends Kratos HTTP server options.
Types ¶
type ClientOptions ¶
type ClientOptions struct {
// ServiceOptions holds common service-level configurations.
ServiceOptions *service.Options
// HttpClientOptions allows passing native Kratos HTTP client options.
HttpClientOptions []transhttp.ClientOption
}
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 { return r.Header.Get("X-Custom") == "true" })`
type ServerOptions ¶
type ServerOptions struct {
options.Context
// ServiceOptions holds common service-level configurations.
ServiceOptions *service.Options
// HttpServerOptions allows passing native Kratos HTTP server options.
HttpServerOptions []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
}
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.