This repository was archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (47 loc) · 1.79 KB
/
index.js
File metadata and controls
60 lines (47 loc) · 1.79 KB
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
54
55
56
57
58
59
60
const express = require('express')
const path = require('path')
const app = express()
const admin = require('firebase-admin')
const { init: initFirestore } = require('./model/firestore')
const { logger, stringify } = require('./helpers/logger')
const { applications, stats, users } = require('./routes')
const env = process.env.PROD ? 'production' : 'development'
const serviceAccount = process.env.PROD ? require('./firebase-admin-prod.json') : require('./firebase-admin-dev.json')
const config = require('./config.json')[env]
// Allow the server to parse JSON
app.use(express.json({ limit: '50mb' }))
// Log each request the server receives
app.use('*', (req, res, next) => {
logger.info(`HTTP request received: ${req.method} -> ${req.originalUrl}`)
// if (req.method !== 'GET') logger.debug(`Request Body: ${stringify(req.body)}`)
next()
})
// Log all errors
app.use((error, req, res, next) => {
logger.error(`Express error: ${stringify(error)}`)
res.sendStatus(error.status || 500)
})
// Initialize Firebase admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: config.firebaseUrl,
databaseAuthVariableOverride: 'cuhacking-admin'
})
// Initializing firestore
initFirestore(admin)
// Frontend
app.use(express.static(path.join(__dirname, './client/build')))
app.get(/^\/(?!api).*/, (req, res) => {
res.sendFile(path.join(__dirname, './client/build/index.html'))
})
// Backend routes
const backendRouter = express.Router()
backendRouter.use('/applications', applications)
// backendRouter.use('/stats', stats)
backendRouter.use('/users', users)
app.use('/api', backendRouter)
const port = process.env.PORT || 3000
app.listen(port)
logger.info(
`Admin console is listening on port ${port}${process.env.PROD ? ' in production mode' : ' in development mode'}.`
)