48 lines
816 B
Go
48 lines
816 B
Go
// Package database implements postgres connection and queries.
|
|
package database
|
|
|
|
import (
|
|
"log"
|
|
|
|
"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.AddQueryHook(&logSQL{})
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
type logSQL struct{}
|
|
|
|
func (l *logSQL) BeforeQuery(e *pg.QueryEvent) {}
|
|
|
|
func (l *logSQL) AfterQuery(e *pg.QueryEvent) {
|
|
query, err := e.FormattedQuery()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println(query)
|
|
}
|
|
|
|
func checkConn(db *pg.DB) error {
|
|
var n int
|
|
_, err := db.QueryOne(pg.Scan(&n), "SELECT 1")
|
|
return err
|
|
}
|