From 17b989ecb731b3b4ade1545442d977a202254005 Mon Sep 17 00:00:00 2001 From: DongGeon0908 Date: Sat, 13 Jul 2024 12:34:37 +0900 Subject: [PATCH] feat: fcm client config --- .github/workflows/gradle.yml | 12 ++++ build.gradle.kts | 3 + buildSrc/src/main/kotlin/DependencyVersion.kt | 3 + .../hero/alignlab/client/WebClientFactory.kt | 70 +++++++++++++++++++ .../hero/alignlab/client/fcm/PushService.kt | 10 +++ .../alignlab/client/fcm/client/FcmClient.kt | 4 ++ .../client/fcm/client/ReactiveFcmClient.kt | 9 +++ .../alignlab/client/fcm/config/FcmConfig.kt | 32 +++++++++ .../alignlab/client/fcm/model/FcmModel.kt | 3 + src/main/resources/config/application.yml | 15 ++++ 10 files changed, 161 insertions(+) create mode 100644 src/main/kotlin/com/hero/alignlab/client/WebClientFactory.kt create mode 100644 src/main/kotlin/com/hero/alignlab/client/fcm/PushService.kt create mode 100644 src/main/kotlin/com/hero/alignlab/client/fcm/client/FcmClient.kt create mode 100644 src/main/kotlin/com/hero/alignlab/client/fcm/client/ReactiveFcmClient.kt create mode 100644 src/main/kotlin/com/hero/alignlab/client/fcm/config/FcmConfig.kt create mode 100644 src/main/kotlin/com/hero/alignlab/client/fcm/model/FcmModel.kt diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index ab0320a..ef70b5e 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -60,6 +60,18 @@ jobs: # Encrypt 환경 변수 주입 encrypt.key: ${{ secrets.encrypt.key }} encrypt.algorithm: ${{ secrets.encrypt.algorithm }} + # FCM 환경 변수 주입 + push.fcm.type: ${{ secrets.push.fcm.type }} + push.fcm.project-id: ${{ secrets.push.fcm.project-id }} + push.fcm.private-key-id: ${{ secrets.push.fcm.private-key-id }} + push.fcm.private-key: ${{ secrets.push.fcm.private-key }} + push.fcm.client-email: ${{ secrets.push.fcm.client-email }} + push.fcm.client-id: ${{ secrets.push.fcm.client-id }} + push.fcm.auth-uri: ${{ secrets.push.fcm.auth-uri }} + push.fcm.token-uri: ${{ secrets.push.fcm.token-uri }} + push.fcm.auth-provider-x509-cert-url: ${{ secrets.push.fcm.auth-provider-x509-cert-url }} + push.fcm.client-x509-cert-url: ${{ secrets.push.fcm.client-x509-cert-url }} + push.fcm.universe-domain: ${{ secrets.push.fcm.universe-domain }} # gradlew 파일 실행권한 설정 - name: Grant execute permission for gradlew diff --git a/build.gradle.kts b/build.gradle.kts index 54d5659..40f591a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -88,6 +88,9 @@ dependencies { /** jwt */ implementation("com.auth0:java-jwt:${DependencyVersion.AUTH_JWT}") + /** fcm */ + implementation("com.google.firebase:firebase-admin:${DependencyVersion.FCM_ADMIN}") + /** test */ testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("io.projectreactor:reactor-test") diff --git a/buildSrc/src/main/kotlin/DependencyVersion.kt b/buildSrc/src/main/kotlin/DependencyVersion.kt index f50c155..147d6ee 100644 --- a/buildSrc/src/main/kotlin/DependencyVersion.kt +++ b/buildSrc/src/main/kotlin/DependencyVersion.kt @@ -14,6 +14,9 @@ object DependencyVersion { /** auth0-jwt */ const val AUTH_JWT = "4.4.0" + /** fcm */ + const val FCM_ADMIN = "9.3.0" + /** test */ const val TEST_CONTAINER_MYSQL = "1.19.8" const val P6SPY_LOG = "1.9.1" diff --git a/src/main/kotlin/com/hero/alignlab/client/WebClientFactory.kt b/src/main/kotlin/com/hero/alignlab/client/WebClientFactory.kt new file mode 100644 index 0000000..52335fb --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/WebClientFactory.kt @@ -0,0 +1,70 @@ +package com.hero.alignlab.client + +import io.netty.channel.ChannelOption +import io.netty.handler.timeout.ReadTimeoutHandler +import io.netty.handler.timeout.WriteTimeoutHandler +import org.springframework.http.client.reactive.ClientHttpConnector +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.Connection +import reactor.netty.http.client.HttpClient +import java.util.concurrent.TimeUnit + +class WebClientFactory { + companion object { + fun generate( + baseUrl: String? = null, + connectionTimeoutMillis: Int = 1000, + readTimeoutMillis: Long = 1000, + writeTimeoutMillis: Long = 1000, + ): WebClient { + val clientHttpConnector = createFactory( + connectionTimeoutMillis = connectionTimeoutMillis, + readTimeoutMillis = readTimeoutMillis, + writeTimeoutMillis = writeTimeoutMillis + ) + + val webClient = WebClient.builder() + + if (baseUrl != null) { + webClient.baseUrl(baseUrl) + } + + return webClient + .codecs { it.defaultCodecs().enableLoggingRequestDetails(true) } + .clientConnector(clientHttpConnector) + .build() + } + + private fun createFactory( + connectionTimeoutMillis: Int, + readTimeoutMillis: Long, + writeTimeoutMillis: Long, + ): ClientHttpConnector { + val httpClient = httpClient( + connectionTimeoutMillis = connectionTimeoutMillis, + readTimeoutMillis = readTimeoutMillis, + writeTimeoutMillis = writeTimeoutMillis + ) + + return ReactorClientHttpConnector(httpClient) + } + + private fun httpClient( + connectionTimeoutMillis: Int, + readTimeoutMillis: Long, + writeTimeoutMillis: Long, + ): HttpClient { + return HttpClient.create() + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutMillis) + .doOnConnected { connection: Connection -> + connection + .addHandlerLast(ReadTimeoutHandler(readTimeoutMillis, TimeUnit.MILLISECONDS)) + .addHandlerLast(WriteTimeoutHandler(writeTimeoutMillis, TimeUnit.MILLISECONDS)) + }.apply { + this.warmup().block() + this.compress(true) + } + } + } +} diff --git a/src/main/kotlin/com/hero/alignlab/client/fcm/PushService.kt b/src/main/kotlin/com/hero/alignlab/client/fcm/PushService.kt new file mode 100644 index 0000000..7b8cd8f --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/fcm/PushService.kt @@ -0,0 +1,10 @@ +package com.hero.alignlab.client.fcm + +import com.hero.alignlab.client.fcm.client.FcmClient +import org.springframework.stereotype.Service + +@Service +class PushService( + private val fcmClient: FcmClient, +) { +} diff --git a/src/main/kotlin/com/hero/alignlab/client/fcm/client/FcmClient.kt b/src/main/kotlin/com/hero/alignlab/client/fcm/client/FcmClient.kt new file mode 100644 index 0000000..1706e1c --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/fcm/client/FcmClient.kt @@ -0,0 +1,4 @@ +package com.hero.alignlab.client.fcm.client + +interface FcmClient { +} diff --git a/src/main/kotlin/com/hero/alignlab/client/fcm/client/ReactiveFcmClient.kt b/src/main/kotlin/com/hero/alignlab/client/fcm/client/ReactiveFcmClient.kt new file mode 100644 index 0000000..7b1d9fb --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/fcm/client/ReactiveFcmClient.kt @@ -0,0 +1,9 @@ +package com.hero.alignlab.client.fcm.client + +import com.hero.alignlab.client.fcm.config.FcmProperties +import io.github.oshai.kotlinlogging.KotlinLogging + +class ReactiveFcmClient(fcmProperties: FcmProperties) : FcmClient { + private val logger = KotlinLogging.logger {} + +} diff --git a/src/main/kotlin/com/hero/alignlab/client/fcm/config/FcmConfig.kt b/src/main/kotlin/com/hero/alignlab/client/fcm/config/FcmConfig.kt new file mode 100644 index 0000000..8b107c7 --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/fcm/config/FcmConfig.kt @@ -0,0 +1,32 @@ +package com.hero.alignlab.client.fcm.config + +import com.hero.alignlab.client.fcm.client.FcmClient +import com.hero.alignlab.client.fcm.client.ReactiveFcmClient +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +@EnableConfigurationProperties(FcmProperties::class) +class FcmConfig { + @Bean + fun fcmClient(fcmProperties: FcmProperties): FcmClient { + return ReactiveFcmClient(fcmProperties) + } +} + +@ConfigurationProperties(prefix = "push.fcm") +data class FcmProperties( + val type: String, + val projectId: String, + val privateKeyId: String, + val privateKey: String, + val clientEmail: String, + val clientId: String, + val authUri: String, + val tokenUri: String, + val authProviderX509CertUrl: String, + val clientX509CertUrl: String, + val universeDomain: String +) diff --git a/src/main/kotlin/com/hero/alignlab/client/fcm/model/FcmModel.kt b/src/main/kotlin/com/hero/alignlab/client/fcm/model/FcmModel.kt new file mode 100644 index 0000000..d1883e5 --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/client/fcm/model/FcmModel.kt @@ -0,0 +1,3 @@ +package com.hero.alignlab.client.fcm.model + + diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml index ee42882..405427e 100644 --- a/src/main/resources/config/application.yml +++ b/src/main/resources/config/application.yml @@ -50,3 +50,18 @@ auth: encrypt: key: algorithm: + +push: + fcm: + type: + project-id: + private-key-id: + private-key: + client-email: + client-id: + auth-uri: + token-uri: + auth-provider-x509-cert-url: + client-x509-cert-url: + universe-domain: +