-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.go
60 lines (48 loc) · 1.97 KB
/
routes.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
package pezdispenser
import (
"fmt"
"os"
"github.com/cloudfoundry-community/go-cfenv"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/pivotal-pez/pezdispenser/service/integrations"
)
const (
//APIVersion1 - version 1 const
APIVersion1 = "v1"
)
var (
//URLBaseV1 - v1 url path base
URLBaseV1 = fmt.Sprintf("/%s", APIVersion1)
)
//InitRoutes - initialize the mappings for controllers against valid routes
func InitRoutes(m *martini.ClassicMartini, keyCheckHandler martini.Handler, appEnv *cfenv.App) {
taskServiceURI, taskServiceDatabase := getTaskBinding(appEnv)
taskCollection := SetupDB(integrations.NewCollectionDialer, taskServiceURI, taskServiceDatabase, TaskCollectionName)
m.Map(taskCollection)
m.Use(render.Renderer())
m.Group("/", func(r martini.Router) {
r.Get("info", GetInfoController())
})
m.Group(URLBaseV1, func(r martini.Router) {
r.Get("/task/:id", GetTaskByIDController())
r.Post("/lease", PostLeaseController())
r.Delete("/lease", DeleteLeaseController())
}, keyCheckHandler)
}
func getTaskBinding(appEnv *cfenv.App) (taskServiceURI string, taskServiceDatabase string) {
taskServiceName := os.Getenv("TASK_SERVICE_NAME")
taskCredsURIName := os.Getenv("TASK_SERVICE_URI_NAME")
taskCredsDBName := os.Getenv("TASK_SERVICE_DATABASE_NAME")
if taskService, err := appEnv.Services.WithName(taskServiceName); err == nil {
if taskServiceURI = taskService.Credentials[taskCredsURIName].(string); taskServiceURI == "" {
panic(fmt.Sprint("we pulled an empty connection string %s from %v - %v", taskServiceURI, taskService, taskService.Credentials))
}
if taskServiceDatabase = taskService.Credentials[taskCredsDBName].(string); taskServiceDatabase == "" {
panic(fmt.Sprint("we pulled an empty connection string %s from %v - %v", taskServiceDatabase, taskService, taskService.Credentials))
}
} else {
panic(fmt.Sprint("Experienced an error trying to grab task service binding information:", err.Error()))
}
return
}