This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (54 loc) · 1.67 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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/cloudfoundry-community/go-cfenv"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
)
func vhostDefinitions(vhost string) string {
appEnv, _ := cfenv.Current()
sharedService, err := appEnv.Services.WithName("shared-instance-admin")
if err != nil {
return fmt.Sprintf("{\"error\": \"Cannot find shared service credentials: %s\"}", err)
}
url, _ := sharedService.CredentialString("URL")
username, _ := sharedService.CredentialString("username")
password, _ := sharedService.CredentialString("password")
tlsConfig := new(tls.Config)
transport := &http.Transport{TLSClientConfig: tlsConfig}
rmqc, _ := rabbithole.NewTLSClient(url, username, password, transport)
definitions, err := rmqc.ListVhostDefinitions(vhost)
if err != nil {
return fmt.Sprintf("{\"error\": \"Cannot retrieve the definitions: %s\"}", err)
}
definitionsJSON, _ := json.Marshal(definitions)
return string(definitionsJSON)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
vhosts, ok := r.URL.Query()["vhost"]
if !ok {
appEnv, _ := cfenv.Current()
fmt.Fprintf(w, "Please specify `vhost` in the URL. For example: https://%s/?vhost=example\n", appEnv.ApplicationURIs[0])
return
}
vhost, err := url.QueryUnescape(vhosts[0])
if err != nil || len(vhost) < 1 {
fmt.Fprintln(w, "Incorrect vhost")
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, vhostDefinitions(vhost))
}
func main() {
http.HandleFunc("/", IndexHandler)
var port string
if port = os.Getenv("PORT"); len(port) == 0 {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}