From b8312110fdb82d756aa953ecb02b6f220f2b6717 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 11 Jan 2023 19:09:53 +0100 Subject: [PATCH] feat: Introduced config switches to choose the used implementations - 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`. --- .../backend/ChatBackendConfiguration.java | 2 - ...=> InMemoryServicesAutoConfiguration.java} | 8 +++- ...ava => FilesStorageAutoConfiguration.java} | 8 +++- .../mongodb/MongoDbStorageConfiguration.java | 38 +++++++++++++++++++ src/main/resources/META-INF/spring.factories | 3 ++ 5 files changed, 55 insertions(+), 4 deletions(-) rename src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/{InMemoryServicesConfiguration.java => InMemoryServicesAutoConfiguration.java} (70%) rename src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/{FilesStorageConfiguration.java => FilesStorageAutoConfiguration.java} (77%) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java create mode 100644 src/main/resources/META-INF/spring.factories diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index ac77748e..10e9d37b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -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/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java similarity index 70% rename from src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java rename to src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java index 20a1b5f6..7e3667df 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesAutoConfiguration.java @@ -2,14 +2,20 @@ 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 +public class InMemoryServicesAutoConfiguration { @Bean InMemoryChatHomeService chatHomeService( 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/FilesStorageAutoConfiguration.java similarity index 77% rename from src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java rename to src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageAutoConfiguration.java index 32bedb8f..2a3936cd 100644 --- 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/FilesStorageAutoConfiguration.java @@ -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,8 +12,13 @@ import java.nio.file.Paths; import java.time.Clock; +@ConditionalOnProperty( + prefix = "chat.backend", + name = "storage", + havingValue = "files", + matchIfMissing = true) @Configuration -public class FilesStorageConfiguration +public class FilesStorageAutoConfiguration { @Bean public StorageStrategy storageStrategy( 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 index 00000000..120fab34 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/mongodb/MongoDbStorageConfiguration.java @@ -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 index 00000000..6688cd53 --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -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 -- 2.20.1