http

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: BSD-2-Clause Imports: 33 Imported by: 12

README

Http

Http server and Http client utils.

Documentation

Index

Constants

View Source
const (
	ApplicationJsonContentType = "application/json"
	TextHtml                   = "text/html"
)
View Source
const (
	ContentTypeHeaderName = "Content-Type"
)
View Source
const PreventRetryHeaderName = "X-Prevent-Retry"

Variables

View Source
var NotFound = stderrors.New("not found")

Functions

func BuildRequest

func BuildRequest(
	ctx context.Context,
	method string,
	urlString string,
	parameters url.Values,
	body io.Reader,
	header http.Header,
) (*http.Request, error)

func CheckResponseIsSuccessful

func CheckResponseIsSuccessful(req *http.Request, resp *http.Response) error

func CreateDefaultHttpClient

func CreateDefaultHttpClient() *http.Client

func CreateHttpClient

func CreateHttpClient(
	timeout time.Duration,
) *http.Client

func CreateTlsClientConfig

func CreateTlsClientConfig(ctx context.Context, caCertPath string, clientCertPath string, clientKeyPath string) (*tls.Config, error)

func FileServer

func FileServer(
	root string,
	prefix string,
) http.Handler

func IsIgnoredSentryError

func IsIgnoredSentryError(err error) bool

func IsRetryError

func IsRetryError(err error) bool

func NewBackgroundRunHandler

func NewBackgroundRunHandler(ctx context.Context, runFunc run.Func) http.Handler

func NewErrorHandler

func NewErrorHandler(withError WithError) http.Handler

func NewPrintHandler

func NewPrintHandler(format string, a ...any) http.Handler

func NewProxy

func NewProxy(
	transport http.RoundTripper,
	apiUrl *url.URL,
	proxyErrorHandler ProxyErrorHandler,
) http.Handler

func NewRoundTripperBasicAuth

func NewRoundTripperBasicAuth(
	roundTripper RoundTripper,
	username string,
	password string,
) http.RoundTripper

func NewRoundTripperHeader

func NewRoundTripperHeader(
	roundTripper http.RoundTripper,
	header http.Header,
) http.RoundTripper

func NewRoundTripperLog

func NewRoundTripperLog(tripper http.RoundTripper) http.RoundTripper

func NewRoundTripperRateLimit

func NewRoundTripperRateLimit(
	ctx context.Context,
	tripper http.RoundTripper,
	maxRequestPerInterval int64,
	intervalDurarion time.Duration,
	logSamplerFactory log.SamplerFactory,
) http.RoundTripper

func NewRoundTripperRemovePathPrefix

func NewRoundTripperRemovePathPrefix(
	roundTripper http.RoundTripper,
	prefix string,
) http.RoundTripper

func NewRoundTripperRetry

func NewRoundTripperRetry(
	roundTripper http.RoundTripper,
	retryLimit int,
	retryDelay time.Duration,
) http.RoundTripper

NewRoundTripperRetry wraps a given RoundTripper and retry the httpRequest with a delay between.

func NewServer

func NewServer(addr string, router http.Handler) run.Func

func NewServerTLS

func NewServerTLS(addr string, router http.Handler, serverCertPath string, serverKeyPath string) run.Func

func NewServerWithPort

func NewServerWithPort(port int, router http.Handler) run.Func

func NewSkipErrorWriter

func NewSkipErrorWriter(writer io.Writer) io.Writer

func NewUpdateErrorHandler added in v1.8.0

func NewUpdateErrorHandler(db libkv.DB, withErrorTx WithErrorTx) http.Handler

func NewViewErrorHandler added in v1.8.0

func NewViewErrorHandler(db libkv.DB, withErrorTx WithErrorTx) http.Handler

func RegisterPprof

func RegisterPprof(router *mux.Router)

RegisterPprof register pprof http endpoint to gorilla mux https://www.codereliant.io/memory-leaks-with-pprof/ kubectl -n erpnext port-forward service/hubspot-resource-exporter 9090:9090 go tool pprof -alloc_space http://localhost:9090/debug/pprof/heap > web or go tool pprof -http=127.0.0.1:16666 -alloc_space http://localhost:9090/debug/pprof/heap

func WriteAndGlog

func WriteAndGlog(w io.Writer, format string, a ...any) (n int, err error)

Types

type CheckRedirect

type CheckRedirect func(req *http.Request, via []*http.Request) error

type DialFunc

type DialFunc func(ctx context.Context, network, address string) (net.Conn, error)

type HasTemporaryError

type HasTemporaryError interface {
	Temporary() bool
}

type HasTimeoutError

type HasTimeoutError interface {
	Timeout() bool
}

type HttpClientBuilder

type HttpClientBuilder interface {
	WithProxy() HttpClientBuilder
	WithoutProxy() HttpClientBuilder
	WithRedirects() HttpClientBuilder
	WithoutRedirects() HttpClientBuilder
	WithTimeout(timeout time.Duration) HttpClientBuilder
	WithDialFunc(dialFunc DialFunc) HttpClientBuilder
	WithInsecureSkipVerify(insecureSkipVerify bool) HttpClientBuilder
	WithClientCert(caCertPath string, clientCertPath string, clientKeyPath string) HttpClientBuilder
	Build(ctx context.Context) (*http.Client, error)
	BuildRoundTripper(ctx context.Context) (http.RoundTripper, error)
}

func NewClientBuilder

func NewClientBuilder() HttpClientBuilder

type JsonHandler

type JsonHandler interface {
	ServeHTTP(ctx context.Context, req *http.Request) (interface{}, error)
}

type JsonHandlerFunc

type JsonHandlerFunc func(ctx context.Context, req *http.Request) (interface{}, error)

func (JsonHandlerFunc) ServeHTTP

func (j JsonHandlerFunc) ServeHTTP(ctx context.Context, req *http.Request) (interface{}, error)

type JsonHandlerTx added in v1.8.0

type JsonHandlerTx interface {
	ServeHTTP(ctx context.Context, tx libkv.Tx, req *http.Request) (interface{}, error)
}

type JsonHandlerTxFunc added in v1.8.0

type JsonHandlerTxFunc func(ctx context.Context, tx libkv.Tx, req *http.Request) (interface{}, error)

func (JsonHandlerTxFunc) ServeHTTP added in v1.8.0

func (j JsonHandlerTxFunc) ServeHTTP(ctx context.Context, tx libkv.Tx, req *http.Request) (interface{}, error)

type Proxy

type Proxy func(req *http.Request) (*url.URL, error)

type ProxyErrorHandler

type ProxyErrorHandler interface {
	HandleError(resp http.ResponseWriter, req *http.Request, err error)
}

func NewSentryProxyErrorHandler

func NewSentryProxyErrorHandler(sentryClient libsentry.Client) ProxyErrorHandler

type ProxyErrorHandlerFunc

type ProxyErrorHandlerFunc func(resp http.ResponseWriter, req *http.Request, err error)

func (ProxyErrorHandlerFunc) HandleError

func (p ProxyErrorHandlerFunc) HandleError(resp http.ResponseWriter, req *http.Request, err error)

type RequestFailedError

type RequestFailedError struct {
	Method     string
	URL        string
	StatusCode int
}

func (RequestFailedError) Error

func (r RequestFailedError) Error() string

type RoundTripper

type RoundTripper interface {
	http.RoundTripper
}

func CreateDefaultRoundTripper

func CreateDefaultRoundTripper() RoundTripper

func CreateDefaultRoundTripperTls

func CreateDefaultRoundTripperTls(ctx context.Context, caCertPath string, clientCertPath string, clientKeyPath string) (RoundTripper, error)

type RoundTripperFunc

type RoundTripperFunc func(req *http.Request) (*http.Response, error)

func (RoundTripperFunc) RoundTrip

func (r RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

type WithError

type WithError interface {
	ServeHTTP(ctx context.Context, resp http.ResponseWriter, req *http.Request) error
}

func NewCpuProfileStartHandler

func NewCpuProfileStartHandler() WithError

func NewCpuProfileStopHandler

func NewCpuProfileStopHandler() WithError

func NewFileDownloader

func NewFileDownloader(path string) WithError

func NewJsonHandler

func NewJsonHandler(jsonHandler JsonHandler) WithError

func NewJsonHandlerUpdateTx added in v1.8.0

func NewJsonHandlerUpdateTx(db libkv.DB, jsonHandler JsonHandlerTx) WithError

func NewJsonHandlerViewTx added in v1.8.0

func NewJsonHandlerViewTx(db libkv.DB, jsonHandler JsonHandlerTx) WithError

func NewMemoryProfileHandler

func NewMemoryProfileHandler() WithError

func NewProfilingStart

func NewProfilingStart() WithError

func NewProfilingStop

func NewProfilingStop() WithError

type WithErrorFunc

type WithErrorFunc func(ctx context.Context, resp http.ResponseWriter, req *http.Request) error

func (WithErrorFunc) ServeHTTP

func (w WithErrorFunc) ServeHTTP(ctx context.Context, resp http.ResponseWriter, req *http.Request) error

type WithErrorTx added in v1.8.0

type WithErrorTx interface {
	ServeHTTP(ctx context.Context, tx libkv.Tx, resp http.ResponseWriter, req *http.Request) error
}

type WithErrorTxFunc added in v1.8.0

type WithErrorTxFunc func(ctx context.Context, tx libkv.Tx, resp http.ResponseWriter, req *http.Request) error

func (WithErrorTxFunc) ServeHTTP added in v1.8.0

func (w WithErrorTxFunc) ServeHTTP(ctx context.Context, tx libkv.Tx, resp http.ResponseWriter, req *http.Request) error

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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