http

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
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

type CorsOption func(*cors.Options)

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.

Jump to

Keyboard shortcuts

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