-
Notifications
You must be signed in to change notification settings - Fork 67
/
datastore.go
81 lines (68 loc) · 1.65 KB
/
datastore.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/lib/pq"
)
type (
Datastore struct {
db *sql.DB
}
LogEntry struct {
Date time.Time
Addr string
Path string
}
)
func NewDatastore(host string, port int, username string, password string, dbName string, dbSSLMode string) (*Datastore, error) {
dsn := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", username, password, host, port, dbName, dbSSLMode)
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
ds := &Datastore{
db: db,
}
ds.init()
return ds, nil
}
func (ds *Datastore) init() {
if _, err := ds.db.Exec(`create table if not exists logs (date timestamp without time zone, addr varchar(64), path varchar(256));`); err != nil {
log.Printf("unable to initdb: %s\n", err)
}
}
func (ds *Datastore) Add(logEntry *LogEntry) error {
ts := logEntry.Date.Format(time.RFC3339)
if _, err := ds.db.Exec(`insert into logs (date, addr, path) values ($1, $2, $3)`, ts, logEntry.Addr, logEntry.Path); err != nil {
return err
}
log.Printf("added entry %s %s\n", ts, logEntry.Path)
return nil
}
func (ds *Datastore) GetAll() ([]*LogEntry, error) {
logs := []*LogEntry{}
rows, err := ds.db.Query(`select * from logs;`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var entry = LogEntry{}
var date time.Time
var addr, path string
if err := rows.Scan(&date, &addr, &path); err != nil {
return nil, err
}
entry.Date = date
entry.Addr = addr
entry.Path = path
logs = append(logs, &entry)
}
log.Printf("retrieved %d entries\n", len(logs))
return logs, nil
}
func (ds *Datastore) Close() {
ds.db.Close()
}