-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
186 lines (162 loc) · 5.04 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"net/http"
"os"
"strings"
cfenv "github.com/cloudfoundry-community/go-cfenv"
"github.com/garyburd/redigo/redis"
"github.com/go-martini/martini"
"github.com/martini-contrib/gorelic"
pez "github.com/pivotalservices/pezauth/service"
"github.com/xchapter7x/cloudcontroller-client"
"gopkg.in/mgo.v2"
)
func main() {
appEnv, _ := cfenv.Current()
m := martini.Classic()
newRelic := new(myNewRelic).New(appEnv)
gorelic.InitNewrelicAgent(newRelic.Key, newRelic.App, true)
m.Use(gorelic.Handler)
oauth2Client := new(myOAuth2).New(appEnv)
pez.ClientID = oauth2Client.ID
pez.ClientSecret = oauth2Client.Secret
rds := new(myRedis).New(appEnv)
defer rds.Conn.Close()
pez.InitSession(m, &redisCreds{
pass: rds.Pass,
uri: rds.URI,
})
h := new(myHeritage).New(appEnv)
heritageClient := &heritage{
Client: ccclient.New(h.LoginTarget, h.LoginUser, h.LoginPass, new(http.Client)),
ccTarget: h.CCTarget,
}
mngo := new(myMongo).New(appEnv)
defer mngo.Session.Close()
if _, err := heritageClient.Login(); err == nil {
pez.InitRoutes(m, rds.Conn, mngo.Col, heritageClient)
m.Run()
} else {
panic(fmt.Sprintf("heritage client login error: %s", err.Error()))
}
}
type redisCreds struct {
pass string
uri string
}
func (s *redisCreds) Pass() string {
return s.pass
}
func (s *redisCreds) Uri() string {
return s.uri
}
type heritage struct {
*ccclient.Client
ccTarget string
}
func (s *heritage) CCTarget() string {
return s.ccTarget
}
type (
myOAuth2 struct {
ID string
Secret string
}
myRedis struct {
URI string
Pass string
Conn redis.Conn
}
myMongo struct {
Col *mgo.Collection
Session *mgo.Session
}
myHeritage struct {
LoginTarget string
LoginUser string
LoginPass string
CCTarget string
}
myNewRelic struct {
Key string
App string
}
)
func (s *myOAuth2) New(appEnv *cfenv.App) *myOAuth2 {
oauth2ServiceName := os.Getenv("OAUTH2_SERVICE_NAME")
clientIdName := os.Getenv("OAUTH2_CLIENT_ID")
clientSecretName := os.Getenv("OAUTH2_CLIENT_SECRET")
oauth2Service, err := appEnv.Services.WithName(oauth2ServiceName)
if err != nil {
panic(fmt.Sprintf("oauth2 client service name error: %s", err.Error()))
}
s.ID = oauth2Service.Credentials[clientIdName]
s.Secret = oauth2Service.Credentials[clientSecretName]
return s
}
func (s *myHeritage) New(appEnv *cfenv.App) *myHeritage {
heritageAdminServiceName := os.Getenv("UPS_PEZ_HERITAGE_ADMIN_NAME")
heritageLoginTargetName := os.Getenv("HERITAGE_LOGIN_TARGET_NAME")
heritageLoginUserName := os.Getenv("HERITAGE_LOGIN_USER_NAME")
heritageLoginPassName := os.Getenv("HERITAGE_LOGIN_PASS_NAME")
heritageCCTargetName := os.Getenv("HERITAGE_CC_TARGET_NAME")
heritageAdminService, err := appEnv.Services.WithName(heritageAdminServiceName)
if err != nil {
panic(fmt.Sprintf("heritage service name error: %s", err.Error()))
}
s.LoginTarget = heritageAdminService.Credentials[heritageLoginTargetName]
s.LoginUser = heritageAdminService.Credentials[heritageLoginUserName]
s.LoginPass = heritageAdminService.Credentials[heritageLoginPassName]
s.CCTarget = heritageAdminService.Credentials[heritageCCTargetName]
return s
}
func (s *myNewRelic) New(appEnv *cfenv.App) *myNewRelic {
serviceName := os.Getenv("NEWRELIC_SERVICE_NAME")
keyName := os.Getenv("NEWRELIC_KEY_NAME")
appName := os.Getenv("NEWRELIC_APP_NAME")
service, err := appEnv.Services.WithName(serviceName)
if err != nil {
panic(fmt.Sprintf("new relic service name error: %s", err.Error()))
}
s.Key = service.Credentials[keyName]
s.App = service.Credentials[appName]
return s
}
func (s *myMongo) New(appEnv *cfenv.App) *myMongo {
mongoServiceName := os.Getenv("MONGO_SERVICE_NAME")
mongoURIName := os.Getenv("MONGO_URI_NAME")
mongoCollName := os.Getenv("MONGO_COLLECTION_NAME")
mongoService, err := appEnv.Services.WithName(mongoServiceName)
if err != nil {
panic(fmt.Sprintf("mongodb service name error: %s", err.Error()))
}
mongoConnectionURI := mongoService.Credentials[mongoURIName]
parsedURI := strings.Split(mongoConnectionURI, "/")
mongoDBName := parsedURI[len(parsedURI)-1]
if s.Session, err = mgo.Dial(mongoConnectionURI); err != nil {
panic(fmt.Sprintf("mongodb dial error: %s", err.Error()))
}
s.Session.SetMode(mgo.Monotonic, true)
s.Col = s.Session.DB(mongoDBName).C(mongoCollName)
return s
}
func (s *myRedis) New(appEnv *cfenv.App) *myRedis {
redisName := os.Getenv("REDIS_SERVICE_NAME")
redisHost := os.Getenv("REDIS_HOSTNAME_NAME")
redisPass := os.Getenv("REDIS_PASSWORD_NAME")
redisPort := os.Getenv("REDIS_PORT_NAME")
redisService, err := appEnv.Services.WithName(redisName)
s.Pass = redisService.Credentials[redisPass]
s.URI = fmt.Sprintf("%s:%s", redisService.Credentials[redisHost], redisService.Credentials[redisPort])
if err != nil {
panic(fmt.Sprintf("redis service name error: %s", err.Error()))
}
if s.Conn, err = redis.Dial("tcp", s.URI); err != nil {
panic(fmt.Sprintf("redis dial error: %s", err.Error()))
}
if _, err = s.Conn.Do("AUTH", s.Pass); err != nil {
panic(fmt.Sprintf("redis auth error: %s", err.Error()))
}
return s
}