2020-11-25 06:57:23 -05:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-07-22 11:57:41 -04:00
|
|
|
Metrics.initialize(process.env.METRICS_APP_NAME || 'filestore')
|
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')
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
Metrics.open_sockets.monitor(logger)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
app.head(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFileHead
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.getFile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.insertFile
|
|
|
|
)
|
|
|
|
app.put(
|
|
|
|
'/project/:project_id/public/:public_file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
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/public/:public_file_id',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicFileKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.deleteFile
|
|
|
|
)
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/size',
|
2019-12-19 10:36:48 -05:00
|
|
|
keyBuilder.publicProjectKeyMiddleware,
|
2019-12-16 05:52:42 -05:00
|
|
|
fileController.directorySize
|
|
|
|
)
|
|
|
|
|
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) {
|
2020-01-06 10:43:24 -05:00
|
|
|
res.send('filestore sharelatex 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
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2021-07-13 07:04:46 -04:00
|
|
|
app.listen(port, host, error => {
|
2019-12-16 07:14:06 -05:00
|
|
|
if (error) {
|
|
|
|
logger.error('Error starting Filestore', error)
|
|
|
|
throw error
|
|
|
|
}
|
2019-12-16 05:52:42 -05:00
|
|
|
logger.info(`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)
|
|
|
|
})
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
module.exports = app
|