|
| 1 | +package com.bbebig.serviceserver.server.controller; |
| 2 | + |
| 3 | +import com.bbebig.commonmodule.global.response.code.CommonResponse; |
| 4 | +import com.bbebig.serviceserver.server.dto.request.ServerParticipateRequestDto; |
| 5 | +import com.bbebig.serviceserver.server.dto.response.PassportTestResponse; |
| 6 | +import com.bbebig.serviceserver.server.dto.response.ServerParticipateResponseDto; |
| 7 | +import com.bbebig.serviceserver.server.service.PassportTestClient; |
| 8 | +import com.bbebig.serviceserver.server.service.ServerService; |
| 9 | +import io.swagger.v3.oas.annotations.Operation; |
| 10 | +import io.swagger.v3.oas.annotations.media.Content; |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 13 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.springframework.web.bind.annotation.PathVariable; |
| 17 | +import org.springframework.web.bind.annotation.PostMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestBody; |
| 19 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 20 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 21 | +import org.springframework.web.bind.annotation.RestController; |
| 22 | + |
| 23 | +@Slf4j |
| 24 | +@RestController |
| 25 | +@RequestMapping("/servers") |
| 26 | +@RequiredArgsConstructor |
| 27 | +@Tag(name = "서버", description = "서버 관련 API") |
| 28 | +public class ServerTestController { |
| 29 | + |
| 30 | + private final ServerService serverService; |
| 31 | + private final PassportTestClient passportTestClient; |
| 32 | + |
| 33 | + @Operation(summary = "서버 참여 (서버 테스트 용)", description = "서버를 참여합니다. (서버 테스트 용)") |
| 34 | + @ApiResponses(value = { |
| 35 | + @ApiResponse(responseCode = "200", description = "서버 참여 성공", useReturnTypeSchema = true), |
| 36 | + @ApiResponse(responseCode = "400", description = "", content = @Content) |
| 37 | + }) |
| 38 | + @PostMapping("/{serverId}/participate/test") |
| 39 | + public CommonResponse<ServerParticipateResponseDto> participateServerTest( |
| 40 | + @RequestHeader("Authorization") String authorizationHeader, |
| 41 | + @PathVariable Long serverId |
| 42 | + ) { |
| 43 | + String jwt = extractJwtFromHeader(authorizationHeader); |
| 44 | + log.info("[Service] JWT로 Passport 서버 호출: {}", jwt); |
| 45 | + |
| 46 | + PassportTestResponse passportResponse = passportTestClient.getMemberIdByJwt(jwt); |
| 47 | + Long memberId = passportResponse.getResult().longValue(); |
| 48 | + |
| 49 | + log.info("[Service] Passport 서버 응답: memberId = {}", memberId); |
| 50 | + return CommonResponse.onSuccess(serverService.participateServer(memberId, serverId)); |
| 51 | + } |
| 52 | + |
| 53 | + private String extractJwtFromHeader(String authorizationHeader) { |
| 54 | + if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { |
| 55 | + return authorizationHeader.substring(7); |
| 56 | + } |
| 57 | + throw new IllegalArgumentException("JWT 토큰이 없습니다."); |
| 58 | + } |
| 59 | +} |
0 commit comments