-
-
Notifications
You must be signed in to change notification settings - Fork 751
/
index.js
53 lines (47 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* React Starter Kit for Firebase
* https://github.com/kriasoft/react-firebase-starter
* Copyright (c) 2015-present Kriasoft | MIT License
*/
const fs = require('fs');
const dotenv = require('dotenv');
const express = require('express');
const firebase = require('firebase-admin');
const functions = require('firebase-functions');
if (process.env.NODE_ENV === 'production') {
process.env.APP_VERSION = fs.readFileSync('./VERSION', 'utf8').trim();
// Load API keys, secrets etc. from Firebase environment
// https://firebase.google.com/docs/functions/config-env
const { app: config } = functions.config();
Object.keys(config).forEach(key => {
process.env[key.toUpperCase()] =
typeof config[key] === 'object'
? JSON.stringify(config[key])
: config[key];
});
}
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });
dotenv.config({ path: '.env.local' });
dotenv.config({ path: '.env' });
// Configure Firebase Admin SDK
// https://firebase.google.com/docs/admin/setup
if (!firebase.apps.length) {
firebase.initializeApp({
credential: firebase.credential.cert(
JSON.parse(process.env.GCP_SERVICE_KEY),
),
});
}
if (process.env.NODE_ENV === 'production') {
// Server environment
exports.app = functions
.runWith({ memory: '2GB' })
.https.onRequest(require('./app').default);
} else {
// Local/dev environment
const app = express();
const db = require('./db').default;
app.use(require('./app').default);
module.exports.default = app;
module.exports.dispose = () => db.destroy();
}