feat: Introduced config switches to choose the used implementations
authorKai Moritz <kai@juplo.de>
Wed, 11 Jan 2023 18:09:53 +0000 (19:09 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 20:59:37 +0000 (21:59 +0100)
- Switched the existing `@Configuration`-classes into an auto-configuration.
- Added a `@ConditionalOnProperty`, that loades the default-configuration.
- Added an Auto-Configuration for the `MongoDbStorageStrategy`, which is
  inactive by default and can be switched on by the property
  `chat.backend.storage` to `mongodb`.

src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java [new file with mode: 0644]

index ac77748..10e9d37 100644 (file)
@@ -5,14 +5,12 @@ import de.juplo.kafka.chat.backend.domain.ChatHomeService;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
 
 import java.time.Clock;
 
 
 @Configuration
 @EnableConfigurationProperties(ChatBackendProperties.class)
-@EnableReactiveMongoRepositories
 public class ChatBackendConfiguration
 {
   @Bean
index 20a1b5f..3a99019 100644 (file)
@@ -2,12 +2,18 @@ package de.juplo.kafka.chat.backend.persistence.inmemory;
 
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import java.time.Clock;
 
 
+@ConditionalOnProperty(
+    prefix = "chat.backend",
+    name = "services",
+    havingValue = "in-memory",
+    matchIfMissing = true)
 @Configuration
 public class InMemoryServicesConfiguration
 {
index 32bedb8..ae6af2f 100644 (file)
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
 import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
@@ -11,6 +12,11 @@ import java.nio.file.Paths;
 import java.time.Clock;
 
 
+@ConditionalOnProperty(
+    prefix = "chat.backend",
+    name = "storage",
+    havingValue = "files",
+    matchIfMissing = true)
 @Configuration
 public class FilesStorageConfiguration
 {
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java
new file mode 100644 (file)
index 0000000..0a3df75
--- /dev/null
@@ -0,0 +1,32 @@
+package de.juplo.kafka.chat.backend.persistence.storage.mongodb;
+
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+    prefix = "chat.backend",
+    name = "storage",
+    havingValue = "mongodb")
+@Configuration
+public class MongoDbStorageConfiguration
+{
+  @Bean
+  public StorageStrategy storageStrategy(
+      ChatRoomRepository chatRoomRepository,
+      ChatBackendProperties properties,
+      Clock clock)
+  {
+    return new MongoDbStorageStrategy(
+        chatRoomRepository,
+        clock,
+        properties.getChatroomBufferSize(),
+        messageFlux -> new InMemoryChatRoomService(messageFlux));
+  }
+}