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, 11 Jan 2023 18:24:55 +0000 (19:24 +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/InMemoryServicesAutoConfiguration.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java [deleted file]
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java [new file with mode: 0644]
src/main/resources/META-INF/spring.factories [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
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java
new file mode 100644 (file)
index 0000000..7e3667d
--- /dev/null
@@ -0,0 +1,31 @@
+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 InMemoryServicesAutoConfiguration
+{
+  @Bean
+  InMemoryChatHomeService chatHomeService(
+      StorageStrategy storageStrategy,
+      Clock clock,
+      ChatBackendProperties properties)
+  {
+    return new InMemoryChatHomeService(
+        storageStrategy.read(),
+        clock,
+        properties.getChatroomBufferSize());
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java
deleted file mode 100644 (file)
index 20a1b5f..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-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.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.time.Clock;
-
-
-@Configuration
-public class InMemoryServicesConfiguration
-{
-  @Bean
-  InMemoryChatHomeService chatHomeService(
-      StorageStrategy storageStrategy,
-      Clock clock,
-      ChatBackendProperties properties)
-  {
-    return new InMemoryChatHomeService(
-        storageStrategy.read(),
-        clock,
-        properties.getChatroomBufferSize());
-  }
-}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java
new file mode 100644 (file)
index 0000000..2a3936c
--- /dev/null
@@ -0,0 +1,36 @@
+package de.juplo.kafka.chat.backend.persistence.storage.files;
+
+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;
+
+import java.nio.file.Paths;
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+    prefix = "chat.backend",
+    name = "storage",
+    havingValue = "files",
+    matchIfMissing = true)
+@Configuration
+public class FilesStorageAutoConfiguration
+{
+  @Bean
+  public StorageStrategy storageStrategy(
+      ChatBackendProperties properties,
+      Clock clock,
+      ObjectMapper mapper)
+  {
+    return new FilesStorageStrategy(
+        Paths.get(properties.getStorageDirectory()),
+        clock,
+        properties.getChatroomBufferSize(),
+        messageFlux -> new InMemoryChatRoomService(messageFlux),
+        mapper);
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java
deleted file mode 100644 (file)
index 32bedb8..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-package de.juplo.kafka.chat.backend.persistence.storage.files;
-
-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.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.nio.file.Paths;
-import java.time.Clock;
-
-
-@Configuration
-public class FilesStorageConfiguration
-{
-  @Bean
-  public StorageStrategy storageStrategy(
-      ChatBackendProperties properties,
-      Clock clock,
-      ObjectMapper mapper)
-  {
-    return new FilesStorageStrategy(
-        Paths.get(properties.getStorageDirectory()),
-        clock,
-        properties.getChatroomBufferSize(),
-        messageFlux -> new InMemoryChatRoomService(messageFlux),
-        mapper);
-  }
-}
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..120fab3
--- /dev/null
@@ -0,0 +1,38 @@
+package de.juplo.kafka.chat.backend.persistence.storage.mongodb;
+
+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 de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
+
+import java.nio.file.Paths;
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+    prefix = "chat.backend",
+    name = "mongodb",
+    havingValue = "files")
+@Configuration
+@EnableReactiveMongoRepositories
+public class MongoDbStorageConfiguration
+{
+  @Bean
+  public StorageStrategy storageStrategy(
+      ChatBackendProperties properties,
+      Clock clock,
+      ObjectMapper mapper)
+  {
+    return new FilesStorageStrategy(
+        Paths.get(properties.getStorageDirectory()),
+        clock,
+        properties.getChatroomBufferSize(),
+        messageFlux -> new InMemoryChatRoomService(messageFlux),
+        mapper);
+  }
+}
diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories
new file mode 100644 (file)
index 0000000..6688cd5
--- /dev/null
@@ -0,0 +1,3 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryServicesAutoConfiguration,\
+de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageAutoConfiguration