Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SERVER_HOSTNAME=localhost
SERVER_PORT=3000
MONGO_URL="mongodb://mongo:mongo@localhost:27017"
MONGO_URL="mongodb://mongo:mongo@localhost:27017"
MONGO_DB="connections"
MONGO_COLLECTION="connections"
14 changes: 8 additions & 6 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
- SERVER_HOSTNAME=server
- SERVER_PORT=3000
- MONGO_URL=mongodb://mongo:mongo@mongo:27017
- MONGO_DB=connections
- MONGO_COLLECTION=connections
depends_on:
- mongo
client_1: &common_client
Expand All @@ -14,24 +16,24 @@ services:
environment:
- SERVER_HOSTNAME=server
- SERVER_PORT=3000
- USER_ID = 1001
- DEVICE_ID = 5001
- USER_ID=1001
- DEVICE_ID=5001
depends_on:
- server
client_2:
<<: *common_client
environment:
- SERVER_HOSTNAME=server
- SERVER_PORT=3000
- USER_ID = 1001
- DEVICE_ID = 5002
- USER_ID=1001
- DEVICE_ID=5002
client_3:
<<: *common_client
environment:
- SERVER_HOSTNAME=server
- SERVER_PORT=3000
- USER_ID = 1002
- DEVICE_ID = 5002
- USER_ID=1002
- DEVICE_ID=5002
mongo:
image: mongo:latest
environment:
Expand Down
36 changes: 24 additions & 12 deletions internal/mongo_adapter/mongo_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)

type Config struct {
Url string
Url string
DbName string
CollectionName string
Logger *zap.Logger
}

type Mongo struct {
client *mongo.Client
context context.Context
ttl time.Duration
client *mongo.Client
context context.Context
ttl time.Duration
dbName string
collectionName string
logger *zap.Logger
}

func New(config Config) (*Mongo, error) {
Expand All @@ -29,9 +36,12 @@
}

return &Mongo{
client: client,
context: context.Background(),
ttl: time.Duration(5000 * time.Millisecond),
client: client,
context: context.Background(),
ttl: time.Duration(5000 * time.Millisecond),
dbName: config.DbName,
collectionName: config.CollectionName,
logger: config.Logger,

Check warning on line 44 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L39-L44

Added lines #L39 - L44 were not covered by tests
}, nil
}

Expand Down Expand Up @@ -61,7 +71,9 @@

func (m *Mongo) FindUserConnection(UserID string) (*domain.Connection, error) {
filter := bson.M{"user_id": UserID, "last_heartbeat": bson.M{"$gt": primitive.NewDateTimeFromTime(time.Now().Add(-m.ttl))}}
collection := m.client.Database("connections").Collection("connections")
m.logger.Sugar().Infof("Mongo db %s\n", m.dbName)
m.logger.Sugar().Infof("Mongo collection %s\n", m.collectionName)
collection := m.client.Database(m.dbName).Collection(m.collectionName)

Check warning on line 76 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L74-L76

Added lines #L74 - L76 were not covered by tests
cursor, err := collection.Find(context.Background(), filter)
if err != nil {
return nil, fmt.Errorf("failed to find connection: %v", err)
Expand All @@ -82,7 +94,7 @@

func (m *Mongo) FindActiveConnection(UserID string, DeviceID string) (*domain.Connection, error) {
filter := bson.M{"user_id": UserID, "device_id": DeviceID, "last_heartbeat": bson.M{"$gt": primitive.NewDateTimeFromTime(time.Now().Add(-m.ttl))}}
collection := m.client.Database("connections").Collection("connections")
collection := m.client.Database(m.dbName).Collection(m.collectionName)

Check warning on line 97 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L97

Added line #L97 was not covered by tests
cursor, err := collection.Find(context.Background(), filter)
if err != nil {
return nil, fmt.Errorf("failed to find connection: %v", err)
Expand All @@ -101,7 +113,7 @@
}

func (m *Mongo) InsertConnection(connection *domain.Connection) error {
collection := m.client.Database("connections").Collection("connections")
collection := m.client.Database(m.dbName).Collection(m.collectionName)

Check warning on line 116 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L116

Added line #L116 was not covered by tests
_, err := collection.InsertOne(context.Background(), ConvertToMongoConnection(connection))
if err != nil {
return fmt.Errorf("failed to insert connection: %v", err)
Expand All @@ -112,7 +124,7 @@

func (m *Mongo) DeleteConnection(userID string, deviceID string) error {
filter := bson.M{"user_id": userID, "device_id": deviceID}
collection := m.client.Database("connections").Collection("connections")
collection := m.client.Database(m.dbName).Collection(m.collectionName)

Check warning on line 127 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L127

Added line #L127 was not covered by tests
_, err := collection.DeleteMany(context.Background(), filter)
if err != nil {
return fmt.Errorf("failed to delete connection: %v", err)
Expand All @@ -124,7 +136,7 @@
func (m *Mongo) HeartbeatConnection(userID string, deviceID string) error {
filter := bson.M{"user_id": userID, "device_id": deviceID, "last_heartbeat": bson.M{"$gt": primitive.NewDateTimeFromTime(time.Now().Add(-m.ttl))}}
update := bson.M{"$set": bson.M{"last_heartbeat": primitive.NewDateTimeFromTime(time.Now())}}
collection := m.client.Database("connections").Collection("connections")
collection := m.client.Database(m.dbName).Collection(m.collectionName)

Check warning on line 139 in internal/mongo_adapter/mongo_adapter.go

View check run for this annotation

Codecov / codecov/patch

internal/mongo_adapter/mongo_adapter.go#L139

Added line #L139 was not covered by tests
_, err := collection.UpdateOne(context.Background(), filter, update)
if err != nil {
return fmt.Errorf("failed to update connection: %v", err)
Expand Down
15 changes: 13 additions & 2 deletions internal/server_app/server_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
defer logger.Sync()

mongoURL := os.Getenv("MONGO_URL")
repositoryClient, err := adapter.New(adapter.Config{Url: mongoURL})
dbName := os.Getenv("MONGO_DB")
collectionName := os.Getenv("MONGO_COLLECTION")

logger.Sugar().Infof("Mongo URL %s\n", mongoURL)
logger.Sugar().Infof("Mongo db %s\n", dbName)
logger.Sugar().Infof("Mongo collection %s\n", collectionName)
repositoryClient, err := adapter.New(adapter.Config{
Url: mongoURL,
DbName: dbName,
CollectionName: collectionName,
Logger: logger,
})

Check warning on line 29 in internal/server_app/server_app.go

View check run for this annotation

Codecov / codecov/patch

internal/server_app/server_app.go#L18-L29

Added lines #L18 - L29 were not covered by tests
if err != nil {
logger.Sugar().Errorf("failed to create mongo client: %v, url: %s", err, mongoURL)
logger.Sugar().Panicf("failed to create mongo client: %v, url: %s", err, mongoURL)

Check warning on line 31 in internal/server_app/server_app.go

View check run for this annotation

Codecov / codecov/patch

internal/server_app/server_app.go#L31

Added line #L31 was not covered by tests
}
appService := service.New(service.Config{Repository: repositoryClient})
appRouter := router.New(router.Config{APIHandlers: appService, Log: logger})
Expand Down