Documentation
¶
Index ¶
- Constants
- Variables
- func Module() fx.Option
- type Config
- type Factory
- type Option
- func WithIdleConnTimeout(t time.Duration) Option
- func WithMaxIdleConns(n int) Option
- func WithMaxIdleConnsPerHost(n int) Option
- func WithProxyURL(rawURL string, bypass string) Option
- func WithRootCAFile(path string) Option
- func WithRootCAPEM(pem string) Option
- func WithRootCAReplaceSystem(replace bool) Option
- func WithTimeout(t time.Duration) Option
- type TLSConfig
Constants ¶
const ModuleName = "httpfx"
Variables ¶
var ( // ErrInvalidConfig is returned when the HTTP client configuration is invalid. ErrInvalidConfig = errors.New("invalid config") // ErrInvalidProxyURL is returned when the proxy URL cannot be parsed. ErrInvalidProxyURL = errors.New("invalid proxy URL") // ErrProxyDialFailed is returned when the proxy dialer cannot be created. ErrProxyDialFailed = errors.New("proxy dialer creation failed") // ErrCertPoolFailed is returned when a root CA file cannot be read or the // root CA certificate pool cannot be built. ErrCertPoolFailed = errors.New("root CA cert pool creation failed") // ErrEmptyCertPEM is returned when PEM data contains no valid certificates. ErrEmptyCertPEM = errors.New("no valid certificates found in PEM data") )
Functions ¶
Types ¶
type Config ¶
type Config struct {
// ProxyURL is an explicit SOCKS5 proxy URL (e.g., "socks5://user:pass@host:port").
// Empty means inherit [http.DefaultTransport]'s value.
ProxyURL string
// Bypass is a comma-separated list of hosts that bypass the proxy
// (e.g., "localhost,127.0.0.1").
Bypass string
// Timeout is the HTTP client-level timeout. Zero means inherit
// [http.DefaultClient]'s timeout (none).
Timeout time.Duration
// MaxIdleConns is the maximum number of idle (keep-alive) connections.
// Zero means inherit [http.DefaultTransport]'s value.
MaxIdleConns int
// MaxIdleConnsPerHost is the maximum idle connections per host.
// Zero means inherit [http.DefaultTransport]'s value.
MaxIdleConnsPerHost int
// IdleConnTimeout is the maximum time an idle connection is kept alive.
// Zero means inherit [http.DefaultTransport]'s value.
IdleConnTimeout time.Duration
// TLS configures root CA trust for TLS connections; see [TLSConfig].
// The zero value keeps the default behavior (system certificate pool only).
TLS TLSConfig
}
Config holds the HTTP client configuration.
type Factory ¶
type Factory interface {
// NewClient creates a new [http.Client] using the factory's base configuration,
// with optional per-client overrides via [Option]. It returns an error when
// the resulting configuration is invalid (e.g. unusable root CA settings).
NewClient(opts ...Option) (*http.Client, error)
}
Factory creates http.Client instances with shared proxy and transport configuration.
func NewFactory ¶
NewFactory creates a new Factory from the provided configuration.
type Option ¶
type Option func(*clientOptions)
Option configures per-client overrides on Factory.NewClient.
func WithIdleConnTimeout ¶
WithIdleConnTimeout overrides the idle connection timeout for this client.
func WithMaxIdleConns ¶
WithMaxIdleConns overrides the maximum idle connections for this client.
func WithMaxIdleConnsPerHost ¶
WithMaxIdleConnsPerHost overrides the maximum idle connections per host for this client.
func WithProxyURL ¶
WithProxyURL overrides the SOCKS5 proxy URL for this client.
func WithRootCAFile ¶ added in v0.1.0
WithRootCAFile overrides the root CA certificate file path for this client. The file must contain PEM-encoded certificates; all of them are added to the trust pool. An empty path clears the base configuration's CA file.
func WithRootCAPEM ¶ added in v0.1.0
WithRootCAPEM overrides the inline PEM-encoded root CA data for this client. Multiple certificates in one string are all added to the trust pool. An empty string clears the base configuration's CA PEM data.
func WithRootCAReplaceSystem ¶ added in v0.1.0
WithRootCAReplaceSystem overrides whether this client replaces the system certificate pool instead of appending to it. When true, only the root CAs configured for this client are trusted.
func WithTimeout ¶
WithTimeout overrides the client-level timeout for this client.
type TLSConfig ¶ added in v0.1.0
type TLSConfig struct {
// RootCAFile is a path to a PEM-encoded root CA certificate file.
// Multiple certificates in one file are all added to the pool.
RootCAFile string
// RootCAPEM is PEM-encoded root CA certificate data. Multiple
// certificates in one string are all added to the pool.
RootCAPEM string
// RootCAReplaceSystem replaces the system certificate pool instead of
// appending to it. When true, only the configured root CAs are trusted.
//
// Note: append mode relies on [x509.SystemCertPool], which returns an
// empty pool on macOS/darwin (system roots load lazily at verify time),
// so append-mode behavior can differ by platform. Replace mode always
// trusts exactly the configured CAs.
RootCAReplaceSystem bool
}
TLSConfig holds root CA trust configuration for TLS connections.