-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
96 lines (73 loc) · 2.47 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
package main
import (
"github.com/18F/aws-broker/config"
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/render"
"log"
"os"
"github.com/18F/aws-broker/catalog"
"github.com/18F/aws-broker/db"
"github.com/18F/aws-broker/taskqueue"
)
func main() {
var settings config.Settings
// Load settings from environment
if err := settings.LoadFromEnv(); err != nil {
log.Println("There was an error loading settings")
log.Println(err)
return
}
DB, err := db.InternalDBInit(settings.DbConfig)
if err != nil {
log.Println("There was an error with the DB. Error: " + err.Error())
return
}
Queue := taskqueue.NewQueueManager()
Queue.Init()
// Try to connect and create the app.
if m := App(&settings, DB, Queue); m != nil {
log.Println("Starting app...")
m.Run()
} else {
log.Println("Unable to setup application. Exiting...")
}
}
// App gathers all necessary dependencies (databases, settings), injects them into the router, and starts the app.
func App(settings *config.Settings, DB *gorm.DB, TaskQueue *taskqueue.QueueManager) *martini.ClassicMartini {
m := martini.Classic()
username := os.Getenv("AUTH_USER")
password := os.Getenv("AUTH_PASS")
m.Use(auth.Basic(username, password))
m.Use(render.Renderer())
m.Map(DB)
m.Map(settings)
m.Map(TaskQueue)
path, _ := os.Getwd()
m.Map(catalog.InitCatalog(path))
log.Println("Loading Routes")
// Serve the catalog with services and plans
m.Get("/v2/catalog", func(r render.Render, c *catalog.Catalog) {
r.JSON(200, map[string]interface{}{
"services": c.GetServices(),
})
})
// Create the service instance (cf create-service-instance)
// This is a PUT per https://github.com/openservicebrokerapi/servicebroker/blob/v2.16/spec.md#provisioning
m.Put("/v2/service_instances/:id", CreateInstance)
// Update the service instance
m.Patch("/v2/service_instances/:id", ModifyInstance)
// Poll service endpoint to get status of rds or elasticache
m.Get("/v2/service_instances/:instance_id/last_operation", LastOperation)
// Bind the service to app (cf bind-service)
m.Put("/v2/service_instances/:instance_id/service_bindings/:id", BindInstance)
// Unbind the service from app
m.Delete("/v2/service_instances/:instance_id/service_bindings/:id", func(p martini.Params, r render.Render) {
var emptyJSON struct{}
r.JSON(200, emptyJSON)
})
// Delete service instance
m.Delete("/v2/service_instances/:instance_id", DeleteInstance)
return m
}