2023-10-18 04:32:14 -04:00
|
|
|
// Metrics must be initialized before importing anything else
|
|
|
|
require('@overleaf/metrics/initialize')
|
|
|
|
|
2024-11-08 05:21:56 -05:00
|
|
|
const Events = require('node:events')
|
2020-11-25 06:57:23 -05:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-01-03 05:06:19 -05:00
|
|
|
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-07-23 08:35:52 -04:00
|
|
|
logger.initialize(process.env.METRICS_APP_NAME || 'filestore')
|
|
|
|
|
2021-07-12 12:47:19 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-01-03 05:06:19 -05:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
const fileController = require('./app/js/FileController')
|
|
|
|
const keyBuilder = require('./app/js/KeyBuilder')
|
|
|
|
const healthCheckController = require('./app/js/HealthCheckController')
|
2020-01-03 05:06:19 -05:00
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
const RequestLogger = require('./app/js/RequestLogger')
|
|
|
|
|
2023-08-02 10:09:11 -04:00
|
|
|
Events.setMaxListeners(20)
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
const app = express()
|
2019-12-16 05:52:40 -05:00
|
|
|
|
2020-02-14 05:50:52 -05:00
|
|
|
app.use(RequestLogger.middleware)
|
2020-01-14 07:02:39 -05:00
|
|
|
|
2020-01-03 05:06:19 -05:00
|
|
|
if (settings.sentry && settings.sentry.dsn) {
|
2019-12-16 05:52:42 -05:00
|
|
|
logger.initializeErrorReporting(settings.sentry.dsn)
|
2019-12-16 05:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-05-22 06:36:51 -04:00
|
|
|
Metrics.open_sockets.monitor(true)
|
2020-01-03 05:06:19 -05:00
|
|
|
Metrics.memory.monitor(logger)
|
|
|
|
if (Metrics.event_loop) {
|
2019-12-16 05:52:42 -05:00
|
|
|
Metrics.event_loop.monitor(logger)
|
2019-12-16 05:52:40 -05:00
|
|
|
}
|
2023-05-30 09:25:50 -04:00
|
|
|
Metrics.leaked_sockets.monitor(logger)
|
2019-12-16 05:52:40 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
app.use(function (req, res, next) {
|
2019-12-16 05:52:42 -05:00
|
|
|
Metrics.inc('http-request')
|
2020-01-03 05:06:19 -05:00
|
|
|
next()
|
2019-12-16 05:52:42 -05:00
|
|
|
})
|
2019-12-16 05:52:40 -05:00
|
|
|
|
2024-01-17 06:30:33 -05:00
|
|
|
// Handle requests that come in after we've started shutting down
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (settings.shuttingDown) {
|
|
|
|
logger.warn(
|
|
|
|
{ req, timeSinceShutdown: Date.now() - settings.shutDownTime },
|
|
|
|
'request received after shutting down'
|
|
|
|
)
|
|
|
|
// We don't want keep-alive connections to be kept open when the server is shutting down.
|
|
|
|
res.set('Connection', 'close')
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
|
|
|
|
app.head(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
app.put(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
bodyParser.json(),
|
|
|
|
fileController.copyFile
|
|
|
|
)
|
2020-01-03 05:06:19 -05:00
|
|
|
app.delete(
|
2019-12-16 05:52:42 -05:00
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.deleteFile
|
|
|
|
)
|
2020-03-14 10:56:29 -04:00
|
|
|
app.delete(
|
|
|
|
'/project/:project_id',
|
|
|
|
keyBuilder.userProjectKeyMiddleware,
|
|
|
|
fileController.deleteProject
|
|
|
|
)
|
2019-12-16 05:52:42 -05:00
|
|
|
|
2024-08-02 07:17:07 -04:00
|
|
|
app.get(
|
|
|
|
'/project/:project_id/size',
|
|
|
|
keyBuilder.userProjectKeyMiddleware,
|
|
|
|
fileController.directorySize
|
|
|
|
)
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
app.head(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/template/:template_id/v/:version/:format/:sub_type',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
|
2020-01-07 10:05:51 -05:00
|
|
|
app.get(
|
|
|
|
'/bucket/:bucket/key/*',
|
|
|
|
keyBuilder.bucketFileKeyMiddleware,
|
|
|
|
fileController.getFile
|
|
|
|
)
|
2019-12-16 05:52:42 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
app.get('/status', function (req, res) {
|
2024-01-17 06:30:33 -05:00
|
|
|
if (settings.shuttingDown) {
|
|
|
|
res.sendStatus(503) // Service unavailable
|
|
|
|
} else {
|
|
|
|
res.send('filestore is up')
|
|
|
|
}
|
2019-12-16 05:52:42 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/health_check', healthCheckController.check)
|
|
|
|
|
2020-02-14 05:50:52 -05:00
|
|
|
app.use(RequestLogger.errorHandler)
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
const port = settings.internal.filestore.port || 3009
|
2021-08-27 02:19:14 -04:00
|
|
|
const host = settings.internal.filestore.host || '0.0.0.0'
|
2019-12-16 05:52:42 -05:00
|
|
|
|
2024-07-05 05:36:32 -04:00
|
|
|
let server = null
|
2019-12-16 05:52:42 -05:00
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2024-07-05 05:36:32 -04:00
|
|
|
server = app.listen(port, host, error => {
|
2019-12-16 07:14:06 -05:00
|
|
|
if (error) {
|
2024-06-27 04:10:31 -04:00
|
|
|
logger.error({ err: error }, 'Error starting Filestore')
|
2019-12-16 07:14:06 -05:00
|
|
|
throw error
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(`Filestore starting up, listening on ${host}:${port}`)
|
2019-12-16 07:14:06 -05:00
|
|
|
})
|
2019-12-16 05:52:42 -05:00
|
|
|
}
|
2019-12-16 05:52:40 -05:00
|
|
|
|
2020-02-27 05:33:48 -05:00
|
|
|
process
|
|
|
|
.on('unhandledRejection', (reason, p) => {
|
|
|
|
logger.err(reason, 'Unhandled Rejection at Promise', p)
|
|
|
|
})
|
2021-07-13 07:04:46 -04:00
|
|
|
.on('uncaughtException', err => {
|
2020-02-27 05:33:48 -05:00
|
|
|
logger.err(err, 'Uncaught Exception thrown')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
2024-01-17 06:30:33 -05:00
|
|
|
function handleShutdownSignal(signal) {
|
|
|
|
logger.info({ signal }, 'received interrupt, cleaning up')
|
|
|
|
if (settings.shuttingDown) {
|
|
|
|
logger.warn({ signal }, 'already shutting down, ignoring interrupt')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
settings.shuttingDown = true
|
|
|
|
settings.shutDownTime = Date.now()
|
2024-07-05 05:36:32 -04:00
|
|
|
// stop accepting new connections, the callback is called when existing connections have finished
|
|
|
|
server.close(() => {
|
|
|
|
logger.info({ signal }, 'server closed')
|
2024-01-17 06:30:33 -05:00
|
|
|
process.exit()
|
2024-07-05 05:36:32 -04:00
|
|
|
})
|
|
|
|
// close idle http keep-alive connections
|
|
|
|
server.closeIdleConnections()
|
|
|
|
setTimeout(() => {
|
|
|
|
logger.info({ signal }, 'shutdown timed out, exiting')
|
|
|
|
// close all connections immediately
|
|
|
|
server.closeAllConnections()
|
|
|
|
// exit after a short delay to allow for cleanup
|
|
|
|
setTimeout(() => {
|
|
|
|
process.exit()
|
|
|
|
}, 100)
|
|
|
|
}, settings.delayShutdownMs)
|
2024-01-17 06:30:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('SIGTERM', handleShutdownSignal)
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
module.exports = app
|