Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
[Setting] Firebase 설정 (#25)
Browse files Browse the repository at this point in the history
* feat: add submodules

* setting(build.gradle.kts): Firebase 의존성 추가

* feat(FirebaseConfig): FirebaseConfig 설정 완료

* refactor(gitmodules): 서브모듈 path 상대경로로 수정
  • Loading branch information
zbqmgldjfh authored Jan 21, 2024
1 parent 21feb7d commit d2f9622
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/main/resources/config"]
path = src/main/resources/config
url = https://github.com/gdsc-konkuk/Plantory-Server-Config.git
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KotlinCompile> {
Expand Down
52 changes: 52 additions & 0 deletions src/main/kotlin/gdsc/plantory/common/config/FirebaseConfig.kt
Original file line number Diff line number Diff line change
@@ -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<FirebaseApp> {
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)
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ local:
companionPlant:
image:
directory: photos/companionPlant

fcm:
key:
path: src/main/resources/config/google-services.json
1 change: 1 addition & 0 deletions src/main/resources/config
Submodule config added at 7897ed

0 comments on commit d2f9622

Please sign in to comment.