Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created July 11, 2019 09:19
Show Gist options
  • Save yfuruyama/e7db7b98c376edf136e20dd6f5bf9c00 to your computer and use it in GitHub Desktop.
Save yfuruyama/e7db7b98c376edf136e20dd6f5bf9c00 to your computer and use it in GitHub Desktop.
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