2019-12-16 10:52:42 +00:00
|
|
|
const Metrics = require('metrics-sharelatex')
|
2020-01-03 10:06:19 +00:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
Metrics.initialize('filestore')
|
|
|
|
logger.initialize('filestore')
|
2020-01-03 10:06:19 +00:00
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
const settings = require('settings-sharelatex')
|
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')
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
const app = express()
|
2019-12-16 10:52:40 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
Metrics.open_sockets.monitor(logger)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
const host = '0.0.0.0'
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-01-06 15:43:24 +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
|
|
|
|
}
|
2019-12-16 10:52:42 +00:00
|
|
|
logger.info(`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)
|
|
|
|
})
|
|
|
|
.on('uncaughtException', err => {
|
|
|
|
logger.err(err, 'Uncaught Exception thrown')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
2019-12-16 10:52:42 +00:00
|
|
|
module.exports = app
|