Skip to content

Commit

Permalink
feat: connection closed dev resource
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 24, 2024
1 parent 40b8e56 commit 161424a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
# Encrypt 환경 변수 주입
encrypt.key: ${{ secrets.ENCRYPT_KEY }}
encrypt.algorithm: ${{ secrets.ENCRYPT_ALGORITHM }}
# dev resource 환경 변수 주입
hero-alignlab.dev.resource.key: ${{ secrets.HERO_ALIGNLAB_DEV_RESOURCE_KEY }}

# Secret Setup - application-prod.yml
- name: Inject env-values to application-prod.yml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.hero.alignlab.config.dev

import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.NoAuthorityException
import jakarta.validation.constraints.NotBlank
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(DevResourceCheckConfig.DevResourceCheckProperties::class)
class DevResourceCheckConfig(
_devResourceCheckProperties: DevResourceCheckProperties
) {
@ConfigurationProperties(prefix = "hero-alignlab.dev.resource")
data class DevResourceCheckProperties(
@field:NotBlank
var key: String = "",
)

init {
devResourceCheckProperties = _devResourceCheckProperties
}

companion object {
const val DEV_AUTH_TOKEN_KEY = "X-HERO-DEV-AUTH-TOKEN"

private lateinit var devResourceCheckProperties: DevResourceCheckProperties

suspend fun <T> devResource(key: String, function: suspend () -> T): T {
if (devResourceCheckProperties.key == key) {
return function.invoke()
}
throw NoAuthorityException(ErrorCode.NO_AUTHORITY_ERROR)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hero.alignlab.config.swagger

import com.hero.alignlab.config.dev.DevResourceCheckConfig.Companion.DEV_AUTH_TOKEN_KEY
import com.hero.alignlab.domain.auth.model.AUTH_TOKEN_KEY
import com.hero.alignlab.domain.auth.model.AuthUser
import io.swagger.v3.oas.models.Components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ class ReactiveConcurrentUserWebSocketHandler(
}
}

fun forceCloseAllSessions() {
concurrentUserMap.forEach { (_, sessions) ->
sessions.forEach { (_, session) ->
session.close().subscribe()
}
}

// 모든 세션이 종료된 후, 맵을 비울 수 있습니다.
concurrentUserMap.clear()
}
}

data class ConcurrentMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.hero.alignlab.domain.user.resource
package com.hero.alignlab.domain.dev.resource

import com.hero.alignlab.common.extension.wrapOk
import com.hero.alignlab.config.dev.DevResourceCheckConfig.Companion.devResource
import com.hero.alignlab.config.swagger.SwaggerTag.DEV_TAG
import com.hero.alignlab.domain.user.application.UserInfoService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@Tag(name = DEV_TAG)
@RestController
Expand All @@ -21,5 +19,8 @@ class DevUserInfoResource(
@GetMapping("/api/dev/v1/users/{id}")
suspend fun getUserInfo(
@PathVariable id: Long,
) = userInfoService.getUserInfo(id).wrapOk()
@RequestHeader("X-HERO-DEV-TOKEN") token: String
) = devResource(token) {
userInfoService.getUserInfo(id).wrapOk()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hero.alignlab.domain.dev.resource

import com.hero.alignlab.config.dev.DevResourceCheckConfig.Companion.devResource
import com.hero.alignlab.config.swagger.SwaggerTag.DEV_TAG
import com.hero.alignlab.config.web.ReactiveConcurrentUserWebSocketHandler
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Tag(name = DEV_TAG)
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class DevWebsocketResource(
private val reactiveConcurrentUserWebSocketHandler: ReactiveConcurrentUserWebSocketHandler
) {
@Operation(summary = "websocket connection closed")
@PostMapping("/api/dev/v1/websocket/connection-closed")
suspend fun closedConnection(
@RequestHeader("X-HERO-DEV-TOKEN") token: String
) = devResource(token) {
reactiveConcurrentUserWebSocketHandler.forceCloseAllSessions()
}
}
5 changes: 5 additions & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ auth:
encrypt:
key:
algorithm:

hero-alignlab:
dev:
resource:
key:

0 comments on commit 161424a

Please sign in to comment.