1package main 2 3import ( 4 "io/ioutil" 5 "fmt" 6 "log" 7 "net/http" 8) 9 10var ( 11 hostname string 12 port = "8080" 13) 14 15 16func serveID(w http.ResponseWriter, r *http.Request) { 17 fmt.Fprintf(w, hostname) 18} 19 20func startServer() { 21 raw, err := ioutil.ReadFile("/etc/hostname") 22 if err != nil { 23 log.Fatal("Fail to read hostname") 24 } 25 hostname = string(raw) 26 http.HandleFunc("/serverid", serveID) 27 http.HandleFunc("/hello", serveID) 28 log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil)) 29} 30 31 32func main() { 33 startServer() 34} 35 36