-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_without_hystrix.go
50 lines (40 loc) · 1015 Bytes
/
main_without_hystrix.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", logger(HandleSubsystem))
fmt.Println("==> Main server is started")
log.Println("listening on :8080")
http.ListenAndServe(":8080", nil)
}
// HandleSubsystem send request to sub-system and extracts its response
func HandleSubsystem(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
resp, err := http.Get("http://localhost:9090")
if err != nil {
log.Println("failed to get response from sub-system:", err.Error())
return
}
log.Println("success to get response from sub-system")
w.WriteHeader(http.StatusOK)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
// Should not reach here
panic(err)
}
w.Write(b)
}
// log is Handler wrapper function for logging
func logger(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path, r.Method)
fn(w, r)
}
}