/
init.go
48 lines (39 loc) · 1.06 KB
/
init.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
package db
import (
"time"
"github.com/gocql/gocql"
libs "github.com/k8guard/k8guardlibs"
)
// Here we'll store connection
var Sess *gocql.Session
// This is wrapper for gocql
func Connect(hosts []string) error {
libs.Log.Info("Connecting to db")
// Creating cluster for cassandra
cluster := gocql.NewCluster(hosts...)
cluster.Consistency = gocql.LocalQuorum
cluster.Timeout = time.Second * 15
// Auth if username is set
if libs.Cfg.CassandraUsername != "" {
libs.Log.Debug("Connecting with username", libs.Cfg.CassandraUsername)
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: libs.Cfg.CassandraUsername,
Password: libs.Cfg.CassandraPassword,
}
}
if libs.Cfg.CassandraCaPath != "" {
libs.Log.Debug("Using Ca")
cluster.SslOpts = &gocql.SslOptions{
CaPath: libs.Cfg.CassandraCaPath,
EnableHostVerification: libs.Cfg.CassandraSslHostValidation,
}
}
// Initializing session
session, err := cluster.CreateSession()
if err != nil {
return err
}
// Storring cassandra session
Sess = session
return nil
}