From 2a799b2bef793b46a21ca9e5634cfd107f865eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Thu, 12 Oct 2023 09:46:43 +0900 Subject: [PATCH] [FEAT] location socket join and share --- .../config/rabbitmq/RabbitMQConfig.java | 2 +- .../config/security/SecurityConfig.java | 3 +- .../backend/config/socket/StompConfig.java | 5 ++- .../{WebConfiguration.java => WebConfig.java} | 2 +- .../controller/LocationController.java | 31 +++++++++++++++++++ .../location/dto/request/LocationRequest.java | 14 +++++++++ .../dto/response/LocationResponse.java | 17 ++++++++++ .../location/mapper/LocationMapper.java | 19 ++++++++++++ .../location/service/LocationService.java | 20 ++++++++++++ 9 files changed, 109 insertions(+), 4 deletions(-) rename backend/src/main/java/com/twtw/backend/config/web/{WebConfiguration.java => WebConfig.java} (95%) create mode 100644 backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java create mode 100644 backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java create mode 100644 backend/src/main/java/com/twtw/backend/domain/location/dto/response/LocationResponse.java create mode 100644 backend/src/main/java/com/twtw/backend/domain/location/mapper/LocationMapper.java create mode 100644 backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java diff --git a/backend/src/main/java/com/twtw/backend/config/rabbitmq/RabbitMQConfig.java b/backend/src/main/java/com/twtw/backend/config/rabbitmq/RabbitMQConfig.java index b5842c76..9f838107 100644 --- a/backend/src/main/java/com/twtw/backend/config/rabbitmq/RabbitMQConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/rabbitmq/RabbitMQConfig.java @@ -24,7 +24,7 @@ public class RabbitMQConfig { private static final String QUEUE_NAME = "map.queue"; private static final String EXCHANGE_NAME = "map.exchange"; - private static final String ROUTING_KEY = "group.*"; + private static final String ROUTING_KEY = "plan.*"; private final RabbitMQProperties rabbitMQProperties; private final ObjectMapper objectMapper; diff --git a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java index 85d439d8..120b645f 100644 --- a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java @@ -37,7 +37,8 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception { "auth/refresh", "auth/save", "auth/login", - "member/duplicate/**") + "member/duplicate/**", + "socket/**") .permitAll()) .authorizeHttpRequests( x -> x.requestMatchers("/test/**").permitAll().anyRequest().authenticated()) diff --git a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java index ba4ee749..69a47df8 100644 --- a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java @@ -6,6 +6,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.util.AntPathMatcher; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @@ -18,11 +19,13 @@ public class StompConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(final StompEndpointRegistry registry) { - registry.addEndpoint("/socket").setAllowedOrigins("*"); + registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS(); } @Override public void configureMessageBroker(final MessageBrokerRegistry registry) { + registry.setPathMatcher(new AntPathMatcher(".")); + registry.enableStompBrokerRelay("/topic", "/queue", "/exchange", "/amq/queue") .setRelayHost(rabbitMQProperties.getHost()) .setRelayPort(61613) diff --git a/backend/src/main/java/com/twtw/backend/config/web/WebConfiguration.java b/backend/src/main/java/com/twtw/backend/config/web/WebConfig.java similarity index 95% rename from backend/src/main/java/com/twtw/backend/config/web/WebConfiguration.java rename to backend/src/main/java/com/twtw/backend/config/web/WebConfig.java index 270b7f9a..8b689896 100644 --- a/backend/src/main/java/com/twtw/backend/config/web/WebConfiguration.java +++ b/backend/src/main/java/com/twtw/backend/config/web/WebConfig.java @@ -13,7 +13,7 @@ import java.util.List; @Configuration -public class WebConfiguration extends WebMvcConfigurationSupport { +public class WebConfig extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(final List> converters) { diff --git a/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java b/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java new file mode 100644 index 00000000..4a6335e7 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java @@ -0,0 +1,31 @@ +package com.twtw.backend.domain.location.controller; + +import com.twtw.backend.domain.location.dto.request.LocationRequest; +import com.twtw.backend.domain.location.service.LocationService; +import lombok.RequiredArgsConstructor; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.stereotype.Controller; + +import java.util.UUID; + +@Controller +@RequiredArgsConstructor +public class LocationController { + + private static final String EXCHANGE_NAME = "map.exchange"; + private static final String ROUTING_KEY = "plan.*"; + private final RabbitTemplate rabbitTemplate; + private final LocationService locationService; + + @MessageMapping("map.join.{planId}") + public void join(@DestinationVariable final UUID planId, final LocationRequest locationRequest) { + rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest)); + } + + @MessageMapping("map.share.{planId}") + public void share(@DestinationVariable final UUID planId, final LocationRequest locationRequest) { + rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest)); + } +} diff --git a/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java b/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java new file mode 100644 index 00000000..2ac39210 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/location/dto/request/LocationRequest.java @@ -0,0 +1,14 @@ +package com.twtw.backend.domain.location.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class LocationRequest { + private String nickname; + private Double x; + private Double y; +} diff --git a/backend/src/main/java/com/twtw/backend/domain/location/dto/response/LocationResponse.java b/backend/src/main/java/com/twtw/backend/domain/location/dto/response/LocationResponse.java new file mode 100644 index 00000000..7a901f0b --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/location/dto/response/LocationResponse.java @@ -0,0 +1,17 @@ +package com.twtw.backend.domain.location.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Getter +@Builder +@AllArgsConstructor +public class LocationResponse { + private String nickname; + private Double x; + private Double y; + private LocalDateTime time; +} diff --git a/backend/src/main/java/com/twtw/backend/domain/location/mapper/LocationMapper.java b/backend/src/main/java/com/twtw/backend/domain/location/mapper/LocationMapper.java new file mode 100644 index 00000000..ca1d3cb0 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/location/mapper/LocationMapper.java @@ -0,0 +1,19 @@ +package com.twtw.backend.domain.location.mapper; + +import com.twtw.backend.domain.location.dto.request.LocationRequest; +import com.twtw.backend.domain.location.dto.response.LocationResponse; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +@Component +public class LocationMapper { + public LocationResponse toResponse(final LocationRequest locationRequest, final LocalDateTime now) { + return LocationResponse.builder() + .x(locationRequest.getX()) + .y(locationRequest.getY()) + .nickname(locationRequest.getNickname()) + .time(now) + .build(); + } +} diff --git a/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java new file mode 100644 index 00000000..633ce7a6 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java @@ -0,0 +1,20 @@ +package com.twtw.backend.domain.location.service; + +import com.twtw.backend.domain.location.dto.request.LocationRequest; +import com.twtw.backend.domain.location.dto.response.LocationResponse; +import com.twtw.backend.domain.location.mapper.LocationMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; + +@Service +@RequiredArgsConstructor +public class LocationService { + + private final LocationMapper locationMapper; + + public LocationResponse addInfo(final LocationRequest locationRequest) { + return locationMapper.toResponse(locationRequest, LocalDateTime.now()); + } +}