-
Notifications
You must be signed in to change notification settings - Fork 1
refactor : 게시글 리스트 조회시 발생하던 오류 제거하는 코드 리펙토링 #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ out/ | |
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| /src/main/resources/*.env | ||
| *.env | ||
| *.yml | ||
|
|
||
| /src/main/generated/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,30 +9,43 @@ | |||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.context.annotation.Bean; | ||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.context.annotation.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.core.io.ClassPathResource; | ||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.core.io.Resource; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.FileInputStream; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.InputStream; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Slf4j | ||||||||||||||||||||||||||||||||||||||||||||||
| @Configuration | ||||||||||||||||||||||||||||||||||||||||||||||
| public class FireBaseConfig { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // 🚨 수정 포인트 1: String 대신 Resource 타입으로 주입받습니다. | ||||||||||||||||||||||||||||||||||||||||||||||
| @Value("${firebase.service-account.path}") | ||||||||||||||||||||||||||||||||||||||||||||||
| private String SERVICE_ACCOUNT_PATH; | ||||||||||||||||||||||||||||||||||||||||||||||
| private Resource serviceAccountResource; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @Bean | ||||||||||||||||||||||||||||||||||||||||||||||
| public FirebaseApp firebaseApp() { | ||||||||||||||||||||||||||||||||||||||||||||||
| try (FileInputStream serviceAccount = new FileInputStream(SERVICE_ACCOUNT_PATH)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||
| // 🚨 수정 포인트 2: 주입받은 Resource에서 바로 InputStream을 얻습니다. | ||||||||||||||||||||||||||||||||||||||||||||||
| InputStream serviceAccount = serviceAccountResource.getInputStream(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| FirebaseOptions options = FirebaseOptions.builder() | ||||||||||||||||||||||||||||||||||||||||||||||
| .setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||||||||||||||||||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Close the service account InputStream via try-with-resources The InputStream should be closed to avoid resource leaks. Wrap it in a try-with-resources. - try {
- // 🚨 수정 포인트 2: 주입받은 Resource에서 바로 InputStream을 얻습니다.
- InputStream serviceAccount = serviceAccountResource.getInputStream();
-
- FirebaseOptions options = FirebaseOptions.builder()
- .setCredentials(GoogleCredentials.fromStream(serviceAccount))
- .build();
+ try {
+ // 🚨 수정 포인트 2: 주입받은 Resource에서 바로 InputStream을 얻습니다.
+ try (InputStream serviceAccount = serviceAccountResource.getInputStream()) {
+ FirebaseOptions options = FirebaseOptions.builder()
+ .setCredentials(GoogleCredentials.fromStream(serviceAccount))
+ .build();And close the additional brace at the end of the init block: - if (FirebaseApp.getApps().isEmpty()) {
+ if (FirebaseApp.getApps().isEmpty()) {
log.info("Successfully initialized firebase app");
- return FirebaseApp.initializeApp(options);
- } else {
- return FirebaseApp.getInstance();
- }
+ return FirebaseApp.initializeApp(options);
+ } else {
+ log.info("Reusing existing firebase app instance");
+ return FirebaseApp.getInstance();
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| log.info("Successfully initialized firebase app"); | ||||||||||||||||||||||||||||||||||||||||||||||
| return FirebaseApp.initializeApp(options); | ||||||||||||||||||||||||||||||||||||||||||||||
| // 앱이 이미 초기화되었는지 확인 (중복 초기화 방지) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (FirebaseApp.getApps().isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.info("Successfully initialized firebase app"); | ||||||||||||||||||||||||||||||||||||||||||||||
| return FirebaseApp.initializeApp(options); | ||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| return FirebaseApp.getInstance(); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| } catch (IOException exception) { | ||||||||||||||||||||||||||||||||||||||||||||||
| log.error("Fail to initialize firebase app: {}", exception.getMessage(), exception); | ||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||
| // 초기화 실패 시 null 대신 예외를 던져서 애플리케이션이 문제를 인지하게 하는 것이 더 좋습니다. | ||||||||||||||||||||||||||||||||||||||||||||||
| throw new RuntimeException("Failed to initialize Firebase app.", exception); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Ensure property resolves to a valid Resource (prevent NPE at startup)
If
${firebase.service-account.path}is missing or invalid,serviceAccountResourcemay be null and cause a NullPointerException before the IOException catch. Add a null check to fail fast with a clear message.Optionally add this at the top of
firebaseApp():And add the import:
To verify configuration is present, run:
🏁 Script executed:
Length of output: 557
🏁 Script executed:
Length of output: 2044
Fail-fast if firebase.service-account.path doesn't resolve (prevent NPE at startup)
serviceAccountResource is injected and used immediately via getInputStream(); if the property is missing/invalid the field can be null and cause an NPE. Add a null (and optionally existence/readability) check before use.
Suggested change (minimal):
Optionally also check serviceAccountResource.exists() / isReadable() and throw a clear IllegalStateException if false.
📝 Committable suggestion
🤖 Prompt for AI Agents