-
Notifications
You must be signed in to change notification settings - Fork 4
/
engine.i_dbconnector.go
138 lines (116 loc) · 3.58 KB
/
engine.i_dbconnector.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
package mongo
import (
"crypto/tls"
"crypto/x509"
"errors"
"gopkg.in/mgo.v2"
"io/ioutil"
"net"
"time"
"github.com/ottemo/commerce/env"
)
// ------------------------------------------------------------------------------------
// InterfaceDBConnector implementation (package "github.com/ottemo/commerce/db/interfaces")
// ------------------------------------------------------------------------------------
// GetConnectionParams returns configured DB connection params
func (it *DBEngine) GetConnectionParams() interface{} {
var connectionParams = connectionParamsType{
DBUri: "mongodb://localhost:27017/ottemo",
DBName: "ottemo",
UseSSL: false,
CertPoolPtr: x509.NewCertPool(),
}
var Cert = ""
if iniConfig := env.GetIniConfig(); iniConfig != nil {
if iniValue := iniConfig.GetValue("mongodb.uri", connectionParams.DBUri); iniValue != "" {
connectionParams.DBUri = iniValue
}
if iniValue := iniConfig.GetValue("mongodb.db", connectionParams.DBName); iniValue != "" {
connectionParams.DBName = iniValue
}
if iniValue := iniConfig.GetValue("ssl.cert", Cert); iniValue != "" {
connectionParams.UseSSL = true
Cert = iniValue
if ca, err := ioutil.ReadFile(Cert); err == nil {
connectionParams.CertPoolPtr.AppendCertsFromPEM(ca)
}
}
}
return connectionParams
}
// Connect establishes DB connection
func (it *DBEngine) Connect(srcConnectionParams interface{}) error {
connectionParams, ok := srcConnectionParams.(connectionParamsType)
if !ok {
return errors.New("Wrong connection parameters type.")
}
if !connectionParams.UseSSL {
// use plain text
if session, err := mgo.Dial(connectionParams.DBUri); err == nil {
it.session = session
} else {
return err
}
} else {
tlsConfig := &tls.Config{}
tlsConfig.RootCAs = connectionParams.CertPoolPtr
// make tls connection
dialInfo, _ := mgo.ParseURL(connectionParams.DBUri)
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
if session, err := mgo.DialWithInfo(dialInfo); err == nil {
it.session = session
} else {
return err
}
}
return nil
}
// AfterConnect makes initialization of DB engine
func (it *DBEngine) AfterConnect(srcConnectionParams interface{}) error {
connectionParams, ok := srcConnectionParams.(connectionParamsType)
if !ok {
return errors.New("Wrong connection parameters type.")
}
it.database = it.session.DB(connectionParams.DBName)
it.DBName = connectionParams.DBName
it.collections = map[string]bool{}
if collectionsList, err := it.database.CollectionNames(); err == nil {
for _, collection := range collectionsList {
it.collections[collection] = true
}
}
return nil
}
// Reconnect tries to reconnect to DB
func (it *DBEngine) Reconnect(connectionParams interface{}) error {
it.session.Refresh()
return nil
}
// IsConnected returns connection status
func (it *DBEngine) IsConnected() bool {
return it.isConnected
}
// SetConnected sets connection status
func (it *DBEngine) SetConnected(connected bool) {
it.isConnected = connected
}
// Ping checks connection alive
func (it *DBEngine) Ping() error {
return it.session.Ping()
}
// GetValidationInterval returns delay between Ping
func (it *DBEngine) GetValidationInterval() time.Duration {
return ConstConnectionValidateInterval
}
// GetEngineName returns DBEngine name (InterfaceDBConnector)
func (it *DBEngine) GetEngineName() string {
return it.GetName()
}
// LogConnection outputs message to log
func (it *DBEngine) LogConnection(message string) {
// ignore error
_ = it.Output(0, message)
}