forked from IBM/CodeEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.go
45 lines (37 loc) · 1.07 KB
/
frontend.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// Get our namespace (aka Code Engine project)
ns := ""
dir := "/var/run/secrets/kubernetes.io/serviceaccount"
if buf, err := ioutil.ReadFile(dir + "/namespace"); err != nil {
panic(fmt.Sprintf("Can't read namespace: %s\n", err))
} else {
ns = string(buf)
}
// Process incoming http request
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Calc backend URL and GET it
backend := fmt.Sprintf("http://backend.%s.svc.cluster.local", ns)
res, err := http.Get(backend)
if err != nil || res.StatusCode/100 != 2 {
w.WriteHeader(http.StatusInternalServerError)
if err != nil {
fmt.Fprintf(w, "Error reaching backend: %s\n", err)
} else {
fmt.Fprintf(w, "Error reaching backend: %s\n", res.Status)
}
return
}
body, _ := ioutil.ReadAll(res.Body)
// Return nice response
fmt.Fprint(w, "Hello from the front-end\n")
fmt.Fprintf(w, "The backend says: %s\n", string(body))
})
fmt.Printf("Listening on port 8080\n")
http.ListenAndServe(":8080", nil)
}