-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
61 lines (55 loc) · 1.07 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package goodidea
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"log"
"net/url"
"os"
)
var (
DB *pgxpool.Pool
)
func Connect() error {
uri := makeConnStr()
dbpool, err := pgxpool.New(context.Background(), uri)
if err != nil {
log.Printf("Unable to create connection pool: %v\n", err)
return err
}
err = dbpool.Ping(context.Background())
if err != nil {
log.Println("open_connection: failed to ping database")
return err
}
DB = dbpool
return nil
}
func makeConnStr() string {
d := os.Getenv("DATABASE_URL")
if d != "" {
return d
}
u := "fairy"
if os.Getenv("DB_USER") != "" {
u = os.Getenv("DB_USER")
}
pw := "goodidea"
if os.Getenv("DB_PASS") != "" {
pw = url.QueryEscape(os.Getenv("DB_PASS"))
}
port := "5555"
if os.Getenv("DB_PORT") != "" {
pw = os.Getenv("DB_PORT")
//TODO check it's all numbers
}
h := "localhost"
if os.Getenv("DB_HOST") != "" {
h = os.Getenv("DB_HOST")
}
n := "tasks"
if os.Getenv("DB_NAME") != "" {
n = os.Getenv("DB_NAME")
}
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", u, pw, h, port, n)
}