-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsight.go
40 lines (36 loc) · 930 Bytes
/
insight.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
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/lafikl/pq"
)
// Insight is the entry point
type Insight struct {
db *sql.DB
}
// NewInsight returns a new Insight
func NewInsight() *Insight {
dburl := os.Getenv("PGINSIGHT_DBURL")
if len(dburl) == 0 {
fmt.Println("\n\tEnvironment variable $PGINSIGHT_DBURL not set")
fmt.Println("\n\tTo set the url:\n\texport PGINSIGHT_DBURL=\"postgres://username:password@localhost/dbname?sslmode=disable\"\n")
os.Exit(1)
}
db, err := sql.Open("postgres", dburl)
if err != nil {
fmt.Println("\n\tCouldn't connect to database\n\t Database URL: ", dburl)
fmt.Println("\n\tTo set the url:\n\texport PGINSIGHT_DBURL=\"postgres://username:password@localhost/dbname?sslmode=disable\"\n")
os.Exit(1)
}
// test connection
var one int
err = db.QueryRow("Select 1;").Scan(&one)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return &Insight{
db: db,
}
}