-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathhello_openshift.go
51 lines (43 loc) · 985 Bytes
/
hello_openshift.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 {}
}