Created
July 11, 2019 09:19
-
-
Save yfuruyama/e7db7b98c376edf136e20dd6f5bf9c00 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/hostname", nil) | |
req.Header.Add("Metadata-Flavor", "Google") | |
if err != nil { | |
log.Println(err) | |
fmt.Fprintf(w, "Error\n") | |
return | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Println(err) | |
fmt.Fprintf(w, "Error\n") | |
return | |
} | |
defer resp.Body.Close() | |
b, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Println(err) | |
fmt.Fprintf(w, "Error\n") | |
return | |
} | |
fmt.Fprintf(w, "NodeHostname: %s\n", string(b)) | |
}) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment