overleaf/services/filestore/app.js

162 lines
3.8 KiB
JavaScript
Raw Normal View History

const Metrics = require('@overleaf/metrics')
Metrics.initialize(process.env.METRICS_APP_NAME || 'filestore')
2020-01-03 05:06:19 -05:00
2020-07-23 08:35:52 -04:00
const logger = require('logger-sharelatex')
logger.initialize(process.env.METRICS_APP_NAME || 'filestore')
const settings = require('settings-sharelatex')
2020-01-03 05:06:19 -05:00
const express = require('express')
const bodyParser = require('body-parser')
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
const RequestLogger = require('./app/js/RequestLogger')
const app = express()
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) {
logger.initializeErrorReporting(settings.sentry.dsn)
}
Metrics.open_sockets.monitor(logger)
2020-01-03 05:06:19 -05:00
Metrics.memory.monitor(logger)
if (Metrics.event_loop) {
Metrics.event_loop.monitor(logger)
}
2020-08-10 12:01:12 -04:00
app.use(function (req, res, next) {
Metrics.inc('http-request')
2020-01-03 05:06:19 -05:00
next()
})
Metrics.injectMetricsRoute(app)
app.head(
'/project/:project_id/file/:file_id',
keyBuilder.userFileKeyMiddleware,
fileController.getFileHead
)
app.get(
'/project/:project_id/file/:file_id',
keyBuilder.userFileKeyMiddleware,
fileController.getFile
)
app.post(
'/project/:project_id/file/:file_id',
keyBuilder.userFileKeyMiddleware,
fileController.insertFile
)
app.put(
'/project/:project_id/file/:file_id',
keyBuilder.userFileKeyMiddleware,
bodyParser.json(),
fileController.copyFile
)
2020-01-03 05:06:19 -05:00
app.delete(
'/project/:project_id/file/:file_id',
keyBuilder.userFileKeyMiddleware,
fileController.deleteFile
)
app.delete(
'/project/:project_id',
keyBuilder.userProjectKeyMiddleware,
fileController.deleteProject
)
app.head(
'/template/:template_id/v/:version/:format',
keyBuilder.templateFileKeyMiddleware,
fileController.getFileHead
)
app.get(
'/template/:template_id/v/:version/:format',
keyBuilder.templateFileKeyMiddleware,
fileController.getFile
)
app.get(
'/template/:template_id/v/:version/:format/:sub_type',
keyBuilder.templateFileKeyMiddleware,
fileController.getFile
)
app.post(
'/template/:template_id/v/:version/:format',
keyBuilder.templateFileKeyMiddleware,
fileController.insertFile
)
app.head(
'/project/:project_id/public/:public_file_id',
keyBuilder.publicFileKeyMiddleware,
fileController.getFileHead
)
app.get(
'/project/:project_id/public/:public_file_id',
keyBuilder.publicFileKeyMiddleware,
fileController.getFile
)
app.post(
'/project/:project_id/public/:public_file_id',
keyBuilder.publicFileKeyMiddleware,
fileController.insertFile
)
app.put(
'/project/:project_id/public/:public_file_id',
keyBuilder.publicFileKeyMiddleware,
bodyParser.json(),
fileController.copyFile
)
2020-01-03 05:06:19 -05:00
app.delete(
'/project/:project_id/public/:public_file_id',
keyBuilder.publicFileKeyMiddleware,
fileController.deleteFile
)
app.get(
'/project/:project_id/size',
keyBuilder.publicProjectKeyMiddleware,
fileController.directorySize
)
app.get(
'/bucket/:bucket/key/*',
keyBuilder.bucketFileKeyMiddleware,
fileController.getFile
)
2020-08-10 12:01:12 -04:00
app.get('/status', function (req, res) {
res.send('filestore sharelatex up')
})
app.get('/health_check', healthCheckController.check)
app.use(RequestLogger.errorHandler)
const port = settings.internal.filestore.port || 3009
const host = '0.0.0.0'
if (!module.parent) {
// Called directly
2020-08-10 12:01:12 -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
}
logger.info(`Filestore starting up, listening on ${host}:${port}`)
2019-12-16 07:14:06 -05:00
})
}
process
.on('unhandledRejection', (reason, p) => {
logger.err(reason, 'Unhandled Rejection at Promise', p)
})
2020-08-10 12:01:12 -04:00
.on('uncaughtException', (err) => {
logger.err(err, 'Uncaught Exception thrown')
process.exit(1)
})
module.exports = app