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