-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
gateway-service/src/main/kotlin/com/grepp/quizy/api/GatewayFallbackController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.grepp.quizy.api | ||
|
||
import com.grepp.quizy.common.api.ApiResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/fallback") | ||
class GatewayFallbackController { | ||
@GetMapping("/user") | ||
fun userServiceFallback(): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "User service is temporarily unavailable")) | ||
} | ||
|
||
@GetMapping("/quiz") | ||
fun quizServiceFallback(): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Quiz service is temporarily unavailable")) | ||
} | ||
|
||
@GetMapping("/game") | ||
fun gameServiceFallback(): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Game service is temporarily unavailable")) | ||
} | ||
|
||
@GetMapping("/ws") | ||
fun webSocketServiceFallback(): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body( | ||
ApiResponse.error( | ||
HttpStatus.SERVICE_UNAVAILABLE.name, | ||
"Game webSocket service is temporarily unavailable" | ||
) | ||
) | ||
} | ||
|
||
@GetMapping("/matching") | ||
fun matchingServiceFallback(): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.SERVICE_UNAVAILABLE) | ||
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Matching service is temporarily unavailable")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
gateway-service/src/main/kotlin/com/grepp/quizy/webclient/UserClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.grepp.quizy.webclient | ||
|
||
import reactor.core.publisher.Mono | ||
|
||
interface UserClient { | ||
fun validateUser(userId: Long): Mono<Unit> | ||
} |
39 changes: 39 additions & 0 deletions
39
gateway-service/src/main/kotlin/com/grepp/quizy/webclient/UserClientImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.grepp.quizy.webclient | ||
|
||
import com.grepp.quizy.exception.CustomJwtException | ||
import com.grepp.quizy.user.RedisTokenRepository | ||
import com.grepp.quizy.user.UserId | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import reactor.core.publisher.Mono | ||
|
||
@Component | ||
class UserClientImpl( | ||
private val webClient: WebClient, | ||
private val redisTokenRepository: RedisTokenRepository | ||
) : UserClient { | ||
|
||
@Value("\${service.user.url}") | ||
private lateinit var BASE_URL: String | ||
|
||
|
||
override fun validateUser(userId: Long): Mono<Unit> { | ||
if (redisTokenRepository.isExistUser(UserId(userId))) { | ||
return Mono.just(Unit) | ||
} | ||
|
||
return webClient.get() | ||
.uri("$BASE_URL/api/internal/user/validate/$userId") | ||
.retrieve() | ||
.toEntity(Unit::class.java) | ||
.handle<Unit> { response, sink -> | ||
when (response.statusCode) { | ||
HttpStatus.OK -> sink.next(Unit) | ||
HttpStatus.UNAUTHORIZED -> sink.error(CustomJwtException.JwtUnknownException) | ||
else -> sink.error(CustomJwtException.NotExistUserException) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters