Skip to content

Commit

Permalink
Merge pull request #620 from woowacourse-teams/BE/test
Browse files Browse the repository at this point in the history
[BE] 운영서버 배포
  • Loading branch information
reddevilmidzy authored Sep 26, 2024
2 parents f81b344 + c0d47fa commit 081d1e2
Show file tree
Hide file tree
Showing 40 changed files with 356 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public void configurePathMatch(final PathMatchConfigurer configurer) {
@Override
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PATCH", "DELETE", "PUT", "HEAD", "OPTION")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH", "HEAD")
.allowedOrigins("http://localhost:3000", "https://coduo.site", "https://test.coduo.site",
"https://api-test.coduo.site/")
"https://api-test.coduo.site", "https://api.coduo.site")
.allowCredentials(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.net.URI;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
Expand Down Expand Up @@ -33,12 +34,15 @@ public class AuthController implements AuthControllerDocs {
private final AuthService authService;
private final MemberService memberService;

@Value("${front.url}")
private String frontUrl;

@GetMapping("/sign-out")
public ResponseEntity<Void> signOut(@CookieValue(name = SIGN_IN_COOKIE_NAME) final String signInToken) {
final SignInCookie cookie = new SignInCookie(signInToken);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.expire().toString())
.header(HttpHeaders.SET_COOKIE, cookie.expire(frontUrl).toString())
.build();
}

Expand All @@ -58,7 +62,7 @@ public ResponseEntity<SignInWebResponse> signInCallback(
@SessionAttribute(name = ACCESS_TOKEN_SESSION_NAME) final String accessToken
) {
final SignInServiceResponse serviceResponse = authService.createSignInToken(accessToken);
final ResponseCookie cookie = new SignInCookie(serviceResponse.token()).generate();
final ResponseCookie cookie = new SignInCookie(serviceResponse.token()).generate(frontUrl);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -33,6 +34,9 @@ public class GithubOAuthController implements GithubOAuthControllerDocs {

private final GithubOAuthService githubOAuthService;

@Value("${front.url}")
private String frontUrl;

@GetMapping("/sign-in/oauth/github")
public ResponseEntity<GithubOAuthEndpoint> getGithubAuthCode(final HttpSession session) {
final GithubAuthQuery query = githubOAuthService.createAuthorizationContent();
Expand All @@ -53,7 +57,7 @@ public ResponseEntity<Void> getAccessToken(@ModelAttribute final GithubCallbackQ
session.setMaxInactiveInterval(ACCESS_TOKEN_EXPIRE_IN_SECOND);

return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create("https://coduo.site/callback"))
.location(URI.create("https://" + frontUrl + "/callback"))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@

public record SignInCookie(String credential) {

private static final String SERVICE_DOMAIN_NAME = "coduo.site";

public ResponseCookie generate() {
public ResponseCookie generate(final String domain) {
return ResponseCookie.from(SIGN_IN_COOKIE_NAME)
.value(credential)
.httpOnly(true)
.secure(true)
.domain(SERVICE_DOMAIN_NAME)
.domain(domain)
.path("/")
.build();
}

public ResponseCookie expire() {
public ResponseCookie expire(final String domain) {
return ResponseCookie.from(SIGN_IN_COOKIE_NAME)
.maxAge(Duration.ZERO)
.domain(SERVICE_DOMAIN_NAME)
.domain(domain)
.path("/")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import site.coduo.pairroom.controller.docs.PairRoomDocs;
import site.coduo.pairroom.service.PairRoomService;
import site.coduo.pairroom.service.dto.PairRoomCreateRequest;
Expand All @@ -26,6 +27,7 @@
import site.coduo.pairroom.service.dto.PairRoomStatusUpdateRequest;
import site.coduo.pairroom.service.dto.PairRoomMemberResponse;

@Slf4j
@RequiredArgsConstructor
@RestController
public class PairRoomController implements PairRoomDocs {
Expand All @@ -37,9 +39,12 @@ public ResponseEntity<PairRoomCreateResponse> createPairRoom(
@Valid @RequestBody final PairRoomCreateRequest request,
@CookieValue(value = SIGN_IN_COOKIE_NAME, required = false) final String token
) {
log.info("1. 일단 Pair Room 생성 POST 요청이 들어와서 Controller가 받았다.");
final String accessCode = pairRoomService.savePairRoom(request, token);
log.info("12. 페어룸 서비스 탈출!!!!");
final PairRoomCreateResponse response = new PairRoomCreateResponse(accessCode);

log.info("13. 최종 응답 보내기 시작!!");
return ResponseEntity.created(URI.create("/"))
.body(response);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.UUID;

public class UUIDAccessCodeStrategy implements AccessCodeStrategy {
public class UUIDAccessCodeGenerator {

@Override
public String generateAccessCode() {
private static final int ACCESS_CODE_LENGTH = 6;

public static String generate() {
return UUID.randomUUID()
.toString()
.substring(0, ACCESS_CODE_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ default PairRoomEntity fetchByAccessCode(AccessCode accessCode) {
return findByAccessCode(accessCode.getValue())
.orElseThrow(() -> new PairRoomNotFoundException("존재하지 않는 페어룸 접근 코드입니다."));
}

boolean existsByAccessCode(String generatedAccessCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import site.coduo.member.domain.Member;
import site.coduo.member.service.MemberService;
import site.coduo.pairroom.domain.Pair;
import site.coduo.pairroom.domain.PairName;
import site.coduo.pairroom.domain.PairRoom;
import site.coduo.pairroom.domain.PairRoomStatus;
import site.coduo.pairroom.domain.accesscode.AccessCode;
import site.coduo.pairroom.domain.accesscode.AccessCodeFactory;
import site.coduo.pairroom.domain.accesscode.UUIDAccessCodeStrategy;
import site.coduo.pairroom.domain.accesscode.UUIDAccessCodeGenerator;
import site.coduo.pairroom.repository.PairRoomEntity;
import site.coduo.pairroom.repository.PairRoomMemberEntity;
import site.coduo.pairroom.repository.PairRoomMemberRepository;
Expand All @@ -28,6 +28,7 @@
import site.coduo.timer.repository.TimerEntity;
import site.coduo.timer.repository.TimerRepository;

@Slf4j
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
Expand All @@ -40,30 +41,42 @@ public class PairRoomService {

@Transactional
public String savePairRoom(final PairRoomCreateRequest request, @Nullable final String token) {
log.info("2. Service의 savePairRoom 메서드를 시작했다.");
log.info("3. Service의 createPairRoom 메서드 실행!");
final PairRoom pairRoom = createPairRoom(request);
log.info("4. Service의 createPairRoom 메서드 탈출!!");
log.info("5. 페어룸 리포지토리 시작!");
final PairRoomEntity pairRoomEntity = pairRoomRepository.save(PairRoomEntity.from(pairRoom));
log.info("6. 페어룸 리포지토리 끝!!");

final Timer timer = new Timer(pairRoom.getAccessCode(), request.timerDuration(), request.timerRemainingTime());
log.info("7. 타이머 리포지토리 시작!!");
timerRepository.save(new TimerEntity(timer, pairRoomEntity));

log.info("8. 토큰 널 체크 시작!!");
if (token != null) {
log.info("9. 리포지토리에서 크래덴셜로 멤버 조회");
final Member member = memberService.findMemberByCredential(token);
log.info("10. 페어룸 & 멤버 중간 테이블 save 저장!!");
pairRoomMemberRepository.save(new PairRoomMemberEntity(pairRoomEntity, member));
}
log.info("11. 페어룸 AccessToken 문자열 반환하면서 메서드 종료 시도");
return pairRoom.getAccessCodeText();
}

private PairRoom createPairRoom(final PairRoomCreateRequest request) {
final AccessCode accessCode = generateAccessCode();
final PairRoomStatus status = PairRoomStatus.findByName(request.status());
final Pair pair = new Pair(new PairName(request.navigator()), new PairName(request.driver()));
final List<AccessCode> accessCodes = pairRoomRepository.findAll()
.stream()
.map(PairRoomEntity::getAccessCode)
.map(AccessCode::new)
.toList();
return new PairRoom(status, pair, accessCode);
}

final AccessCodeFactory accessCodeFactory = new AccessCodeFactory(new UUIDAccessCodeStrategy());
return new PairRoom(status, pair, accessCodeFactory.generate(accessCodes));
private AccessCode generateAccessCode() {
final String generatedAccessCode = UUIDAccessCodeGenerator.generate();
if (pairRoomRepository.existsByAccessCode(generatedAccessCode)) {
return generateAccessCode();
}
return new AccessCode(generatedAccessCode);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import site.coduo.referencelink.controller.docs.ReferenceLinkDocs;
import site.coduo.referencelink.service.ReferenceLinkService;
import site.coduo.referencelink.service.dto.ReferenceLinkCreateRequest;
import site.coduo.referencelink.service.dto.ReferenceLinkResponse;

@Slf4j
@RequiredArgsConstructor
@RestController
public class ReferenceLinkController implements ReferenceLinkDocs {
Expand All @@ -42,8 +44,10 @@ public ResponseEntity<ReferenceLinkResponse> createReferenceLink(
public ResponseEntity<List<ReferenceLinkResponse>> getReferenceLinks(
@PathVariable("accessCode") final String accessCodeText
) {
log.info("[Reference Link] 1. 링크 조회 API 호출 시작!");
final List<ReferenceLinkResponse> responses = referenceLinkService.readAllReferenceLink(accessCodeText);

log.info("[Reference Link] 5. 끝!! 응답!");
return ResponseEntity.ok(responses);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package site.coduo.referencelink.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import site.coduo.pairroom.repository.PairRoomEntity;

public interface ReferenceLinkRepository extends JpaRepository<ReferenceLinkEntity, Long> {

List<ReferenceLinkEntity> findByPairRoomEntity(PairRoomEntity pairRoomEntity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import site.coduo.pairroom.domain.accesscode.AccessCode;
import site.coduo.pairroom.repository.PairRoomEntity;
import site.coduo.pairroom.repository.PairRoomRepository;
Expand All @@ -22,6 +23,7 @@
import site.coduo.referencelink.service.dto.ReferenceLinkCreateRequest;
import site.coduo.referencelink.service.dto.ReferenceLinkResponse;

@Slf4j
@Transactional
@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -66,11 +68,13 @@ private ReferenceLinkEntity saveReferenceLink(final ReferenceLinkCreateRequest r

@Transactional(readOnly = true)
public List<ReferenceLinkResponse> readAllReferenceLink(final String accessCodeText) {
final List<ReferenceLinkEntity> referenceLinkEntities = referenceLinkRepository.findAll()
.stream()
.filter(link -> link.isSameAccessCode(new AccessCode(accessCodeText)))
.toList();
log.info("[Reference Link] 2. readAllReferenceLink 메서드 호출 시작!");
log.info("[Reference Link] 3. referenceLinkRepository.findAll() 호출 시작!");
final PairRoomEntity pairRoom = pairRoomRepository.fetchByAccessCode(accessCodeText);

final List<ReferenceLinkEntity> referenceLinkEntities = referenceLinkRepository.findByPairRoomEntity(pairRoom);

log.info("[Reference Link] 4. referenceLinkRepository.findAll() 반환 데이터 필터링 시작!!");
return referenceLinkEntities.stream()
.map(this::makeReferenceLinkResponse)
.toList();
Expand All @@ -87,9 +91,8 @@ public List<ReferenceLinkResponse> findReferenceLinksByCategory(
categoryId);
final Category category = new Category(categoryEntity.getCategoryName());

return referenceLinkRepository.findAll()
return referenceLinkRepository.findByPairRoomEntity(pairRoomEntity)
.stream()
.filter(link -> link.isSameAccessCode(accessCode))
.filter(link -> link.isSameCategory(category))
.map(this::makeReferenceLinkResponse)
.toList();
Expand All @@ -106,9 +109,8 @@ public List<ReferenceLinkEntity> findReferenceLinksEntityByCategory(
categoryId);
final Category category = new Category(categoryEntity.getCategoryName());

return referenceLinkRepository.findAll()
return referenceLinkRepository.findByPairRoomEntity(pairRoomEntity)
.stream()
.filter(link -> link.isSameAccessCode(accessCode))
.filter(link -> link.isSameCategory(category))
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package site.coduo.sync.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -10,6 +11,7 @@
import site.coduo.sync.controller.docs.SseDocs;
import site.coduo.sync.service.SseService;


@RequiredArgsConstructor
@RestController
public class SseController implements SseDocs {
Expand All @@ -22,4 +24,12 @@ public ResponseEntity<SseEmitter> createConnection(@PathVariable("key") final St

return ResponseEntity.ok(sseEmitter);
}

@DeleteMapping("/{key}/connect")
public ResponseEntity<Void> deleteConnection(@PathVariable("key") final String key) {
sseService.disconnectAll(key);

return ResponseEntity.noContent()
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public interface SseDocs {
@ApiResponse(responseCode = "200", description = "SSE 연결 성공 - event:connect\ndata:OK 메시지를 응답", content = @Content(mediaType = MediaType.TEXT_EVENT_STREAM_VALUE))
@ApiResponse(responseCode = "4xx", description = "SSE 연결 실패", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ApiErrorResponse.class)))
ResponseEntity<SseEmitter> createConnection(String key);

@Operation(summary = "특정 key에 속하는 SSE 연결을 모두 삭제한다.")
@ApiResponse(responseCode = "204", description = "SSE 삭제 성공 - event:close\ndata:OK 메시지를 응답", content = @Content(mediaType = MediaType.TEXT_EVENT_STREAM_VALUE))
ResponseEntity<Void> deleteConnection(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface EventStream {
SseEmitter connect();

void flush(String name, String message);

void close();
}
Loading

0 comments on commit 081d1e2

Please sign in to comment.