Support multiple service tags for identifying the db

This commit is contained in:
Geoff Franks 2016-11-16 13:04:45 -05:00 committed by James Hunt
parent cea80c545a
commit 1a87365c7d
2 changed files with 8 additions and 5 deletions

View File

@ -24,7 +24,7 @@ func main() {
log.Infof("genesis-index starting up")
var d *db.DB
if dsn, err := ParseVcap(os.Getenv("VCAP_SERVICES"), "postgres", "uri"); err == nil {
if dsn, err := ParseVcap(os.Getenv("VCAP_SERVICES"), []string{"postgres", "postgresql"}, "uri"); err == nil {
d, err = Database("postgres", fmt.Sprintf("%s?sslmode=disable", dsn))
if err != nil {
log.Infof("Unable to connect to database: %s", err)

11
vcap.go
View File

@ -5,7 +5,7 @@ import (
"fmt"
)
func ParseVcap(src, tag, subkey string) (string, error) {
func ParseVcap(src string, tags []string, subkey string) (string, error) {
var services map[string][]struct {
Credentials map[string]interface{} `json:"credentials"`
@ -25,10 +25,13 @@ func ParseVcap(src, tag, subkey string) (string, error) {
for _, l := range services {
for _, service := range l {
tagged := false
TAGS:
for _, actual := range service.Tags {
if tag == actual {
tagged = true
break
for _, tag := range tags {
if tag == actual {
tagged = true
break TAGS
}
}
}