Heroku integration
This commit is contained in:
parent
25f08a8eda
commit
26e72410e6
2 changed files with 18 additions and 18 deletions
14
bactdb.go
14
bactdb.go
|
@ -21,13 +21,6 @@ func main() {
|
||||||
Name: "serve",
|
Name: "serve",
|
||||||
ShortName: "s",
|
ShortName: "s",
|
||||||
Usage: "Start web server",
|
Usage: "Start web server",
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.IntFlag{
|
|
||||||
Name: "port",
|
|
||||||
Usage: "HTTP service port",
|
|
||||||
Value: 8901,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: cmdServe,
|
Action: cmdServe,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -54,7 +47,12 @@ func main() {
|
||||||
|
|
||||||
func cmdServe(c *cli.Context) {
|
func cmdServe(c *cli.Context) {
|
||||||
var err error
|
var err error
|
||||||
httpAddr := fmt.Sprintf(":%v", c.Int("port"))
|
|
||||||
|
addr := os.Getenv("PORT")
|
||||||
|
if addr == "" {
|
||||||
|
addr = "8901"
|
||||||
|
}
|
||||||
|
httpAddr := fmt.Sprintf(":%v", addr)
|
||||||
|
|
||||||
datastore.Connect()
|
datastore.Connect()
|
||||||
err = api.SetupCerts()
|
err = api.SetupCerts()
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/DavidHuie/gomigrate"
|
"github.com/DavidHuie/gomigrate"
|
||||||
"github.com/jmoiron/modl"
|
"github.com/jmoiron/modl"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DB is the global database
|
// DB is the global database
|
||||||
|
@ -26,8 +26,8 @@ var connectOnce sync.Once
|
||||||
func Connect() {
|
func Connect() {
|
||||||
connectOnce.Do(func() {
|
connectOnce.Do(func() {
|
||||||
var err error
|
var err error
|
||||||
setDBCredentialsFromFig()
|
conn := setDBCredentials()
|
||||||
DB.Dbx, err = sqlx.Open("postgres", "timezone=UTC sslmode=disable")
|
DB.Dbx, err = sqlx.Open("postgres", conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error connecting to PostgreSQL database (using PG* environment variables): ", err)
|
log.Fatal("Error connecting to PostgreSQL database (using PG* environment variables): ", err)
|
||||||
}
|
}
|
||||||
|
@ -93,11 +93,13 @@ func transact(dbh modl.SqlExecutor, fn func(fbh modl.SqlExecutor) error) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDBCredentialsFromFig() {
|
func setDBCredentials() string {
|
||||||
if figVal := os.Getenv("BACTDB_DB_1_PORT_5432_TCP_ADDR"); figVal != "" {
|
connection := "timezone=UTC "
|
||||||
err := os.Setenv("PGHOST", figVal)
|
if heroku := os.Getenv("HEROKU"); heroku == "true" {
|
||||||
if err != nil {
|
url := os.Getenv("DATABASE_URL")
|
||||||
log.Print(err)
|
conn, _ := pq.ParseURL(url)
|
||||||
}
|
connection += conn
|
||||||
|
connection += " sslmode=require"
|
||||||
}
|
}
|
||||||
|
return connection
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue