/
mongodb.go
125 lines (108 loc) · 3.7 KB
/
mongodb.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
package datasource
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/rubberyconf/rubberyconf/internal/config"
"github.com/rubberyconf/rubberyconf/internal/feature"
"github.com/rubberyconf/rubberyconf/internal/logs"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DataSourceMongoDB struct {
client *mongo.Client
featuresCollection *mongo.Collection
}
var (
mongodbDataSource *DataSourceMongoDB
onceMongodb sync.Once
ctx context.Context
)
type MongoFeature struct {
Id primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
FeatureKey string `json:"featureKey" bson:"featureKey"`
FeatureDef feature.FeatureDefinition `json:"featureDef" bson:"featureDef"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}
func NewDataSourceMongoDB() *DataSourceMongoDB {
onceMongodb.Do(func() {
mongodbDataSource = new(DataSourceMongoDB)
mongodbDataSource.connect()
})
return mongodbDataSource
}
func (source *DataSourceMongoDB) GetFeature(feature *Feature) (bool, error) {
document := MongoFeature{}
filter := bson.D{{"FeatureKey", feature.Key}}
err := source.featuresCollection.FindOne(ctx, filter).Decode(&document)
if err == mongo.ErrNoDocuments {
feature.Value = nil
return false, nil
} else if err != nil {
logs.GetLogs().WriteMessage("error", fmt.Sprintf("mongodb doesn't asnwered properly when running FindOne feature %s", feature.Key), err)
return false, err
}
feature.Value = &document.FeatureDef
return true, nil
}
func (source *DataSourceMongoDB) DeleteFeature(feature Feature) bool {
_, err := source.featuresCollection.DeleteMany(ctx, bson.D{{"FeatureKey", feature.Key}})
if err != nil {
logs.GetLogs().WriteMessage("error", fmt.Sprintf("mongodb didn't delete feature %s", feature.Key), err)
return false
}
return true
}
func (source *DataSourceMongoDB) CreateFeature(feature Feature) bool {
newDoc := MongoFeature{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
FeatureKey: feature.Key,
FeatureDef: *feature.Value,
}
_, err := source.featuresCollection.InsertOne(ctx, newDoc)
if err != nil {
logs.GetLogs().WriteMessage("error", fmt.Sprintf("mongodb didn't create a new feature document for feature %s", feature.Key), err)
return false
}
return true
}
func (source *DataSourceMongoDB) EnableFeature(keys map[string]string) (Feature, bool) {
return enableFeature(keys)
}
func (source *DataSourceMongoDB) reviewDependencies(conf *config.Config) {
if conf.Api.Source == MONGODB &&
conf.Database.Url == "" {
logs.GetLogs().WriteMessage("error", "database mongodb server dependency enabled but not configured, check config yml file.", nil)
os.Exit(2)
}
}
func (source *DataSourceMongoDB) connect() {
conf := config.GetConfiguration()
clientOptions := options.Client().ApplyURI(conf.Database.Url)
client, err := mongo.NewClient(clientOptions)
if err != nil {
logs.GetLogs().WriteMessage("error", "unable to cerate monogo client", err)
os.Exit(2)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
logs.GetLogs().WriteMessage("error", "unable to connect monogo client", err)
os.Exit(2)
}
err = client.Ping(ctx, nil)
if err != nil {
logs.GetLogs().WriteMessage("error", "mongodb doesn't answer ping", err)
os.Exit(2)
}
//defer client.Disconnect(ctx)
source.client = client
database := client.Database(conf.Database.DatabaseName)
source.featuresCollection = database.Collection(conf.Database.Collections.Features)
}