httpkit

package
v0.2.20 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient added in v0.2.8

func NewClient(options ...ClientOption) *http.Client

NewClient takes options to configure and return a pointer to a new instance of http.Client.

func ProfilerMiddleware added in v0.2.7

func ProfilerMiddleware() http.Handler

Types

type ClientOption added in v0.2.8

type ClientOption func(config *Config)

ClientOption represents functional options pattern for Config type. ClientOption type represents a function which receive a pointer Config struct. ClientOption functions can only be passed to NewClient function. ClientOption function can change the default value of Config struct fields.

func WithCustomDialer added in v0.2.8

func WithCustomDialer(dialer CustomDialer) ClientOption

WithCustomDialer sets given dialer as custom net.Dialer underlying http.Transport of http.Client.

func WithDialTimeout added in v0.2.8

func WithDialTimeout(timeout time.Duration) ClientOption

WithDialTimeout sets the Dial timeout to underlying http.Transport of http.Client.

func WithKeepAliveDisabled added in v0.2.8

func WithKeepAliveDisabled(disabled bool) ClientOption

WithKeepAliveDisabled sets the

func WithKeepAliveTimeout added in v0.2.8

func WithKeepAliveTimeout(timeout time.Duration) ClientOption

WithKeepAliveTimeout sets the KeepAlive timeout to underlying http.Transport of http.Client.

func WithMaxIdleConns added in v0.2.8

func WithMaxIdleConns(max int) ClientOption

WithMaxIdleConns sets the MaxIdleConns value to underlying http.Transport of http.Client.

func WithMaxIdleConnsPerHost added in v0.2.8

func WithMaxIdleConnsPerHost(max int) ClientOption

WithMaxIdleConnsPerHost sets the MaxIdleConnsPerHost value to underlying http.Transport of http.Client.

func WithReadBufferSize added in v0.2.8

func WithReadBufferSize(size int) ClientOption

WithReadBufferSize sets the ReadBufferSize value to underlying http.Transport of http.Client.

func WithResponseHeaderTimeout added in v0.2.8

func WithResponseHeaderTimeout(timeout time.Duration) ClientOption

WithResponseHeaderTimeout sets the ResponseHeaderTimeout value to underlying http.Transport of http.Client.

func WithRetries added in v0.2.8

func WithRetries(options ...retry.Option) ClientOption

WithRetries configure http.Client to do retries when request failed and retry could be made.

func WithTLSHandshakeTimeout added in v0.2.8

func WithTLSHandshakeTimeout(timeout time.Duration) ClientOption

WithTLSHandshakeTimeout sets timeout for TLS handshake to underlying http.Transport of http.Client, after which connection will be terminated.

func WithWriteBufferSize added in v0.2.8

func WithWriteBufferSize(size int) ClientOption

WithWriteBufferSize sets the WriteBufferSize value to underlying http.Transport of http.Client.

type Config added in v0.2.8

type Config struct {
	// contains filtered or unexported fields
}

Config holds configuration options which will be applied to http.Client.

type CustomDialer added in v0.2.8

type CustomDialer interface {
	// DialContext connects to the address on the named network using
	// the provided context.
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

CustomDialer holds logic of establishing connection to remote network address.

type HealthConfig added in v0.2.15

type HealthConfig struct {
	// contains filtered or unexported fields
}

HealthConfig represents configuration for builtin health check route.

type ListenerConfig added in v0.2.15

type ListenerConfig struct {
	// contains filtered or unexported fields
}

ListenerConfig holds ListenerHTTP configuration.

type ListenerHTTP

type ListenerHTTP struct {
	// contains filtered or unexported fields
}

func NewListenerHTTP

func NewListenerHTTP(addr string, options ...ListenerOption[ListenerConfig]) (*ListenerHTTP, error)

NewListenerHTTP creates a new ListenerHTTP with the specified address and options. The options parameter is a variadic argument that accepts functions of type ListenerOption. The ListenerHTTP instance is returned, which can be used to mount routes and start serving requests.

func (*ListenerHTTP) Mount

func (l *ListenerHTTP) Mount(route string, handler http.Handler, middlewares ...Middleware)

func (*ListenerHTTP) MountGroup added in v0.2.2

func (l *ListenerHTTP) MountGroup(route string, fn func(r chi.Router))

func (*ListenerHTTP) Serve

func (l *ListenerHTTP) Serve(ctx context.Context) error

type ListenerOption added in v0.2.15

type ListenerOption[T ListenerOptionConstraint] func(o *T)

ListenerOption implements functional options pattern for the ListenerHTTP type. Represents a function which receive a pointer to the generic struct that represents a part of ListenerHTTP configuration and changes it default values to the given ones.

See the applyOptionsHTTP function to understand the configuration behaviour. ListenerOption functions should only be passed to ListenerHTTP constructor function NewListenerHTTP.

func HTTPServerIdleTimeout

func HTTPServerIdleTimeout(t time.Duration) ListenerOption[TimeoutsConfig]

HTTPServerIdleTimeout sets the http.Server IdleTimeout.

func HTTPServerReadHeaderTimeout

func HTTPServerReadHeaderTimeout(t time.Duration) ListenerOption[TimeoutsConfig]

HTTPServerReadHeaderTimeout sets the http.Server ReadHeaderTimeout.

func HTTPServerReadTimeout

func HTTPServerReadTimeout(t time.Duration) ListenerOption[TimeoutsConfig]

HTTPServerReadTimeout sets the http.Server ReadTimeout.

func HTTPServerWriteTimeout

func HTTPServerWriteTimeout(t time.Duration) ListenerOption[TimeoutsConfig]

HTTPServerWriteTimeout sets the http.Server WriteTimeout.

func HealthCheckAccessLog

func HealthCheckAccessLog(enable bool) ListenerOption[HealthConfig]

HealthCheckAccessLog represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.accessLogsEnabled to true.

func HealthCheckMetricsForEndpoint

func HealthCheckMetricsForEndpoint(enable bool) ListenerOption[HealthConfig]

HealthCheckMetricsForEndpoint represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.metricsForEndpointEnabled to true.

func HealthCheckRoute

func HealthCheckRoute(route string) ListenerOption[HealthConfig]

HealthCheckRoute represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.route.

func HealthChecker

func HealthChecker(checker hc.HealthChecker) ListenerOption[HealthConfig]

HealthChecker represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.healthChecker.

func MetricsAccessLog

func MetricsAccessLog(enable bool) ListenerOption[MetricsConfig]

MetricsAccessLog represents an optional function for WithMetrics function. If passed to the WithMetrics, will set the ServerSettings.health.accessLogsEnabled to true.

func MetricsMetricsForEndpoint

func MetricsMetricsForEndpoint(enable bool) ListenerOption[MetricsConfig]

MetricsMetricsForEndpoint represents an optional function for WithMetrics function. If passed to the WithMetrics, will set the ServerSettings.health.metricsForEndpointEnabled to true.

func MetricsRoute

func MetricsRoute(route string) ListenerOption[MetricsConfig]

MetricsRoute represents an optional function for WithMetrics function. If passed to the WithMetrics, will set the ServerSettings.health.route.

func NewListenerOption added in v0.2.15

func NewListenerOption[T ListenerOptionConstraint](options ...ListenerOption[T]) []ListenerOption[T]

NewListenerOption returns a slice of ListenerOption[T].

func WithGlobalMiddlewares

func WithGlobalMiddlewares(middlewares ...Middleware) ListenerOption[ListenerConfig]

WithGlobalMiddlewares sets given middlewares as router-wide middlewares. Means that they will be applied to each server endpoint.

func WithHTTPServerTimeouts

func WithHTTPServerTimeouts(options ...ListenerOption[TimeoutsConfig]) ListenerOption[ListenerConfig]

WithHTTPServerTimeouts configures the HTTP listener TimeoutsConfig. Receives the following option to configure the endpoint: - HTTPServerReadHeaderTimeout - sets the http.Server ReadHeaderTimeout. - HTTPServerReadTimeout - sets the http.Server ReadTimeout. - HTTPServerWriteTimeout - sets the http.Server WriteTimeout. - HTTPServerIdleTimeout - sets the http.Server IdleTimeout.

func WithHealthCheck

func WithHealthCheck(options ...ListenerOption[HealthConfig]) ListenerOption[ListenerConfig]

WithHealthCheck turns on the health check endpoint. Receives the following option to configure the endpoint: - HealthChecker - to change the healthChecker implementation. - HealthCheckRoute - to set the endpoint route. - HealthCheckAccessLog - to enable access log for endpoint. - HealthCheckMetricsForEndpoint - to enable metrics collection for endpoint.

func WithLogger

func WithLogger(logger *slog.Logger) ListenerOption[ListenerConfig]

WithLogger sets the server logger.

func WithMetrics

WithMetrics turns on the metrics endpoint. Receives the following option to configure the endpoint: - MetricsRoute - to set the endpoint route. - MetricsAccessLog - to enable access log for endpoint. - MetricsMetricsForEndpoint - to enable metrics collection for endpoint.

func WithProfiler

func WithProfiler(cfg PPROFConfig) ListenerOption[ListenerConfig]

WithProfiler turns on the profiler endpoint.

func WithTLS

func WithTLS(cert, key string) ListenerOption[ListenerConfig]

WithTLS sets the TLS certificate and key to be used by the HTTP server. The certificate and key must be provided as strings containing the file paths. Note that this function is an ListenerOption for ListenerConfig and should be passed to the NewServer constructor.

type ListenerOptionConstraint added in v0.2.15

type ListenerOptionConstraint interface {
	ListenerConfig | TimeoutsConfig | HealthConfig | MetricsConfig | PPROFConfig
}

ListenerOptionConstraint represents a constraint for generic types that are related to ListenerOption.

type MetricsConfig added in v0.2.15

type MetricsConfig struct {
	// contains filtered or unexported fields
}

MetricsConfig represents configuration for builtin metrics route.

type Middleware added in v0.2.7

type Middleware = func(next http.Handler) http.Handler

Middleware represents a function type that serves as a middleware in an HTTP server. It takes the next http.Handler as a parameter and returns an http.Handler. The middleware function is responsible for intercepting and processing HTTP requests and responses.

func CORSMiddleware added in v0.2.7

func CORSMiddleware(o cors.Options) Middleware

func LoggingMiddleware added in v0.2.7

func LoggingMiddleware(logger *slog.Logger) Middleware

LoggingMiddleware represents logging middleware.

func MetricsMiddleware added in v0.2.7

func MetricsMiddleware() Middleware

MetricsMiddleware represents HTTP metrics collecting middlewares.

func RecoveryMiddleware added in v0.2.7

func RecoveryMiddleware() Middleware

func RedirectSlashesMiddleware added in v0.2.7

func RedirectSlashesMiddleware() Middleware

type PPROFConfig added in v0.2.15

type PPROFConfig struct {
	// contains filtered or unexported fields
}

PPROFConfig represents configuration for builtin profiler route.

type TimeoutsConfig added in v0.2.15

type TimeoutsConfig struct {
	// contains filtered or unexported fields
}

TimeoutsConfig holds an HTTP server TimeoutsConfig configuration.

Jump to

Keyboard shortcuts

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