-
Notifications
You must be signed in to change notification settings - Fork 2
/
negronicql.go
62 lines (42 loc) · 1022 Bytes
/
negronicql.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
package negronicql
import (
"net/http"
"github.com/gocql/gocql"
"github.com/gorilla/context"
)
type Negronicql struct {
Cluster *gocql.ClusterConfig
Ips []string
Keyspace string
Consistency gocql.Consistency
Session *gocql.Session
}
func New() *Negronicql {
return &Negronicql{}
}
// be sure to defer session.close()
func (m *Negronicql) Connect() error {
//default to localhost
if len(m.Ips) < 1 {
m.Ips = []string{"127.0.0.1"}
}
//create cluster config if not created by user
if m.Cluster == nil {
m.Cluster = gocql.NewCluster(m.Ips...)
m.Cluster.Keyspace = m.Keyspace
m.Cluster.Consistency = m.Consistency
}
session, err := m.Cluster.CreateSession()
m.Session = session
if err != nil {
return err
}
return nil
}
// The middleware handler
func (m *Negronicql) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
//attach the session
context.Set(r, "CQLSession", m.Session)
// Call the next middleware handler
next(rw, r)
}