hello-openshift: Echo listening port in HTTP resp
Set the "request-port" header on hello-openshift HTTP
responses so that clients reaching the hello-openshift
application know which port the HTTP listener was accessed
on. This will be useful for the Ingress Operator fault
detection work. See
and for additional context.
@@ -2,8 +2,10 @@ package main
import (
"fmt"
+ "net"
"net/http"
"os"
+ "strconv"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
@@ -12,6 +14,13 @@ func helloHandler(w http.ResponseWriter, r *http.Request) {
response = "Hello OpenShift!"
}
+ // Echo back the port the request was received on
+ // via a "request-port" header.
+ addr := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
+ if tcpAddr, ok := addr.(*net.TCPAddr); ok {
+ w.Header().Set("request-port", strconv.Itoa(tcpAddr.Port))
+ }
+
fmt.Fprintln(w, response)
fmt.Println("Servicing request.")
}
