diff --git a/.env b/.env index a384332..2519dea 100644 --- a/.env +++ b/.env @@ -1,3 +1,5 @@ SERVER_HOSTNAME=localhost SERVER_PORT=3000 -MONGO_URL="mongodb://mongo:mongo@localhost:27017" \ No newline at end of file +MONGO_URL="mongodb://mongo:mongo@localhost:27017" +MONGO_DB="connections" +MONGO_COLLECTION="connections" \ No newline at end of file diff --git a/compose.yml b/compose.yml index eb7dd2c..6d22ba5 100644 --- a/compose.yml +++ b/compose.yml @@ -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 @@ -14,8 +16,8 @@ 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: @@ -23,15 +25,15 @@ services: 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: diff --git a/internal/mongo_adapter/mongo_adapter.go b/internal/mongo_adapter/mongo_adapter.go index 3eb76c0..7d897fc 100644 --- a/internal/mongo_adapter/mongo_adapter.go +++ b/internal/mongo_adapter/mongo_adapter.go @@ -10,16 +10,23 @@ import ( "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) { @@ -29,9 +36,12 @@ func New(config Config) (*Mongo, error) { } 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, }, nil } @@ -61,7 +71,9 @@ func ConvertToDomainConnection(connection *Connection) *domain.Connection { 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) cursor, err := collection.Find(context.Background(), filter) if err != nil { return nil, fmt.Errorf("failed to find connection: %v", err) @@ -82,7 +94,7 @@ func (m *Mongo) FindUserConnection(UserID string) (*domain.Connection, error) { 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) cursor, err := collection.Find(context.Background(), filter) if err != nil { return nil, fmt.Errorf("failed to find connection: %v", err) @@ -101,7 +113,7 @@ func (m *Mongo) FindActiveConnection(UserID string, DeviceID string) (*domain.Co } func (m *Mongo) InsertConnection(connection *domain.Connection) error { - collection := m.client.Database("connections").Collection("connections") + collection := m.client.Database(m.dbName).Collection(m.collectionName) _, err := collection.InsertOne(context.Background(), ConvertToMongoConnection(connection)) if err != nil { return fmt.Errorf("failed to insert connection: %v", err) @@ -112,7 +124,7 @@ func (m *Mongo) InsertConnection(connection *domain.Connection) error { 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) _, err := collection.DeleteMany(context.Background(), filter) if err != nil { return fmt.Errorf("failed to delete connection: %v", err) @@ -124,7 +136,7 @@ func (m *Mongo) DeleteConnection(userID string, deviceID string) error { 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) _, err := collection.UpdateOne(context.Background(), filter, update) if err != nil { return fmt.Errorf("failed to update connection: %v", err) diff --git a/internal/server_app/server_app.go b/internal/server_app/server_app.go index 9bc0fe6..48f56c6 100644 --- a/internal/server_app/server_app.go +++ b/internal/server_app/server_app.go @@ -15,9 +15,20 @@ func Run() { 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, + }) 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) } appService := service.New(service.Config{Repository: repositoryClient}) appRouter := router.New(router.Config{APIHandlers: appService, Log: logger})