Created
May 1, 2019 16:59
-
-
Save kainlite/f7e0c3e40b02cc31a78f4eef491fa834 to your computer and use it in GitHub Desktop.
whatismyip-g
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net" | |
| "net/http" | |
| "os" | |
| "strconv" | |
| ) | |
| func httphandler(w http.ResponseWriter, r *http.Request) { | |
| ipAddress, _, _ := net.SplitHostPort(r.RemoteAddr) | |
| fmt.Fprintf(w, "%s", ipAddress) | |
| } | |
| func main() { | |
| port, err := strconv.Atoi(os.Getenv("WHATISMYIP_PORT")) | |
| if err != nil { | |
| log.Fatalf("Please make sure the environment variable WHATISMYIP_PORT is defined and is a valid integer [1024-65535], error: %s", err) | |
| } | |
| listener := fmt.Sprintf(":%d", port) | |
| http.HandleFunc("/", httphandler) | |
| log.Fatal(http.ListenAndServe(listener, nil)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment