diff --git a/server-info-spring-web/build.gradle.kts b/server-info-spring-web/build.gradle.kts new file mode 100644 index 0000000..a103ea4 --- /dev/null +++ b/server-info-spring-web/build.gradle.kts @@ -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") +} diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/EnableServerInfo.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/EnableServerInfo.kt new file mode 100644 index 0000000..fcd654e --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/EnableServerInfo.kt @@ -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 diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/advices/ServerInfoAdvice.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/advices/ServerInfoAdvice.kt new file mode 100644 index 0000000..21d0ccf --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/advices/ServerInfoAdvice.kt @@ -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 { + + override fun supports( + returnType: MethodParameter, + converterType: Class> + ): Boolean { + return true //TODO(ADD CHECK) + } + + override fun beforeBodyWrite( + body: Any?, + returnType: MethodParameter, + selectedContentType: MediaType, + selectedConverterType: Class>, + request: ServerHttpRequest, + response: ServerHttpResponse + ): Any? { + return serverInfoService.addHeader(response) + } + +} diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/configurations/ServerInfo.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/configurations/ServerInfo.kt new file mode 100644 index 0000000..69a2c33 --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/configurations/ServerInfo.kt @@ -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 diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/properties/ServerInfoProperties.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/properties/ServerInfoProperties.kt new file mode 100644 index 0000000..9b93273 --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/properties/ServerInfoProperties.kt @@ -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 +) diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoService.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoService.kt new file mode 100644 index 0000000..987061c --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoService.kt @@ -0,0 +1,9 @@ +package ru.touchin.info.services + +import org.springframework.http.server.ServerHttpResponse + +interface ServerInfoService { + + fun addHeader(response: ServerHttpResponse): ServerHttpResponse + +} diff --git a/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoServiceImpl.kt b/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoServiceImpl.kt new file mode 100644 index 0000000..2fdbc7f --- /dev/null +++ b/server-info-spring-web/src/main/kotlin/ru/touchin/info/services/ServerInfoServiceImpl.kt @@ -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 + } + +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 1887c54..455433d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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")