Skip to content

Commit

Permalink
feat: allow setting Database URI from environmental variable
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Nov 30, 2022
1 parent 5a3a80f commit ac7e418
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"github.com/forbole/juno/v4/database"
"github.com/forbole/juno/v4/types"
"github.com/forbole/juno/v4/types/config"
"github.com/forbole/juno/v4/types/env"
"github.com/forbole/juno/v4/types/utils"
)

// Builder creates a database connection with the given database connection info
// from config. It returns a database connection handle or an error if the
// connection fails.
func Builder(ctx *database.Context) (database.Database, error) {
postgresDb, err := sqlx.Open("postgres", ctx.Cfg.URL)
postgresDb, err := sqlx.Open("postgres", utils.GetEnvOr(env.DatabaseURI, ctx.Cfg.URL))
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions types/env/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package env

const (
DatabaseURI = "JUNO_DATABASE_URL"
)
13 changes: 13 additions & 0 deletions types/utils/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"os"
)

// GetEnvOr returns the value of the ENV variable having the given key, or the provided orValue
func GetEnvOr(envKey string, orValue string) string {
if envValue := os.Getenv(envKey); envValue != "" {
return envValue
}
return orValue
}

0 comments on commit ac7e418

Please sign in to comment.