8460: Scaffold for websocket server.
[arvados.git] / services / ws / pg.go
1 package main
2
3 import (
4         "database/sql"
5         "log"
6         "strings"
7         "sync"
8
9         _ "github.com/lib/pq"
10 )
11
12 type pgConfig map[string]string
13
14 func (c pgConfig) ConnectionString() string {
15         s := ""
16         for k, v := range c {
17                 s += k
18                 s += "='"
19                 s += strings.Replace(
20                         strings.Replace(v, `\`, `\\`, -1),
21                         `'`, `\'`, -1)
22                 s += "' "
23         }
24         return s
25 }
26
27 type pgEventSource struct {
28         PgConfig  pgConfig
29         QueueSize int
30
31         db        *sql.DB
32         setupOnce sync.Once
33 }
34
35 func (es *pgEventSource) setup() {
36         db, err := sql.Open("postgres", es.PgConfig.ConnectionString())
37         if err != nil {
38                 log.Fatal(err)
39         }
40         es.db = db
41 }
42
43 func (es *pgEventSource) EventSource() <-chan event {
44         es.setupOnce.Do(es.setup)
45         return nil
46 }