Skip to content

Commit

Permalink
Refactor bootstrap so it's decoupled from main function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Aug 8, 2017
1 parent 24a87d4 commit cbe5ede
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
24 changes: 23 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package main

import (
"log"
"os"

"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/handlers"
"github.com/play-with-docker/play-with-docker/storage"
)

func main() {
config.ParseFlags()
handlers.Bootstrap()
handlers.Bootstrap(initStorage, initEvent, initFactory)
handlers.Register()
}

func initStorage() storage.StorageApi {
s, err := storage.NewFileStorage(config.SessionsFile)
if err != nil && !os.IsNotExist(err) {
log.Fatal("Error initializing StorageAPI: ", err)
}
return s
}

func initEvent() event.EventApi {
return event.NewLocalBroker()
}

func initFactory(s storage.StorageApi) docker.FactoryApi {
return docker.NewLocalCachedFactory(s)
}
12 changes: 5 additions & 7 deletions handlers/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ var core pwd.PWDApi
var e event.EventApi
var ws *socketio.Server

func Bootstrap() {
s, err := storage.NewFileStorage(config.SessionsFile)
e = event.NewLocalBroker()
func Bootstrap(storageInit func() storage.StorageApi, eventInit func() event.EventApi, factoryInit func(storage.StorageApi) docker.FactoryApi) {
s := storageInit()

f := docker.NewLocalCachedFactory(s)
e = eventInit()

f := factoryInit(s)

if err != nil && !os.IsNotExist(err) {
log.Fatal("Error initializing StorageAPI: ", err)
}
core = pwd.NewPWD(f, e, s)

sch, err := scheduler.NewScheduler(s, e, core)
Expand Down

0 comments on commit cbe5ede

Please sign in to comment.