-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (47 loc) · 1014 Bytes
/
main.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
62
63
64
package main
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
_ "github.com/lib/pq"
"github.com/rossi1/coding-challenge/cmd"
"github.com/rossi1/coding-challenge/operation"
)
var (
host = os.Getenv("DB_HOST")
port, _ = strconv.Atoi(os.Getenv("DB_PORT"))
user = os.Getenv("DB_USER")
password = os.Getenv("DB_PASSWORD")
dbname = os.Getenv("DB_NAME")
)
func main() {
db, err := DBInstance()
if err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
defer db.Close()
ops := operation.NewToken() // instiantate token
err = cmd.Execute(db, ops)
if err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
}
// DBInstance connects to database instance
func DBInstance() (*sql.DB, error) {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}