/
utils.go
157 lines (135 loc) · 3.76 KB
/
utils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mongoutils
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/google/uuid"
"github.com/panagiotisptr/hermes-messenger/libs/utils"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/fx"
"go.uber.org/zap"
)
var (
tUUID = reflect.TypeOf(uuid.UUID{})
uuidSubtype = byte(0x04)
mongoRegistry = bson.NewRegistryBuilder().
RegisterTypeEncoder(tUUID, bsoncodec.ValueEncoderFunc(uuidEncodeValue)).
RegisterTypeDecoder(tUUID, bsoncodec.ValueDecoderFunc(uuidDecodeValue)).
Build()
)
func uuidEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != tUUID {
return bsoncodec.ValueEncoderError{Name: "uuidEncodeValue", Types: []reflect.Type{tUUID}, Received: val}
}
b := val.Interface().(uuid.UUID)
return vw.WriteBinaryWithSubtype(b[:], uuidSubtype)
}
func uuidDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tUUID {
return bsoncodec.ValueDecoderError{Name: "uuidDecodeValue", Types: []reflect.Type{tUUID}, Received: val}
}
var data []byte
var subtype byte
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Binary:
data, subtype, err = vr.ReadBinary()
if subtype != uuidSubtype {
return fmt.Errorf("unsupported binary subtype %v for UUID", subtype)
}
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a UUID", vrType)
}
if err != nil {
return err
}
uuid2, err := uuid.FromBytes(data)
if err != nil {
return err
}
val.Set(reflect.ValueOf(uuid2))
return nil
}
// SetRegistryForUuids sets the registry for mongodb such that
// it can encode and decode primitive.Binary UUIDs into google/uuid.UUID type
func SetRegistryForUuids(
opts *options.ClientOptions,
) *options.ClientOptions {
return opts.SetRegistry(mongoRegistry)
}
func BinaryToUuid(id interface{}) uuid.UUID {
bid := id.(primitive.Binary).Data
return *(*uuid.UUID)(bid)
}
type MongoConfig struct {
MongoUri string `mapstructure:"MONGO_URI"`
MongoDB string `mapstructure:"MONGO_DB"`
}
func ProvideMongoConfig(cl *utils.ConfigLocation) (*MongoConfig, error) {
cfg := &MongoConfig{}
viper.AddConfigPath(cl.ConfigPath)
viper.SetConfigName(cl.ConfigName)
viper.SetConfigType("env")
viper.AutomaticEnv()
isNotFoundError := func(m string) bool {
return strings.Contains(strings.ToLower(m), "not found")
}
err := viper.ReadInConfig()
if err != nil && !isNotFoundError(err.Error()) {
return cfg, err
}
if err != nil && isNotFoundError(err.Error()) {
cfg.MongoDB = viper.GetString("MONGO_DB")
cfg.MongoUri = viper.GetString("MONGO_URI")
return cfg, nil
}
err = viper.Unmarshal(&cfg)
return cfg, err
}
func ProvideMongoClient(
lc fx.Lifecycle,
logger *zap.Logger,
cfg *MongoConfig,
) (*mongo.Client, error) {
client, err := mongo.NewClient(
SetRegistryForUuids(
options.Client().ApplyURI(cfg.MongoUri),
),
)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
logger.Sugar().Info("Connecting to mongo")
err := client.Connect(ctx)
if err != nil {
return err
}
return nil
},
OnStop: func(ctx context.Context) error {
logger.Sugar().Info("Disconnecting from database")
return client.Disconnect(ctx)
},
})
return client, nil
}
func ProvideMongoDatabase(
client *mongo.Client,
cfg *MongoConfig,
) *mongo.Database {
return client.Database(cfg.MongoDB)
}