forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_service.go
58 lines (51 loc) · 1.33 KB
/
test_service.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
52
53
54
55
56
57
58
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var (
port = os.Getenv("PORT")
_, amITheProxy = os.LookupEnv("AM_I_THE_PROXY")
hostname, _ = os.Hostname()
defaultResponse = fmt.Sprintf("%s:%s", hostname, port)
)
func returnHostAndPortHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request [%v] returning [%s]", r, response())
fmt.Fprintln(w, response())
}
func callOtherServiceHandler(w http.ResponseWriter, r *http.Request) {
url := r.FormValue("url")
log.Printf("Got request [%v] making HTTP call to [%s]", r, url)
downstreamResp, err := http.Get(url)
if err != nil {
http.Error(w, err.Error(), 500)
} else {
body, err := ioutil.ReadAll(downstreamResp.Body)
if err != nil {
http.Error(w, err.Error(), 500)
} else {
response := fmt.Sprintf("me:[%s]downstream:[%s]", response(), strings.TrimSpace(string(body)))
fmt.Fprintln(w, response)
}
}
}
func response() string {
if amITheProxy {
return "proxy"
} else {
return defaultResponse
}
}
func main() {
fmt.Printf("Starting stub HTTP server on port [%s] will serve [%s] proxy [%t]", port, hostname, amITheProxy)
http.HandleFunc("/", returnHostAndPortHandler)
http.HandleFunc("/call", callOtherServiceHandler)
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
panic(err)
}
}