Skip to content

Instantly share code, notes, and snippets.

@ramene
Last active November 9, 2018 07:12
Show Gist options
  • Select an option

  • Save ramene/1dba5922ac92a71564654ff4af274db6 to your computer and use it in GitHub Desktop.

Select an option

Save ramene/1dba5922ac92a71564654ff4af274db6 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"log"
"fmt"
"github.com/DATA-DOG/go-txdb"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
// Using the blank identifier in order to solely
// provide the side-effects of the package.
// Eseentially the side effect is calling the `init()`
// method of `lib/pq`:
// func init () { sql.Register("postgres", &Driver{} }
// which you can see at `github.com/lib/pq/conn.go`
_ "github.com/lib/pq"
)
// Roach holds the connection pool to the database - created by a configuration
// object (`Config`).
type dbPool struct {
// Db holds a sql.DB pointer that represents a pool of zero or more
// underlying connections - safe for concurrent use by multiple
// goroutines -, with freeing/creation of new connections all managed
// by `sql/database` package.
Db *sql.DB
cfg ActivityPub
}
type ActivityPub struct {
host string
port int
username string
password string
}
func init() {
const (
host = "postgres"
port = 5432
user = "postgres"
password = "postgres"
dbname = "pubcast_test"
)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
// we register an sql driver named "txdb"
txdb.Register("txdb", "postgres", psqlInfo)
}
func ConnectToPool() (activitypool dbPool, err error) {
// The first argument corresponds to the driver name that the driver
// (in this case, `lib/pq`) used to register itself in `database/sql`.
// The next argument specifies the parameters to be used in the connection.
// Details about this string can be seen at https://godoc.org/github.com/lib/pq
db, err := sql.Open("txdb", "twelve")
if err != nil {
err = errors.Wrapf(err,
"Couldn't open connection to postgre database (%s)",
spew.Sdump(err))
return
}
// Ping verifies if the connection to the database is alive or if a
// new connection can be made.
if err = db.Ping(); err != nil {
err = errors.Wrapf(err,
"Couldn't ping postgre database (%s)",
spew.Sdump(err))
return
}
activitypool.Db = db
return
}
func main() {
// configuration := ActivityPub{}
db, err := ConnectToPool()
if err != nil {
log.Fatal(err)
spew.Sdump(db)
}
fmt.Println("Connection tests successful!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment