-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4727[GOLIUM][MongoDB] Create_generic_code_working_MongoDB
- Loading branch information
1 parent
77eea8a
commit 95e9265
Showing
6 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Telefónica Cybersecurity & Cloud Tech S.L. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mongo | ||
|
||
import ( | ||
"context" | ||
|
||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type ClientFunctions interface { | ||
|
||
Ping(ctx context.Context, client *mongo.Client) error | ||
|
||
} | ||
type ClientService struct{} | ||
|
||
func NewMongoClientService() *ClientService { | ||
return &ClientService{} | ||
} | ||
|
||
//Ping check that there is a connection to MongoDB | ||
func (c ClientService) Ping(ctx context.Context, client *mongo.Client) error { | ||
return client.Ping(context.Background(), nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2021 Telefonica Cybersecurity & Cloud Tech SL | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mongo | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ContextKey defines a type to store the mongo session in context.Context. | ||
type ContextKey string | ||
|
||
const contextKey ContextKey = "mongoSession" | ||
|
||
// InitializeContext adds the mongo session to the context. | ||
// The new context is returned because context is immutable. | ||
func InitializeContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, contextKey, &Session{MongoClientService: *NewMongoClientService()}) | ||
} | ||
|
||
// GetSession returns the mongo session stored in context. | ||
// Note that the context should be previously initialized with InitializeContext function. | ||
func GetSession(ctx context.Context) *Session { | ||
return ctx.Value(contextKey).(*Session) | ||
} |
Oops, something went wrong.