/
db.go
149 lines (133 loc) · 3.51 KB
/
db.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"database/sql"
"encoding/base64"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/gorilla/sessions"
"github.com/BurntSushi/csql"
"github.com/BurntSushi/migration"
"github.com/BurntSushi/sqlsess"
)
const (
apiVersion = 1
)
var schemaMigrations = []migration.Migrator{
func(tx migration.LimitedTx) error {
_, err := tx.Exec(`
CREATE DOMAIN utctime AS timestamp with time zone
CHECK (EXTRACT(TIMEZONE FROM VALUE) = '0');
CREATE TABLE project (
owner TEXT NOT NULL,
name TEXT NOT NULL,
created utctime NOT NULL,
PRIMARY KEY (owner, name)
);
CREATE TABLE collaborator (
project_owner TEXT NOT NULL,
project_name TEXT NOT NULL,
userid TEXT NOT NULL,
PRIMARY KEY (project_owner, project_name, userid),
FOREIGN KEY (project_owner, project_name)
REFERENCES project (owner, name)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE document (
project_owner TEXT NOT NULL,
project_name TEXT NOT NULL,
name TEXT NOT NULL,
recorded DATE NOT NULL,
categories TEXT NOT NULL,
content TEXT NOT NULL,
created_by TEXT NOT NULL,
created utctime NOT NULL,
modified utctime NOT NULL,
PRIMARY KEY (name, recorded),
FOREIGN KEY (project_owner, project_name)
REFERENCES project (owner, name)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE score (
project_owner TEXT NOT NULL,
project_name TEXT NOT NULL,
document_name TEXT NOT NULL,
document_recorded DATE NOT NULL,
word INTEGER NOT NULL,
category TEXT NOT NULL,
name TEXT NOT NULL,
created_by TEXT NOT NULL,
created utctime NOT NULL,
PRIMARY KEY
(project_owner, project_name,
document_name, document_recorded,
word, category),
FOREIGN KEY (project_owner, project_name)
REFERENCES project (owner, name)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (document_name, document_recorded)
REFERENCES document (name, recorded)
ON DELETE CASCADE
ON UPDATE CASCADE
);
`)
return err
},
}
type lcmDB struct {
*sql.DB
conf configPgsql
}
func connect(conf configPgsql) *lcmDB {
conns := fmt.Sprintf(
"user=%s password=%s host=%s port=%d dbname=%s sslmode=disable",
conf.User, conf.Password, conf.Host, conf.Port, conf.Database)
pgsqlDB, err := migration.Open("postgres", conns, schemaMigrations)
if err != nil {
log.Fatalf("Could not connect to PostgreSQL (%s@%s/%s): %s",
conf.User, conf.Host, conf.Database, err)
}
// All time operations in the database are done in UTC.
// Times for the user (in their timezone) are mostly handled in template
// helper functions.
csql.Exec(pgsqlDB, "SET timezone = UTC")
return &lcmDB{pgsqlDB, conf}
}
const (
sessionName = "void" // we only use one
sessionUserId = "userid"
)
func newStore(db *lcmDB, conf configSecurity) *sqlsess.Store {
decode64 := func(name, s string) []byte {
dec := base64.StdEncoding
bs, err := dec.DecodeString(s)
if err != nil {
log.Fatal("Could not decode %s key: %s", name, err)
}
return bs
}
hashKey := decode64("hash", conf.HashKey)
blockKey := decode64("block", conf.BlockKey)
var err error
store, err := sqlsess.Open(db.DB)
if err != nil {
log.Fatalf("Could not open session storage: %s", err)
}
store.SetKeys(hashKey, blockKey)
return store
}
func sessGet(sess *sessions.Session, key string) string {
var val string
var v interface{}
var ok bool
if v, ok = sess.Values[key]; !ok {
return ""
}
if val, ok = v.(string); !ok {
return ""
}
return val
}