Created
February 10, 2018 01:37
-
-
Save DazWilkin/ef5add5c0a9ac45a70d0ecbd91dd9bf9 to your computer and use it in GitHub Desktop.
github.com/salrashid123/istio_helloworld/blob/master/nodeapp/app.js in Golang
This file contains hidden or 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" | |
| "os" | |
| ) | |
| const ( | |
| backendServiceName = "be.default.svc.cluster.local" | |
| ) | |
| func main() { | |
| http.HandleFunc("/", defaultHandler) | |
| http.HandleFunc("/_ah/health", healthzHandler) | |
| http.HandleFunc("/backend", backendHandler) | |
| http.HandleFunc("/healthz", healthzHandler) | |
| http.HandleFunc("/hostz", hostzHandler) | |
| http.HandleFunc("/requestz", requestzHandler) | |
| http.HandleFunc("/varz", varzHandler) | |
| http.HandleFunc("/version", versionHandler) | |
| http.ListenAndServe(":8080", nil) | |
| } | |
| func defaultHandler(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, "<html><body>") | |
| fmt.Fprint(w, "<div>Hello Henry!</div>\n") | |
| fmt.Fprint(w, "[") | |
| urls := []string{ | |
| "/", | |
| "/_ah/health", | |
| "/backend", | |
| "/healthz", | |
| "/hostz", | |
| "/requestz", | |
| "/varz", | |
| "/version"} | |
| for i, url := range urls { | |
| if i != 0 { | |
| fmt.Fprint(w, ",") | |
| } | |
| fmt.Fprintf(w, "<a href=\"%v\">%v</a>\n", url, url) | |
| } | |
| fmt.Fprint(w, "]") | |
| fmt.Fprint(w, "</body></html>") | |
| } | |
| func backendHandler(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintf(w, "pod: [%v]; node: [%v]", os.Getenv("MY_POD_NAME"), os.Getenv("MY_NODE_NAME")) | |
| } | |
| func healthzHandler(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, "OK") | |
| } | |
| func hostzHandler(w http.ResponseWriter, r *http.Request) { | |
| url := fmt.Sprintf("http://%v:%v/%v", backendServiceName, "8080", "backend") | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| log.Fatalf("%s", err) | |
| } | |
| defer resp.Body.Close() | |
| body, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| log.Fatalf("%s", err) | |
| } | |
| fmt.Fprintf(w, "url: %v, body: %v", url, string(body)) | |
| } | |
| func requestzHandler(w http.ResponseWriter, r *http.Request) { | |
| urls := []string{ | |
| "https://www.cnn.com", | |
| "https://www.cnn.com/robots.txt", | |
| "http://www.bbc.com/robots.txt"} | |
| for _, url := range urls { | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| } | |
| defer resp.Body.Close() | |
| fmt.Fprintf(w, "%v: %v\n", url, resp.StatusCode) | |
| } | |
| } | |
| func varzHandler(w http.ResponseWriter, r *http.Request) { | |
| for _, e := range os.Environ() { | |
| fmt.Fprintf(w, "%v\n", e) | |
| } | |
| } | |
| func versionHandler(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprint(w, os.Getenv("VER")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment