mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 11:56:57 -08:00
add database name envvar to GetDbUrl
This commit is contained in:
9
db/db.go
9
db/db.go
@@ -21,18 +21,23 @@ func CreateAllTables() error {
|
|||||||
return CreateSessionsTable()
|
return CreateSessionsTable()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDbUrl() string {
|
func GetDbUrl(noDBName bool) string {
|
||||||
host := os.Getenv("PGHOST")
|
host := os.Getenv("PGHOST")
|
||||||
port := os.Getenv("PGPORT")
|
port := os.Getenv("PGPORT")
|
||||||
user := os.Getenv("PGUSER")
|
user := os.Getenv("PGUSER")
|
||||||
pass := os.Getenv("PGPASSWORD")
|
pass := os.Getenv("PGPASSWORD")
|
||||||
|
db := os.Getenv("PGDATABASE")
|
||||||
|
|
||||||
|
if noDBName {
|
||||||
return fmt.Sprintf("postgres://%s:%s@%s:%s", user, pass, host, port)
|
return fmt.Sprintf("postgres://%s:%s@%s:%s", user, pass, host, port)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, pass, host, port, db)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDB() error {
|
func CreateDB() error {
|
||||||
conn, err := pgx.Connect(context.Background(),
|
conn, err := pgx.Connect(context.Background(),
|
||||||
GetDbUrl(),
|
GetDbUrl(true),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Cannot connect to PostgreSQL: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Cannot connect to PostgreSQL: %v\n", err)
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -22,10 +22,7 @@ func main() {
|
|||||||
check("ensuring muzi DB exists", db.CreateDB())
|
check("ensuring muzi DB exists", db.CreateDB())
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
db.Pool, err = pgxpool.New(
|
db.Pool, err = pgxpool.New(context.Background(), db.GetDbUrl(false))
|
||||||
context.Background(),
|
|
||||||
fmt.Sprintf(db.GetDbUrl(), "/muzi"),
|
|
||||||
)
|
|
||||||
check("connecting to muzi database", err)
|
check("connecting to muzi database", err)
|
||||||
defer db.Pool.Close()
|
defer db.Pool.Close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user