-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve metric endpoint reporting (#23)
- Loading branch information
Showing
7 changed files
with
267 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ipfetcher | ||
|
||
import ( | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// GetIP fetches the public IP address from icanhazip.com | ||
func GetIP() (string, error) { | ||
// Create a custom HTTP client with timeout settings | ||
client := &http.Client{ | ||
Timeout: 30 * time.Second, | ||
Transport: &http.Transport{ | ||
DialContext: (&net.Dialer{ | ||
Timeout: 10 * time.Second, | ||
}).DialContext, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
}, | ||
} | ||
|
||
// Make the GET request | ||
resp, err := client.Get("https://icanhazip.com") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Read the response body | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Trim any surrounding whitespace from the response body | ||
ip := strings.TrimSpace(string(body)) | ||
return ip, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.