2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package cassandra
|
|
|
|
|
2019-04-25 06:18:43 +08:00
|
|
|
import (
|
|
|
|
"github.com/gocql/gocql"
|
|
|
|
)
|
2018-08-06 23:06:55 +08:00
|
|
|
|
2019-05-16 05:31:41 +08:00
|
|
|
// DBConfig contains Cassandra DB specific parameters.
|
|
|
|
type DBConfig struct {
|
|
|
|
Hosts []string
|
|
|
|
Keyspace string
|
2020-04-07 18:02:17 +08:00
|
|
|
User string
|
|
|
|
Pass string
|
2019-05-16 05:31:41 +08:00
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
// Connect establishes connection to the Cassandra cluster.
|
2019-05-16 05:31:41 +08:00
|
|
|
func Connect(cfg DBConfig) (*gocql.Session, error) {
|
|
|
|
cluster := gocql.NewCluster(cfg.Hosts...)
|
|
|
|
cluster.Keyspace = cfg.Keyspace
|
2018-08-06 23:06:55 +08:00
|
|
|
cluster.Consistency = gocql.Quorum
|
2019-05-16 05:31:41 +08:00
|
|
|
cluster.Authenticator = gocql.PasswordAuthenticator{
|
2020-04-07 18:02:17 +08:00
|
|
|
Username: cfg.User,
|
|
|
|
Password: cfg.Pass,
|
2019-05-16 05:31:41 +08:00
|
|
|
}
|
|
|
|
cluster.Port = cfg.Port
|
2018-08-06 23:06:55 +08:00
|
|
|
|
|
|
|
return cluster.CreateSession()
|
|
|
|
}
|