Documentation
¶
Index ¶
- func Mount(mux Multiplexer, pattern string, handler http.Handler)
- func WithMiddleware(handler http.Handler, ffns ...MiddlewareFactoryFunc) http.Handler
- func WithRoundTripper(transport http.RoundTripper, rts ...RoundTripperFactoryFunc) http.RoundTripper
- type AccessLog
- type MiddlewareFactoryFunc
- type Multiplexer
- type RetryRoundTripper
- type RoundTripperFactoryFunc
- type RoundTripperFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Mount ¶ added in v0.187.0
func Mount(mux Multiplexer, pattern string, handler http.Handler)
Mount will help to register a handler on a request multiplexer in both as the concrete path to the handler and as a prefix match. example:
if pattern -> "/something" registered as "/something" for exact match registered as "/something/" for prefix match
Example ¶
package main import ( "net/http" "go.llib.dev/frameless/pkg/httpkit" ) func main() { var ( apiV0 http.Handler webUI http.Handler mux = http.NewServeMux() ) httpkit.Mount(mux, "/api/v0", apiV0) httpkit.Mount(mux, "/ui", webUI) }
func WithMiddleware ¶ added in v0.211.0
func WithMiddleware(handler http.Handler, ffns ...MiddlewareFactoryFunc) http.Handler
WithMiddleware will combine an http.Handler with a stack of middleware factory functions. The order in which you pass the MiddlewareFactoryFunc -s is the same as the order, they will be called during the http.Handler.ServeHTTP method call.
func WithRoundTripper ¶ added in v0.216.0
func WithRoundTripper(transport http.RoundTripper, rts ...RoundTripperFactoryFunc) http.RoundTripper
WithRoundTripper will combine an http.RoundTripper with a stack of middleware factory functions. The execution order is in which you pass the factory funcs.
Example ¶
package main import ( "net/http" "go.llib.dev/frameless/pkg/httpkit" ) func main() { transport := httpkit.WithRoundTripper(nil, func(next http.RoundTripper) http.RoundTripper { return httpkit.RoundTripperFunc(func(request *http.Request) (*http.Response, error) { request.Header.Set("Authorization", "<type> <credentials>") return next.RoundTrip(request) }) }) _ = &http.Client{ Transport: transport, } }
Types ¶
type AccessLog ¶
type MiddlewareFactoryFunc ¶ added in v0.211.0
MiddlewareFactoryFunc is a constructor function that is meant to wrap an http.Handler with given middleware. Its http.Handler argument represents the next middleware http.Handler in the pipeline.
type Multiplexer ¶ added in v0.211.0
Multiplexer represents a http request Multiplexer.
type RetryRoundTripper ¶
type RetryRoundTripper struct { // Transport specifies the mechanism by which individual // HTTP requests are made. // // Default: http.DefaultTransport Transport http.RoundTripper // RetryStrategy will be used to evaluate if a new retry attempt should be done. // // Default: retry.ExponentialBackoff RetryStrategy retry.Strategy[retry.FailureCount] // OnStatus is an [OPTIONAL] configuration field that could contain whether a certain http status code should be retried or not. // The RetryRoundTripper has a default behaviour about which status code can be retried, and this option can override that. OnStatus map[int]bool }
Example ¶
package main import ( "net/http" "time" "go.llib.dev/frameless/pkg/httpkit" "go.llib.dev/frameless/pkg/retry" ) func main() { httpClient := http.Client{ Transport: httpkit.RetryRoundTripper{ RetryStrategy: retry.ExponentialBackoff{ // optional Timeout: 5 * time.Minute, }, Transport: http.DefaultTransport, // optional OnStatus: map[int]bool{ // optional http.StatusTeapot: true, http.StatusTooManyRequests: false, }, }, } httpClient.Get("https://go.llib.dev") }
type RoundTripperFactoryFunc ¶ added in v0.216.0
type RoundTripperFactoryFunc func(next http.RoundTripper) http.RoundTripper
RoundTripperFactoryFunc is a constructor function that is meant to wrap an http.RoundTripper with given middleware. Its http.RoundTripper argument represents the next middleware http.RoundTripper in the pipeline.