103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/user"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var version string
|
|
|
|
var tracer = []string{
|
|
"x-request-id",
|
|
"x-b3-traceid",
|
|
"x-b3-spanid",
|
|
"x-b3-parentspanid",
|
|
"x-b3-sampled",
|
|
"x-b3-flags",
|
|
"x-ot-span-context",
|
|
}
|
|
|
|
func trace(rd, rs *http.Request) {
|
|
if rd == nil || rs == nil {
|
|
return
|
|
}
|
|
for _, t := range tracer {
|
|
v := rs.Header.Get(t)
|
|
if v != "" {
|
|
rd.Header.Set(t, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func get(c *http.Client, req *http.Request, url string) (body string, err error) {
|
|
r, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
trace(r, req)
|
|
resp, err := c.Do(r)
|
|
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
|
|
}
|
|
u, err := user.LookupId(strconv.Itoa(os.Getuid()))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
//url := "http://" + os.Getenv("DB_SERVICE_HOST") + ":" + os.Getenv("DB_SERVICE_PORT")
|
|
//url := "http://testapp-db.default.svc.cluster.local:8080"
|
|
c := &http.Client{Transport: &http.Transport{
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 5 * time.Second,
|
|
}).DialContext,
|
|
}}
|
|
dbUrl := "http://testapp-db:8080"
|
|
dbBody, err := get(c, r, dbUrl)
|
|
if err != nil {
|
|
dbBody = err.Error()
|
|
}
|
|
apiUrl := "http://testapp-api:8080"
|
|
apiBody, err := get(c, r, apiUrl)
|
|
if err != nil {
|
|
apiBody = err.Error()
|
|
}
|
|
fmt.Fprintf(w, "Version: %s\nHostname: %s\nUser: %s (%s) (%s)\nDB URL: %s\nDB Response: %s\nAPI URL: %s\nAPI Response: %s\n",
|
|
version, h, u.Username, u.Uid, u.Gid,
|
|
dbUrl, dbBody,
|
|
apiUrl, apiBody)
|
|
})
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
})
|
|
s := &http.Server{
|
|
ReadTimeout: 5 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
Addr: ":8080",
|
|
}
|
|
log.Fatal(s.ListenAndServe())
|
|
}
|