Skip to content

Instantly share code, notes, and snippets.

@kainlite
Created May 1, 2019 16:59
Show Gist options
  • Select an option

  • Save kainlite/f7e0c3e40b02cc31a78f4eef491fa834 to your computer and use it in GitHub Desktop.

Select an option

Save kainlite/f7e0c3e40b02cc31a78f4eef491fa834 to your computer and use it in GitHub Desktop.
whatismyip-g
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