Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 선착순 이벤트 카운트다운에 대한 sse 추가 #26

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: 이벤트 시간까지 카운트다운 시간을 sse로 전송하는 api 추가
luna156 committed Aug 20, 2024
commit 8ba2ccb6ace9079e972b8f901e6e231c1288b077
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.softeer.podoarrival.event.controller;

import com.softeer.podoarrival.event.service.ArrivalEventService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequiredArgsConstructor
@RequestMapping("/arrival")
@Slf4j
public class ArrivalEventSseController {
private final ArrivalEventService arrivalEventService;

@GetMapping(value = "/time", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@Operation(summary = "선착순 서버시간 SSE Api")
public Flux<Long> streamServerTime() {
return arrivalEventService.streamServerTime();
}
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,11 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;

@Slf4j
@@ -14,8 +18,24 @@
public class ArrivalEventService {

private final ArrivalEventReleaseService arrivalEventReleaseServiceRedisImpl;
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public CompletableFuture<ArrivalApplicationResponseDto> applyEvent(AuthInfo authInfo) {
return arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo);
}

/**
* SSE로 서버시간기준 이벤트 시작 시간까지 남은 시간을 전송
* @return Flux<String> sse로 전송하게되는 이벤트까지 남은 시간
*/
public Flux<Long> streamServerTime() {
return Flux.concat(
Flux.just(0L), // Emit initial value immediately
Flux.interval(Duration.ofSeconds(20)))
.map(sequence -> {
LocalTime startTime = ArrivalEventReleaseServiceJavaImpl.getStartTime();
long seconds = Duration.between(LocalTime.now(), startTime).getSeconds();
return Math.max(seconds, 0);
});
}
}