Documentation
¶
Overview ¶
Package ipify provides a small client to resolve the current instance public IP address using the ipify service (https://www.ipify.org/).
Problem ¶
Services running behind NAT, cloud load balancers, or dynamic outbound egress often need to discover their externally visible IP address at runtime (for allow-list updates, diagnostics, or registration workflows). Implementing this from scratch requires HTTP setup, timeout handling, status validation, and error fallback logic.
Solution ¶
This package wraps those concerns in a focused Client API:
- New creates a configurable ipify client.
- Client.GetPublicIP performs the request and returns the resolved IP.
The default configuration uses:
- endpoint: https://api.ipify.org
- timeout: 4 seconds
Use WithURL, WithTimeout, WithHTTPClient, and WithErrorIP to adapt the client to custom endpoints, transport stacks, and fallback policies.
Error Fallback Behavior ¶
When request creation, transport, status-code validation, or body reading fails, Client.GetPublicIP returns the configured error-IP value together with the error. By default the fallback string is empty, but it can be set (for example to "0.0.0.0") via WithErrorIP.
IPv6 Note ¶
The default endpoint resolves standard ipify behavior. To force IPv6-capable resolution, point the client to `https://api64.ipify.org` via WithURL.
Benefits ¶
ipify gives applications a minimal, testable, and timeout-safe way to discover public IP information without duplicating HTTP boilerplate.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client resolves public IP addresses through an ipify-compatible endpoint.
type HTTPClient ¶
HTTPClient is the minimal HTTP transport contract used by Client.
type Option ¶
type Option func(c *Client)
Option is the interface that allows to set client options.
func WithErrorIP ¶
WithErrorIP sets the fallback IP string returned on failures.
func WithHTTPClient ¶
func WithHTTPClient(hc HTTPClient) Option
WithHTTPClient injects a custom HTTP client implementation.
This is useful for testing, tracing, proxies, or custom transports.
func WithTimeout ¶
WithTimeout sets the request timeout used by GetPublicIP.
func WithURL ¶
WithURL sets the ipify service endpoint URL.
For IPv6-aware responses, use https://api64.ipify.org.