refactor: One stream -> using `flatMap` instead of an inner `subscribe`
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / ChatBackendApplication.java
index 7f6f213..76debbe 100644 (file)
@@ -1,15 +1,42 @@
 package de.juplo.kafka.chat.backend;
 
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
+import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import jakarta.annotation.PreDestroy;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-
-import java.time.Clock;
+import org.springframework.web.reactive.config.CorsRegistry;
+import org.springframework.web.reactive.config.WebFluxConfigurer;
 
 
 @SpringBootApplication
-public class ChatBackendApplication
+public class ChatBackendApplication implements WebFluxConfigurer
 {
+       @Autowired
+       ChatBackendProperties properties;
+       @Autowired
+       ChatHomeService chatHomeService;
+       @Autowired
+       StorageStrategy storageStrategy;
+
+
+       @Override
+       public void addCorsMappings(CorsRegistry registry)
+       {
+               registry
+                               .addMapping("/**")
+                               .allowedOrigins(properties.getAllowedOrigins());
+       }
+
+       @PreDestroy
+       public void onExit()
+       {
+               storageStrategy
+                               .write(chatHomeService)
+                               .subscribe();
+       }
+
        public static void main(String[] args)
        {
                SpringApplication.run(ChatBackendApplication.class, args);