-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
71 lines (54 loc) · 1.39 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
package main
import (
"encoding/json"
"net/http"
"os"
"github.com/pivotal-cf/brokerapi"
"github.com/pivotal-golang/lager"
"github.com/s-matyukevich/bosh-broker/source/broker"
"github.com/s-matyukevich/bosh-broker/source/config"
)
func main() {
logger := lager.NewLogger(appName(os.Getenv("VCAP_APPLICATION"), "bosh-broker"))
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
config, err := config.ParseConfig("config.yml")
if err != nil {
logger.Fatal("config", err)
return
}
logger.Debug("config", lager.Data{"config": config})
credentials := brokerapi.BrokerCredentials{
Username: config.ServiceUser,
Password: config.ServicePassword,
}
handler, err := broker.NewHandler(config)
if err != nil {
logger.Fatal("handler", err)
}
http.Handle("/", brokerapi.New(handler, logger, credentials))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
logger.Info("boot-up", lager.Data{"port": port})
if err := http.ListenAndServe(":"+port, nil); err != nil {
logger.Fatal("listen-and-serve", err)
}
}
func appName(envJSON string, defaultName string) string {
env := struct {
ApplicationName string `json:"application_name"`
}{}
if envJSON == "" {
goto DEFAULT
}
if err := json.Unmarshal([]byte(envJSON), &env); err != nil {
panic(err)
}
if env.ApplicationName == "" {
goto DEFAULT
}
return env.ApplicationName
DEFAULT:
return defaultName
}