feat: Allowed the frontend as origin for CORS
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / ChatBackendApplication.java
1 package de.juplo.kafka.chat.backend;
2
3 import de.juplo.kafka.chat.backend.domain.ChatHome;
4 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
5 import jakarta.annotation.PreDestroy;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication;
9 import org.springframework.web.reactive.config.CorsRegistry;
10 import org.springframework.web.reactive.config.WebFluxConfigurer;
11 import reactor.core.publisher.Flux;
12
13
14 @SpringBootApplication
15 public class ChatBackendApplication implements WebFluxConfigurer
16 {
17         @Autowired
18         ChatBackendProperties properties;
19         @Autowired
20         ChatHome chatHome;
21         @Autowired
22         StorageStrategy storageStrategy;
23
24
25         @Override
26         public void addCorsMappings(CorsRegistry registry)
27         {
28                 registry
29                                 .addMapping("/**")
30                                 .allowedOrigins(properties.getAllowedOrigins());
31         }
32
33         @PreDestroy
34         public void onExit()
35         {
36                 storageStrategy.writeChatrooms(Flux.fromStream(chatHome.list()));
37         }
38
39         public static void main(String[] args)
40         {
41                 SpringApplication.run(ChatBackendApplication.class, args);
42         }
43 }