Documentation
¶
Index ¶
- func ErrorHTTP(w http.ResponseWriter, r *http.Request, err error, options ...ResponseOption)
- func HTML(w http.ResponseWriter, r *http.Request, v []byte, options ...ResponseOption)
- func JSON(w http.ResponseWriter, r *http.Request, v any, options ...ResponseOption)
- func NewClient(options ...ClientOption) *http.Client
- func ProfilerMiddleware() http.Handler
- func SetHTMLTemplater(templater HTMLTemplateProvider)
- func SetHTTPErrorResponder(responder HTTPErrorResponder)
- func Status(w http.ResponseWriter, _ *http.Request, statusCode int, ...)
- func TEXT(w http.ResponseWriter, r *http.Request, v []byte, options ...ResponseOption)
- func TemplateHTML(w http.ResponseWriter, r *http.Request, name string, v any, ...)
- type ClientOption
- func WithCustomDialer(dialer CustomDialer) ClientOption
- func WithDialTimeout(timeout time.Duration) ClientOption
- func WithKeepAliveDisabled(disabled bool) ClientOption
- func WithKeepAliveTimeout(timeout time.Duration) ClientOption
- func WithMaxIdleConns(maxn int) ClientOption
- func WithMaxIdleConnsPerHost(maxn int) ClientOption
- func WithReadBufferSize(size int) ClientOption
- func WithResponseHeaderTimeout(timeout time.Duration) ClientOption
- func WithRetries(options ...retry.Option) ClientOption
- func WithTLSHandshakeTimeout(timeout time.Duration) ClientOption
- func WithWriteBufferSize(size int) ClientOption
- type Config
- type CustomDialer
- type HTMLTemplateProvider
- type HTTPErrorResponder
- type HealthConfig
- type ListenerConfig
- type ListenerHTTP
- type ListenerOption
- func HTTPServerIdleTimeout(t time.Duration) ListenerOption[TimeoutsConfig]
- func HTTPServerReadHeaderTimeout(t time.Duration) ListenerOption[TimeoutsConfig]
- func HTTPServerReadTimeout(t time.Duration) ListenerOption[TimeoutsConfig]
- func HTTPServerWriteTimeout(t time.Duration) ListenerOption[TimeoutsConfig]
- func HealthCheckAccessLog(enable bool) ListenerOption[HealthConfig]
- func HealthCheckMetricsForEndpoint(enable bool) ListenerOption[HealthConfig]
- func HealthCheckReportHTML() ListenerOption[HealthConfig]
- func HealthCheckReportJSON() ListenerOption[HealthConfig]
- func HealthCheckRoute(route string) ListenerOption[HealthConfig]
- func HealthChecker(checker hc.HealthChecker) ListenerOption[HealthConfig]
- func MetricsAccessLog(enable bool) ListenerOption[MetricsConfig]
- func MetricsMetricsForEndpoint(enable bool) ListenerOption[MetricsConfig]
- func MetricsRoute(route string) ListenerOption[MetricsConfig]
- func NewListenerOption[T ListenerOptionConstraint](options ...ListenerOption[T]) []ListenerOption[T]
- func WithGlobalMiddlewares(middlewares ...Middleware) ListenerOption[ListenerConfig]
- func WithHTTPServerTimeouts(options ...ListenerOption[TimeoutsConfig]) ListenerOption[ListenerConfig]
- func WithHealthCheck(options ...ListenerOption[HealthConfig]) ListenerOption[ListenerConfig]
- func WithLogger(logger *slog.Logger) ListenerOption[ListenerConfig]
- func WithMetrics(options ...ListenerOption[MetricsConfig]) ListenerOption[ListenerConfig]
- func WithProfiler(cfg PPROFConfig) ListenerOption[ListenerConfig]
- func WithTLS(cert, key string) ListenerOption[ListenerConfig]
- type ListenerOptionConstraint
- type MetricsConfig
- type Middleware
- type PPROFConfig
- type ResponseOption
- type ResponseOptions
- type TimeoutsConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorHTTP ¶ added in v0.3.0
func ErrorHTTP(w http.ResponseWriter, r *http.Request, err error, options ...ResponseOption)
ErrorHTTP tries to map err to errkit.Error and based on result writes standard HTTP error with status statusCode to the response writer.
func HTML ¶ added in v0.3.0
func HTML(w http.ResponseWriter, r *http.Request, v []byte, options ...ResponseOption)
HTML tries to encode v into json representation and write it to response writer.
func JSON ¶ added in v0.3.0
func JSON(w http.ResponseWriter, r *http.Request, v any, options ...ResponseOption)
JSON tries to encode v into json representation and write it to response writer.
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 SetHTMLTemplater ¶ added in v0.3.0
func SetHTMLTemplater(templater HTMLTemplateProvider)
SetHTMLTemplater sets the given templater as htmlTemplater.
func SetHTTPErrorResponder ¶ added in v0.3.0
func SetHTTPErrorResponder(responder HTTPErrorResponder)
SetHTTPErrorResponder sets the given responder as errHTTPResponder.
func Status ¶ added in v0.3.0
func Status(w http.ResponseWriter, _ *http.Request, statusCode int, options ...ResponseOption)
Status writes an HTTP status to the w http.ResponseWriter.
func TEXT ¶ added in v0.3.0
func TEXT(w http.ResponseWriter, r *http.Request, v []byte, options ...ResponseOption)
TEXT tries to write v to response writer.
func TemplateHTML ¶ added in v0.3.0
func TemplateHTML( w http.ResponseWriter, r *http.Request, name string, v any, options ...ResponseOption, )
TemplateHTML generates an HTML template response for the given name and data. It sets the Content-Type header to "text/html; charset=utf-8" and writes the response with the specified status code. If the template execution fails, an error will be logged and a 500 Internal Server Error response will be sent. Additional options can be passed to modify the response using the Option functions.
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 DisableKeepAlives value to underlying http.Transport of http.Client.
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(maxn int) ClientOption
WithMaxIdleConns sets the MaxIdleConns value to underlying http.Transport of http.Client.
func WithMaxIdleConnsPerHost ¶ added in v0.2.8
func WithMaxIdleConnsPerHost(maxn 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 HTMLTemplateProvider ¶ added in v0.3.0
type HTMLTemplateProvider interface { // Template renders the HTML templates by given name. Template(ctx context.Context, name string) (*template.Template, error) }
HTMLTemplateProvider wraps a Template method to render requested HTML template.
type HTTPErrorResponder ¶ added in v0.3.0
type HTTPErrorResponder func(w http.ResponseWriter, err error, options ...ResponseOption)
HTTPErrorResponder represents a function which should be called to respond with an error on HTTP call.
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))
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 HealthCheckReportHTML ¶ added in v0.2.43
func HealthCheckReportHTML() ListenerOption[HealthConfig]
HealthCheckReportHTML represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.healthReport to healthReportHTML.
func HealthCheckReportJSON ¶ added in v0.2.43
func HealthCheckReportJSON() ListenerOption[HealthConfig]
HealthCheckReportJSON represents an optional function for WithHealthCheck function. If passed to the WithHealthCheck, will set the ServerSettings.health.healthReport to healthReportJSON.
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 ¶
func WithMetrics(options ...ListenerOption[MetricsConfig]) ListenerOption[ListenerConfig]
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
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 ResponseOption ¶ added in v0.3.0
type ResponseOption func(o *ResponseOptions)
ResponseOption represents a function type that modifies ResponseOptions for an HTTP response.
func WithErrorReport ¶ added in v0.3.0
func WithErrorReport() ResponseOption
WithErrorReport is an Option function that will enable error reporting by the errkit.ErrorReporter. Used in the default implementation of HTTPErrorResponder.
func WithHeader ¶ added in v0.3.0
func WithHeader(key, value string) ResponseOption
WithHeader is an Option function that adds the given key-value pair to the headers of the ResponseOptions. The headers are used to modify the headers of an HTTP response.
func WithStatus ¶ added in v0.3.0
func WithStatus(code int) ResponseOption
WithStatus sets the given status code as the statusCode field of the ResponseOptions parameter.
type ResponseOptions ¶ added in v0.3.0
type ResponseOptions struct {
// contains filtered or unexported fields
}
ResponseOptions represents a set of options for an HTTP response.
func NewResponseOptions ¶ added in v0.3.0
func NewResponseOptions(w http.ResponseWriter, options ...ResponseOption) *ResponseOptions
NewResponseOptions returns a pointer to a new ResponseOptions object with default values and applies the given options to it.
type TimeoutsConfig ¶ added in v0.2.15
type TimeoutsConfig struct {
// contains filtered or unexported fields
}
TimeoutsConfig holds an HTTP server TimeoutsConfig configuration.