Skip to content

Commit

Permalink
Merge pull request #3 from France-Travail/initializeFirebase
Browse files Browse the repository at this point in the history
Create function to initialize firebase
  • Loading branch information
EricDisdero authored Jul 29, 2024
2 parents 63a4315 + f56f28c commit 1504174
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
38 changes: 30 additions & 8 deletions translation-app-assessment/backend/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const express = require('express')
const bodyParser = require('body-parser')
const Moment = require('moment')
const firebaseAdmin = require('firebase-admin')
const fs = require('fs');

const firebaseConfig = JSON.parse(fs.readFileSync('/secrets/firebase-config.json', 'utf8'));
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManagerServiceClient = new SecretManagerServiceClient();
const Monitoring = require('@google-cloud/monitoring')

// Init express.js app
Expand All @@ -19,7 +18,7 @@ const cookieParser = require('cookie-parser')
app.use(cookieParser())

app.disable('x-powered-by')
require("dotenv");
require('dotenv');
const cors = require('cors');

const corsOptions = {
Expand All @@ -32,10 +31,11 @@ const corsOptions = {
app.use(cors(corsOptions));
// Init project and services
const projectId = process.env.GCP_PROJECT
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(firebaseConfig)
});
const firestore = firebaseAdmin.firestore()

// Call initializeFirebase during startup
initializeFirebase();


// Function to write monitoring "heartbeat" that cleanup has run
const writeMonitoring = async () => {
const monitoringOptions = {
Expand Down Expand Up @@ -83,6 +83,8 @@ app.get('/', async (req, res) => {

app.post('/', async (req, res, next) => {
try {


await kpi()
// Delete chat that have been expired for an hour or longer
const deletionPromises = []
Expand Down Expand Up @@ -412,3 +414,23 @@ async function deleteInactiveUsers() {
}
}

// Function to initialize Firebase
async function initializeFirebase() {
try {
const [version] = await secretManagerServiceClient.accessSecretVersion({
name: `projects/${projectId}/secrets/firebase-config/versions/latest`,
});

// Extract the secret payload as a string
const payload = version.payload.data.toString('utf8');

// Initialize Firebase Admin SDK
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(JSON.parse(payload)),
});

console.log('Firebase Admin initialized successfully');
} catch (error) {
console.error('Error initializing Firebase Admin:', error);
}
}
3 changes: 2 additions & 1 deletion translation-app-assessment/backend/cleanup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"main": "index.js",
"dependencies": {
"@google-cloud/monitoring": "^2.2.0",
"@google-cloud/secret-manager": "^5.6.0",
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.17.1",
"find-config": "^1.0.0",
"firebase-admin": "^9.5.0",
"moment": "^2.29.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
Expand Down

0 comments on commit 1504174

Please sign in to comment.