-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_conf.go
43 lines (34 loc) · 927 Bytes
/
database_conf.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
package main
import (
"database/sql"
"fmt"
"os"
"errors"
"strconv"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func configureDatabase() (*sql.DB, error) {
if err := godotenv.Load(); err != nil {
return nil, err;
}
db_port, err := strconv.Atoi(os.Getenv("DB_PORT"));
if err != nil {
return nil, err;
}
db_config := DB_Config {
host: "psql",
port: uint16(db_port),
user: os.Getenv("DB_USER"),
password: os.Getenv("DB_PASS"),
db_name: os.Getenv("DB_NAME"),
};
psqlconn :=
fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
db_config.host, db_config.port, db_config.user, db_config.password, db_config.db_name);
db, err := sql.Open("postgres", psqlconn);
if err != nil {
return nil, errors.New("Couldn't open database!\n");
}
return db, nil;
}