From d2f96226c4695f8eca4a943cb3d38173da158ca1 Mon Sep 17 00:00:00 2001 From: JiWoo Date: Sun, 21 Jan 2024 11:06:05 +0900 Subject: [PATCH] =?UTF-8?q?[Setting]=20Firebase=20=EC=84=A4=EC=A0=95=20(#2?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add submodules * setting(build.gradle.kts): Firebase 의존성 추가 * feat(FirebaseConfig): FirebaseConfig 설정 완료 * refactor(gitmodules): 서브모듈 path 상대경로로 수정 --- .gitmodules | 3 ++ build.gradle.kts | 3 ++ .../plantory/common/config/FirebaseConfig.kt | 52 +++++++++++++++++++ src/main/resources/application.yml | 4 ++ src/main/resources/config | 1 + 5 files changed, 63 insertions(+) create mode 100644 .gitmodules create mode 100644 src/main/kotlin/gdsc/plantory/common/config/FirebaseConfig.kt create mode 160000 src/main/resources/config diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f996b98 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/main/resources/config"] + path = src/main/resources/config + url = https://github.com/gdsc-konkuk/Plantory-Server-Config.git diff --git a/build.gradle.kts b/build.gradle.kts index 2999786..bc6f91b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -57,6 +57,9 @@ dependencies { implementation("com.querydsl:querydsl-jpa:5.0.0") kapt("com.querydsl:querydsl-apt:5.0.0:jpa") kapt("org.springframework.boot:spring-boot-configuration-processor") + + // firebase + implementation("com.google.firebase:firebase-admin:9.2.0") } tasks.withType { diff --git a/src/main/kotlin/gdsc/plantory/common/config/FirebaseConfig.kt b/src/main/kotlin/gdsc/plantory/common/config/FirebaseConfig.kt new file mode 100644 index 0000000..2578533 --- /dev/null +++ b/src/main/kotlin/gdsc/plantory/common/config/FirebaseConfig.kt @@ -0,0 +1,52 @@ +package gdsc.plantory.common.config + +import com.google.auth.oauth2.GoogleCredentials +import com.google.firebase.FirebaseApp +import com.google.firebase.FirebaseOptions +import com.google.firebase.messaging.FirebaseMessaging +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.io.ClassPathResource +import java.util.Optional + +@Configuration +class FirebaseConfig( + @Value("\${fcm.key.path}") + private val fcmPrivateKeyPath: String +) { + + @Bean + fun firebaseMessaging(): FirebaseMessaging { + val defaultFirebaseApp = defaultFirebaseApp() + + if (defaultFirebaseApp.isPresent) { + return FirebaseMessaging.getInstance(defaultFirebaseApp.get()) + } + + return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(createFirebaseOption())) + } + + private fun defaultFirebaseApp(): Optional { + val firebaseAppList = FirebaseApp.getApps() + + if (firebaseAppList == null || firebaseAppList.isEmpty()) { + return Optional.empty() + } + + return firebaseAppList.stream() + .filter { firebaseApp: FirebaseApp -> firebaseApp.name == FirebaseApp.DEFAULT_APP_NAME } + .findAny() + } + + private fun createFirebaseOption(): FirebaseOptions { + return FirebaseOptions.builder() + .setCredentials(createGoogleCredentials()) + .build() + } + + private fun createGoogleCredentials(): GoogleCredentials { + return GoogleCredentials + .fromStream(ClassPathResource(fcmPrivateKeyPath).inputStream) + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3a49803..cd8babe 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -20,3 +20,7 @@ local: companionPlant: image: directory: photos/companionPlant + +fcm: + key: + path: src/main/resources/config/google-services.json diff --git a/src/main/resources/config b/src/main/resources/config new file mode 160000 index 0000000..7897edf --- /dev/null +++ b/src/main/resources/config @@ -0,0 +1 @@ +Subproject commit 7897edf6aa6e3a328799b060bb61f744967da5fa