Heroku integration
This commit is contained in:
parent
25f08a8eda
commit
26e72410e6
2 changed files with 18 additions and 18 deletions
16
bactdb.go
16
bactdb.go
|
@ -21,14 +21,7 @@ func main() {
|
|||
Name: "serve",
|
||||
ShortName: "s",
|
||||
Usage: "Start web server",
|
||||
Flags: []cli.Flag{
|
||||
cli.IntFlag{
|
||||
Name: "port",
|
||||
Usage: "HTTP service port",
|
||||
Value: 8901,
|
||||
},
|
||||
},
|
||||
Action: cmdServe,
|
||||
Action: cmdServe,
|
||||
},
|
||||
{
|
||||
Name: "createdb",
|
||||
|
@ -54,7 +47,12 @@ func main() {
|
|||
|
||||
func cmdServe(c *cli.Context) {
|
||||
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()
|
||||
err = api.SetupCerts()
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/DavidHuie/gomigrate"
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// DB is the global database
|
||||
|
@ -26,8 +26,8 @@ var connectOnce sync.Once
|
|||
func Connect() {
|
||||
connectOnce.Do(func() {
|
||||
var err error
|
||||
setDBCredentialsFromFig()
|
||||
DB.Dbx, err = sqlx.Open("postgres", "timezone=UTC sslmode=disable")
|
||||
conn := setDBCredentials()
|
||||
DB.Dbx, err = sqlx.Open("postgres", conn)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func setDBCredentialsFromFig() {
|
||||
if figVal := os.Getenv("BACTDB_DB_1_PORT_5432_TCP_ADDR"); figVal != "" {
|
||||
err := os.Setenv("PGHOST", figVal)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
func setDBCredentials() string {
|
||||
connection := "timezone=UTC "
|
||||
if heroku := os.Getenv("HEROKU"); heroku == "true" {
|
||||
url := os.Getenv("DATABASE_URL")
|
||||
conn, _ := pq.ParseURL(url)
|
||||
connection += conn
|
||||
connection += " sslmode=require"
|
||||
}
|
||||
return connection
|
||||
}
|
||||
|
|
Reference in a new issue