/
postgresql.go
60 lines (48 loc) · 1.04 KB
/
postgresql.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
package services
import (
"database/sql"
"errors"
"log"
"strings"
// Blank import required to initialize the SQL driver
_ "github.com/lib/pq"
)
// Postgresql service
type Postgresql struct{}
// Run implements Service interface.
func (Postgresql) Run(parameters map[string]interface{}) error {
var (
dsn string
err error
ok bool
)
dsn, ok = parameters["dsn"].(string)
if !ok || dsn == "" {
dsn = "postgres://?connect_timeout=5"
}
if !strings.Contains(dsn, "connect_timeout") {
return &HardError{errors.New(`missing "connect_timeout" parameter in postgresql url`)}
}
log.Printf(`dsn: "%s"`, dsn)
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Printf(`Error: "%s"`, err)
return &SoftError{err}
}
err = db.Ping()
if err != nil {
log.Printf(`Error: "%s"`, err)
return &SoftError{err}
}
return nil
}
// Name implements Service interface.
func (Postgresql) Name() string {
return "postgresql"
}
// Parameters implements Service interface.
func (Postgresql) Parameters() []string {
return []string{
"dsn",
}
}