/
githubmirror
/
origin
Обзор
Документация
Войти
/
githubmirror
/
origin
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/hello-openshift/hello_openshift.go
51 строка
985 B
Stephen Greene
hello-openshift: Make request-port header spec compliant
20 ноя 2020, 21:20
20 ноя 2020, 21:20
36260dd
Код
Авторство
О чём код?
package main import ( "fmt" "net" "net/http" "os" "strconv" ) func helloHandler(w http.ResponseWriter, r *http.Request) { response := os.Getenv("RESPONSE") if len(response) == 0 { 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("x-request-port", strconv.Itoa(tcpAddr.Port)) } fmt.Fprintln(w, response) fmt.Println("Servicing request.") } func listenAndServe(port string) { fmt.Printf("serving on %s\n", port) err := http.ListenAndServe(":"+port, nil) if err != nil { panic("ListenAndServe: " + err.Error()) } } func main() { http.HandleFunc("/", helloHandler) port := os.Getenv("PORT") if len(port) == 0 { port = "8080" } go listenAndServe(port) port = os.Getenv("SECOND_PORT") if len(port) == 0 { port = "8888" } go listenAndServe(port) select {} }