initial commit

This commit is contained in:
dhax 2017-09-25 18:23:11 +02:00
commit 93d8310491
46 changed files with 3379 additions and 0 deletions

44
database/postgres.go Normal file
View file

@ -0,0 +1,44 @@
package database
import (
"fmt"
"time"
"github.com/spf13/viper"
"github.com/go-pg/pg"
)
// DBConn returns a postgres connection pool
func DBConn() (*pg.DB, error) {
opts, err := pg.ParseURL(viper.GetString("database_url"))
if err != nil {
return nil, err
}
db := pg.Connect(opts)
if err := checkConn(db); err != nil {
return nil, err
}
if viper.GetBool("db_debug") {
db.OnQueryProcessed(func(event *pg.QueryProcessedEvent) {
query, err := event.FormattedQuery()
if err != nil {
panic(err)
}
fmt.Printf("%s %s\n", time.Since(event.StartTime), query)
})
}
return db, nil
}
func checkConn(db *pg.DB) error {
var n int
if _, err := db.QueryOne(pg.Scan(&n), "SELECT 1"); err != nil {
return err
}
return nil
}