mongo unfatal

This commit is contained in:
mr 2024-07-31 16:06:47 +02:00
parent 78bef28a21
commit c4448fd3af
2 changed files with 54 additions and 21 deletions

View File

@ -23,8 +23,9 @@ var (
cancel context.CancelFunc cancel context.CancelFunc
existingCollections []string existingCollections []string
mngoCollections []string
ResourceMap map[string]interface{} mngoConfig MongoConf
ResourceMap map[string]interface{}
) )
var MONGOService = MongoDB{} var MONGOService = MongoDB{}
@ -46,20 +47,27 @@ func (m *MongoDB) Init(collections []string, config MongoConf) {
m.Logger.Info().Msg("Connecting to" + config.GetUrl()) m.Logger.Info().Msg("Connecting to" + config.GetUrl())
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
mngoCollections = collections
m.createClient(config.GetUrl()) mngoConfig = config
if err := m.createClient(config.GetUrl()); err == nil {
m.Logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase()) m.connect()
m.prepareDB(collections, config) }
m.Logger.Info().Msg("Database is READY")
} }
func (m *MongoDB) createClient(MongoURL string) { func (m *MongoDB) connect() error {
if mngoClient == nil {
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
m.prepareDB(mngoCollections, mngoConfig)
m.Logger.Info().Msg("Database is READY")
return nil
} else {
return m.createClient(mngoConfig.GetUrl())
}
}
func (m *MongoDB) createClient(MongoURL string) error {
var err error var err error
// Allows us to use marshal and unmarshall with results of FindOne() and others // Allows us to use marshal and unmarshall with results of FindOne() and others
bsonOpts := &options.BSONOptions{ bsonOpts := &options.BSONOptions{
UseJSONStructTags: true, UseJSONStructTags: true,
@ -69,21 +77,21 @@ func (m *MongoDB) createClient(MongoURL string) {
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts) clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
mngoClient, err = mongo.Connect(MngoCtx, clientOptions) mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
if err != nil { if err != nil {
m.Logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err") mngoClient = nil
panic(err) return errors.New("Mongodb NewClient " + MongoURL + ":" + "err")
} }
// Ping the primary // Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil { if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
m.Logger.Fatal().Msg("Mongodb connect " + MongoURL + ":" + "err") mngoClient = nil
panic(err) return errors.New("Mongodb connect " + MongoURL + ":" + "err")
} }
if err = mngoClient.Ping(MngoCtx, nil); err != nil { if err = mngoClient.Ping(MngoCtx, nil); err != nil {
m.Logger.Fatal().Msg("Mongodb ping " + MongoURL + ":" + "err") mngoClient = nil
panic(err) return errors.New("Mongodb connect " + MongoURL + ":" + "err")
} }
return nil
} }
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) { func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
@ -133,6 +141,9 @@ func (m *MongoDB) createCollection(collection_name string, new_collection *mongo
} }
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) { func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
filter := bson.M{"_id": id} filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}}) opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
@ -148,6 +159,9 @@ func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, erro
} }
func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) { func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
filter := bson.D{} filter := bson.D{}
for k, v := range f { for k, v := range f {
filter = append(filter, bson.E{Key: k, Value: v}) filter = append(filter, bson.E{Key: k, Value: v})
@ -166,13 +180,15 @@ func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name strin
} }
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) { func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
var doc map[string]interface{} var doc map[string]interface{}
b, _ := bson.Marshal(set) b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc) bson.Unmarshal(b, &doc)
f := bson.D{} f := bson.D{}
for k, v := range filter { for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v}) f = append(f, bson.E{Key: k, Value: v})
} }
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@ -186,6 +202,9 @@ func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{},
} }
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) { func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
var doc map[string]interface{} var doc map[string]interface{}
b, _ := bson.Marshal(set) b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc) bson.Unmarshal(b, &doc)
@ -202,6 +221,9 @@ func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string)
} }
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) { func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
var doc map[string]interface{} var doc map[string]interface{}
b, _ := bson.Marshal(obj) b, _ := bson.Marshal(obj)
bson.Unmarshal(b, &doc) bson.Unmarshal(b, &doc)
@ -221,6 +243,9 @@ func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (
} }
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) { func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
filter := bson.M{"_id": id} filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
@ -237,6 +262,9 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
} }
func (m *MongoDB) Search(search string, filter []string, collection_name string) (*mongo.Cursor, int, error) { func (m *MongoDB) Search(search string, filter []string, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
opts := options.Find() opts := options.Find()
opts.SetLimit(100) opts.SetLimit(100)
if strings.TrimSpace(search) == "*" { if strings.TrimSpace(search) == "*" {
@ -263,6 +291,9 @@ func (m *MongoDB) Search(search string, filter []string, collection_name string)
} }
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) { func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
f := bson.D{} f := bson.D{}
for k, v := range filter { for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v}) f = append(f, bson.E{Key: k, Value: v})
@ -281,6 +312,9 @@ func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name stri
} }
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) { func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)

View File

@ -18,7 +18,6 @@ type AbstractResource struct {
Price string `json:"price,omitempty" bson:"price,omitempty"` Price string `json:"price,omitempty" bson:"price,omitempty"`
License string `json:"license,omitempty" bson:"license,omitempty"` License string `json:"license,omitempty" bson:"license,omitempty"`
ResourceModel *ResourceModel `json:"resource_model,omitempty" bson:"resource_model,omitempty"` ResourceModel *ResourceModel `json:"resource_model,omitempty" bson:"resource_model,omitempty"`
//Proxy *ResourceProxy `json:"proxy,omitempty" bson:"proxy,omitempty"`
} }
type Model struct { type Model struct {