package main import ( "fmt" "io/ioutil" "log" "net" "net/http" "os" "time" ) var version string func get(c *http.Client, url string) (body string, err error) { resp, err := c.Get(url) if err != nil { return } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { return } body = string(b) return } func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) h, err := os.Hostname() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } c := &http.Client{Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 5 * time.Second, }).DialContext, }} health, err := get(c, "http://testapp-db:8080/healthz") if err != nil { health = err.Error() } ext, err := get(c, "http://httpbin.org/robots.txt") if err != nil { ext = err.Error() } fmt.Fprintf(w, "%s (%s) DB Health: '%s' external GET: '%s'", h, version, health, ext) }) http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) fmt.Fprintf(w, "api: ok") }) s := &http.Server{ ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, Addr: ":8080", } log.Fatal(s.ListenAndServe()) }