ipld-eth-server/Gododir/main.go

75 lines
2.2 KiB
Go
Raw Normal View History

2017-11-01 14:40:04 +00:00
package main
import (
"log"
"fmt"
"github.com/8thlight/vulcanizedb/blockchain_listener"
2017-11-02 22:37:27 +00:00
"github.com/8thlight/vulcanizedb/config"
2017-11-01 14:40:04 +00:00
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/geth"
"github.com/8thlight/vulcanizedb/observers"
2017-11-01 14:40:04 +00:00
"github.com/jmoiron/sqlx"
do "gopkg.in/godo.v2"
)
2017-11-02 22:37:27 +00:00
func parseEnvironment(context *do.Context) string {
environment := context.Args.MayString("", "environment", "env", "e")
if environment == "" {
log.Fatalln("--environment required")
}
2017-11-02 22:37:27 +00:00
return environment
}
2017-11-02 22:37:27 +00:00
func startBlockchainListener(cfg config.Config) {
fmt.Println("Client Path ", cfg.Client.IPCPath)
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
loggingObserver := observers.BlockchainLoggingObserver{}
2017-11-02 22:37:27 +00:00
connectString := config.DbConnectionString(cfg.Database)
db, err := sqlx.Connect("postgres", connectString)
if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err)
}
dbObserver := (observers.BlockchainDBObserver{Db: db})
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
loggingObserver,
dbObserver,
})
listener.Start()
}
2017-11-01 14:40:04 +00:00
func tasks(p *do.Project) {
2017-11-02 22:37:27 +00:00
p.Task("run", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
startBlockchainListener(cfg)
})
2017-11-02 22:37:27 +00:00
p.Task("migrate", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Database.Name)
context.Bash(migrate)
context.Bash(dumpSchema)
})
2017-11-02 22:37:27 +00:00
p.Task("rollback", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Database.Name)
context.Bash(migrate)
context.Bash(dumpSchema)
2017-11-01 14:40:04 +00:00
})
}
func main() {
do.Godo(tasks)
}