Documentation
¶
Overview ¶
Package ipify provides a small client to resolve the current instance public IP address using the ipify service (https://www.ipify.org/).
The Client API:
- New creates a configurable ipify client.
- Client.GetPublicIP performs the request and returns the resolved IP.
- Client.HealthCheck probes endpoint reachability for parity with the other nurago HTTP clients.
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. A non-positive timeout is clamped to the default; an API URL that is missing, unparseable, or not http/https with a host is rejected by New.
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. The response body is whitespace-trimmed and an empty body is treated as a failure.
Errors ¶
Configuration problems are reported at construction time with errors matching the exported sentinel ErrInvalidOptions. A nil or empty response body from the endpoint surfaces as ErrInvalidResponse. Match the sentinels with errors.Is.
IPv6 Note ¶
The default endpoint resolves standard ipify behavior. To force IPv6-capable resolution, point the client to `https://api64.ipify.org` via WithURL.
When To Use ¶
- A service needs to know its own egress address, for allow-listing or for logging.
- You want a fallback value rather than an error when the lookup fails.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidOptions is returned by New when the configured API URL is // missing, unparseable, or does not use an http/https scheme with a host. ErrInvalidOptions = errors.New("ipify: missing or invalid client options") // ErrInvalidResponse is returned by GetPublicIP when the endpoint returns a // nil response body or an empty (whitespace-only) payload. ErrInvalidResponse = errors.New("ipify: invalid response") )
Sentinel errors returned by the package. Match them with errors.Is so callers can distinguish configuration problems from response failures.
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.
func New ¶
New constructs an ipify client with validated configuration.
It applies options, initializes a default HTTP client when needed, and validates the configured API URL. A non-positive timeout is silently clamped to the default rather than rejected, so a misconfigured timeout cannot make every request fail with an already-expired context. An API URL that is missing, unparseable, or that does not use an http/https scheme with a host is rejected with an error matching ErrInvalidOptions.
func (*Client) GetPublicIP ¶
GetPublicIP resolves the instance public IP through the configured ipify endpoint.
On any request or response failure, it returns the configured fallback errorIP together with the error.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/tecnickcom/nurago/pkg/ipify"
)
func main() {
// A stand-in for https://api.ipify.org, which returns the caller
// address as plain text. Real code omits WithURL and uses the default.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("192.0.2.1"))
}))
defer srv.Close()
client, err := ipify.New(
ipify.WithURL(srv.URL),
ipify.WithTimeout(2*time.Second),
ipify.WithErrorIP("0.0.0.0"), // returned instead of an empty string on failure
)
if err != nil {
fmt.Println(err)
return
}
ip, err := client.GetPublicIP(context.TODO())
fmt.Println(ip, err)
}
Output: 192.0.2.1 <nil>
func (*Client) HealthCheck ¶
HealthCheck verifies that the configured ipify endpoint is reachable and returns a usable public IP.
It performs a GetPublicIP call, discards the resolved address, and returns only the error. It exists for parity with the other nurago HTTP clients that expose a HealthCheck probe.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/tecnickcom/nurago/pkg/ipify"
)
func main() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("192.0.2.1"))
}))
defer srv.Close()
client, err := ipify.New(ipify.WithURL(srv.URL))
if err != nil {
fmt.Println(err)
return
}
// HealthCheck fits the healthcheck package's HealthChecker contract, so
// the dependency can be reported on a service /healthz endpoint.
fmt.Println(client.HealthCheck(context.TODO()))
}
Output: <nil>
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.