This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
74 lines (67 loc) · 1.87 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
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
type vcapService struct {
Name string `json:"name"`
Credentials map[string]interface{} `json:"credentials"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Path: %s", html.EscapeString(r.URL.Path))
})
http.HandleFunc("/env", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, strings.Join(os.Environ(), "\n"))
})
http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
for name, arg := range r.URL.Query() {
out, err := exec.Command(name, arg...).CombinedOutput()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s\n", err)
return
}
fmt.Fprintf(w, "%s", out)
}
})
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))
}