Added new module serverInfo

This commit is contained in:
Denis Kazantsev 2022-04-07 21:40:12 +03:00
parent be1ef45fb2
commit cffefac613
8 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,13 @@
plugins {
id("kotlin")
id("kotlin-spring")
id("maven-publish")
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

View File

@ -0,0 +1,7 @@
package ru.touchin.info
import org.springframework.context.annotation.Import
import ru.touchin.info.configurations.ServerInfo
@Import(value = [ServerInfo::class])
annotation class EnableServerInfo

View File

@ -0,0 +1,35 @@
package ru.touchin.info.advices
import org.springframework.core.MethodParameter
import org.springframework.http.MediaType
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
import ru.touchin.info.services.ServerInfoService
@RestControllerAdvice
class ServerInfoAdvice(
private val serverInfoService: ServerInfoService
): ResponseBodyAdvice<Any> {
override fun supports(
returnType: MethodParameter,
converterType: Class<out HttpMessageConverter<*>>
): Boolean {
return true //TODO(ADD CHECK)
}
override fun beforeBodyWrite(
body: Any?,
returnType: MethodParameter,
selectedContentType: MediaType,
selectedConverterType: Class<out HttpMessageConverter<*>>,
request: ServerHttpRequest,
response: ServerHttpResponse
): Any? {
return serverInfoService.addHeader(response)
}
}

View File

@ -0,0 +1,9 @@
package ru.touchin.info.configurations
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
@Suppress("SpringFacetCodeInspection")
@Configuration
@ComponentScan("ru.touchin.info.advices", "ru.touchin.info.services")
class ServerInfo

View File

@ -0,0 +1,10 @@
package ru.touchin.info.properties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding
@ConstructorBinding
@ConfigurationProperties(prefix = "server.info")
data class ServerInfoProperties(
val buildNumber: String
)

View File

@ -0,0 +1,9 @@
package ru.touchin.info.services
import org.springframework.http.server.ServerHttpResponse
interface ServerInfoService {
fun addHeader(response: ServerHttpResponse): ServerHttpResponse
}

View File

@ -0,0 +1,18 @@
package ru.touchin.info.services
import org.springframework.http.server.ServerHttpResponse
import ru.touchin.info.properties.ServerInfoProperties
class ServerInfoServiceImpl(
private val serverInfoProperties: ServerInfoProperties
) : ServerInfoService {
override fun addHeader(response: ServerHttpResponse): ServerHttpResponse {
response
.headers
.add("X-App-Build-Version", serverInfoProperties.buildNumber)
return response
}
}

View File

@ -51,3 +51,4 @@ include("security-resource-server-custom-jwt-configuration")
include("security-resource-server-test-jwt-configuration")
include("security-jwt-common")
include("s3-storage")
include("server-info-spring-web")