2019-12-16 05:52:42 -05:00
|
|
|
const Metrics = require('metrics-sharelatex')
|
2020-01-03 05:06:19 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
Metrics.initialize('filestore')
|
|
|
|
logger.initialize('filestore')
|
2020-01-03 05:06:19 -05:00
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
const settings = require('settings-sharelatex')
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-12-16 07:14:06 -05:00
|
|
|
app.get('/heapdump', (req, res, next) =>
|
2019-12-16 05:52:42 -05:00
|
|
|
require('heapdump').writeSnapshot(
|
|
|
|
'/tmp/' + Date.now() + '.filestore.heapsnapshot',
|
2019-12-16 07:14:06 -05:00
|
|
|
(err, filename) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.send(filename)
|
|
|
|
}
|
2019-12-16 05:52:42 -05: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
|
|
|
|
const host = '0.0.0.0'
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-01-06 10:43:24 -05: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)
|
|
|
|
})
|
|
|
|
.on('uncaughtException', err => {
|
|
|
|
logger.err(err, 'Uncaught Exception thrown')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
2019-12-16 05:52:42 -05:00
|
|
|
module.exports = app
|