2023-08-02 14:09:11 +00:00
|
|
|
const Events = require('events')
|
2020-11-25 11:57:23 +00:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-07-22 15:57:41 +00:00
|
|
|
Metrics.initialize(process.env.METRICS_APP_NAME || 'filestore')
|
2020-01-03 10:06:19 +00:00
|
|
|
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-07-23 12:35:52 +00:00
|
|
|
logger.initialize(process.env.METRICS_APP_NAME || 'filestore')
|
|
|
|
|
2021-07-12 16:47:19 +00:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-01-03 10:06:19 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
const fileController = require('./app/js/FileController')
|
|
|
|
const keyBuilder = require('./app/js/KeyBuilder')
|
|
|
|
const healthCheckController = require('./app/js/HealthCheckController')
|
2020-01-03 10:06:19 +00:00
|
|
|
|
2020-01-07 21:19:26 +00:00
|
|
|
const RequestLogger = require('./app/js/RequestLogger')
|
|
|
|
|
2023-08-02 14:09:11 +00:00
|
|
|
Events.setMaxListeners(20)
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
const app = express()
|
2019-12-16 10:52:40 +00:00
|
|
|
|
2023-08-02 14:09:11 +00:00
|
|
|
process.on('warning', warning => {
|
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
name: warning.name,
|
|
|
|
message: warning.message,
|
|
|
|
stack: warning.stack,
|
|
|
|
},
|
|
|
|
'Warning details'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-02-14 10:50:52 +00:00
|
|
|
app.use(RequestLogger.middleware)
|
2020-01-14 12:02:39 +00:00
|
|
|
|
2020-01-03 10:06:19 +00:00
|
|
|
if (settings.sentry && settings.sentry.dsn) {
|
2019-12-16 10:52:42 +00:00
|
|
|
logger.initializeErrorReporting(settings.sentry.dsn)
|
2019-12-16 10:52:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 10:36:51 +00:00
|
|
|
Metrics.open_sockets.monitor(true)
|
2020-01-03 10:06:19 +00:00
|
|
|
Metrics.memory.monitor(logger)
|
|
|
|
if (Metrics.event_loop) {
|
2019-12-16 10:52:42 +00:00
|
|
|
Metrics.event_loop.monitor(logger)
|
2019-12-16 10:52:40 +00:00
|
|
|
}
|
2023-05-30 13:25:50 +00:00
|
|
|
Metrics.leaked_sockets.monitor(logger)
|
2019-12-16 10:52:40 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
app.use(function (req, res, next) {
|
2019-12-16 10:52:42 +00:00
|
|
|
Metrics.inc('http-request')
|
2020-01-03 10:06:19 +00:00
|
|
|
next()
|
2019-12-16 10:52:42 +00:00
|
|
|
})
|
2019-12-16 10:52:40 +00:00
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
|
|
|
|
app.head(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
app.put(
|
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
bodyParser.json(),
|
|
|
|
fileController.copyFile
|
|
|
|
)
|
2020-01-03 10:06:19 +00:00
|
|
|
app.delete(
|
2019-12-16 10:52:42 +00:00
|
|
|
'/project/:project_id/file/:file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.userFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.deleteFile
|
|
|
|
)
|
2020-03-14 14:56:29 +00:00
|
|
|
app.delete(
|
|
|
|
'/project/:project_id',
|
|
|
|
keyBuilder.userProjectKeyMiddleware,
|
|
|
|
fileController.deleteProject
|
|
|
|
)
|
2019-12-16 10:52:42 +00:00
|
|
|
|
|
|
|
app.head(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/template/:template_id/v/:version/:format/:sub_type',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/template/:template_id/v/:version/:format',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.templateFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
|
|
|
|
app.head(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
app.put(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
bodyParser.json(),
|
|
|
|
fileController.copyFile
|
|
|
|
)
|
2020-01-03 10:06:19 +00:00
|
|
|
app.delete(
|
2019-12-16 10:52:42 +00:00
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.deleteFile
|
|
|
|
)
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/size',
|
2019-12-19 15:36:48 +00:00
|
|
|
keyBuilder.publicProjectKeyMiddleware,
|
2019-12-16 10:52:42 +00:00
|
|
|
fileController.directorySize
|
|
|
|
)
|
|
|
|
|
2020-01-07 15:05:51 +00:00
|
|
|
app.get(
|
|
|
|
'/bucket/:bucket/key/*',
|
|
|
|
keyBuilder.bucketFileKeyMiddleware,
|
|
|
|
fileController.getFile
|
|
|
|
)
|
2019-12-16 10:52:42 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
app.get('/status', function (req, res) {
|
2020-01-06 15:43:24 +00:00
|
|
|
res.send('filestore sharelatex up')
|
2019-12-16 10:52:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/health_check', healthCheckController.check)
|
|
|
|
|
2020-02-14 10:50:52 +00:00
|
|
|
app.use(RequestLogger.errorHandler)
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
const port = settings.internal.filestore.port || 3009
|
2021-08-27 06:19:14 +00:00
|
|
|
const host = settings.internal.filestore.host || '0.0.0.0'
|
2019-12-16 10:52:42 +00:00
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2021-07-13 11:04:46 +00:00
|
|
|
app.listen(port, host, error => {
|
2019-12-16 12:14:06 +00:00
|
|
|
if (error) {
|
|
|
|
logger.error('Error starting Filestore', error)
|
|
|
|
throw error
|
|
|
|
}
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(`Filestore starting up, listening on ${host}:${port}`)
|
2019-12-16 12:14:06 +00:00
|
|
|
})
|
2019-12-16 10:52:42 +00:00
|
|
|
}
|
2019-12-16 10:52:40 +00:00
|
|
|
|
2020-02-27 10:33:48 +00:00
|
|
|
process
|
|
|
|
.on('unhandledRejection', (reason, p) => {
|
|
|
|
logger.err(reason, 'Unhandled Rejection at Promise', p)
|
|
|
|
})
|
2021-07-13 11:04:46 +00:00
|
|
|
.on('uncaughtException', err => {
|
2020-02-27 10:33:48 +00:00
|
|
|
logger.err(err, 'Uncaught Exception thrown')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
module.exports = app
|