forked from cloudfoundry-incubator/cflocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (62 loc) · 1.72 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
type vcapService struct {
Name string `json:"name"`
Credentials map[string]interface{} `json:"credentials"`
}
func main() {
contents, err := ioutil.ReadFile("file")
if err != nil {
os.Exit(1)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Path: %s", html.EscapeString(r.URL.Path))
})
http.HandleFunc("/file", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", contents)
})
http.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.Join(os.Environ(), "\n"))
})
http.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
vcapServices := map[string][]vcapService{}
if err := json.Unmarshal([]byte(os.Getenv("VCAP_SERVICES")), &vcapServices); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s\n", err)
return
}
for _, services := range vcapServices {
for _, service := range services {
uri := service.Credentials["uri"].(string)
fmt.Fprintf(w, "Name: %s\nURI: %s\n", service.Name, uri)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Fprintf(w, "Error: %s\n\n", err)
continue
}
req.Host = service.Credentials["host_header"].(string)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Fprintf(w, "Error: %s\n\n", err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(w, "Error: %s\n\n", err)
continue
}
fmt.Fprintf(w, "Response: %s\n\n", body)
}
}
})
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}