fix: prisma connection exhaustion & firebase lazy init
- prisma/schema.prisma: tambah binaryTargets debian & linux-musl untuk Docker - src/lib/prisma.ts: pakai global singleton di dev & prod, hapus eager $connect() - src/lib/firebase-admin.ts: lazy initialization agar tidak crash saat build time - .env.example: lengkap dengan semua env variable + connection_limit & pool_timeout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
// lib/firebase-admin.ts
|
||||
import { cert, getApp, getApps, initializeApp } from 'firebase-admin/app';
|
||||
import { getMessaging } from 'firebase-admin/messaging';
|
||||
import { getMessaging, Messaging } from 'firebase-admin/messaging';
|
||||
|
||||
// Ambil dari environment
|
||||
const serviceAccount = {
|
||||
projectId: process.env.FIREBASE_ADMIN_PROJECT_ID,
|
||||
clientEmail: process.env.FIREBASE_ADMIN_CLIENT_EMAIL,
|
||||
privateKey: process.env.FIREBASE_ADMIN_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||
};
|
||||
function getAdminApp() {
|
||||
if (getApps().length > 0) return getApp();
|
||||
|
||||
if (!serviceAccount.projectId || !serviceAccount.clientEmail || !serviceAccount.privateKey) {
|
||||
throw new Error('Firebase Admin credentials are missing in environment variables');
|
||||
const privateKey = process.env.FIREBASE_ADMIN_PRIVATE_KEY?.replace(/\\n/g, '\n');
|
||||
const projectId = process.env.FIREBASE_ADMIN_PROJECT_ID;
|
||||
const clientEmail = process.env.FIREBASE_ADMIN_CLIENT_EMAIL;
|
||||
|
||||
if (!projectId || !clientEmail || !privateKey) {
|
||||
throw new Error('Firebase Admin credentials are missing in environment variables');
|
||||
}
|
||||
|
||||
return initializeApp({
|
||||
credential: cert({ projectId, clientEmail, privateKey }),
|
||||
projectId,
|
||||
});
|
||||
}
|
||||
|
||||
// Inisialisasi hanya sekali
|
||||
const app = !getApps().length
|
||||
? initializeApp({
|
||||
credential: cert(serviceAccount),
|
||||
projectId: serviceAccount.projectId,
|
||||
})
|
||||
: getApp();
|
||||
|
||||
export const adminMessaging = getMessaging(app);
|
||||
export const adminMessaging: Pick<Messaging, 'send' | 'sendEachForMulticast'> = {
|
||||
send: (message) => getMessaging(getAdminApp()).send(message),
|
||||
sendEachForMulticast: (message) => getMessaging(getAdminApp()).sendEachForMulticast(message),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user