2023-10-18 04:32:14 -04:00
|
|
|
// Metrics must be initialized before importing anything else
|
|
|
|
require('@overleaf/metrics/initialize')
|
|
|
|
|
2020-10-30 04:10:50 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-04-27 10:17:39 -04:00
|
|
|
const PlansLocator = require('./app/src/Features/Subscription/PlansLocator')
|
2023-02-08 10:19:18 -05:00
|
|
|
const SiteAdminHandler = require('./app/src/infrastructure/SiteAdminHandler')
|
2020-12-16 05:37:00 -05:00
|
|
|
logger.initialize(process.env.METRICS_APP_NAME || 'web')
|
2022-01-10 05:23:05 -05:00
|
|
|
logger.logger.serializers.user =
|
|
|
|
require('./app/src/infrastructure/LoggerSerializers').user
|
|
|
|
logger.logger.serializers.docs =
|
|
|
|
require('./app/src/infrastructure/LoggerSerializers').docs
|
|
|
|
logger.logger.serializers.files =
|
|
|
|
require('./app/src/infrastructure/LoggerSerializers').files
|
|
|
|
logger.logger.serializers.project =
|
|
|
|
require('./app/src/infrastructure/LoggerSerializers').project
|
2023-10-18 04:32:14 -04:00
|
|
|
if (Settings.sentry?.dsn != null) {
|
2019-05-29 05:21:06 -04:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
|
|
|
}
|
|
|
|
|
2021-07-12 06:40:22 -04:00
|
|
|
const http = require('http')
|
|
|
|
const https = require('https')
|
|
|
|
http.globalAgent.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
|
|
|
|
https.globalAgent.maxSockets = Settings.limits.httpsGlobalAgentMaxSockets
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
metrics.memory.monitor(logger)
|
2023-05-30 09:25:50 -04:00
|
|
|
metrics.leaked_sockets.monitor(logger)
|
2023-05-22 06:36:51 -04:00
|
|
|
metrics.open_sockets.monitor()
|
2021-03-31 05:24:39 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const Server = require('./app/src/infrastructure/Server')
|
2021-08-17 09:08:54 -04:00
|
|
|
const QueueWorkers = require('./app/src/infrastructure/QueueWorkers')
|
2020-10-01 04:30:26 -04:00
|
|
|
const mongodb = require('./app/src/infrastructure/mongodb')
|
2021-01-18 10:29:24 -05:00
|
|
|
const mongoose = require('./app/src/infrastructure/Mongoose')
|
2022-08-04 04:16:38 -04:00
|
|
|
const {
|
|
|
|
triggerGracefulShutdown,
|
|
|
|
} = require('./app/src/infrastructure/GracefulShutdown')
|
2023-08-02 09:01:29 -04:00
|
|
|
const FileWriter = require('./app/src/infrastructure/FileWriter')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
if (Settings.catchErrors) {
|
|
|
|
process.removeAllListeners('uncaughtException')
|
|
|
|
process.on('uncaughtException', error =>
|
|
|
|
logger.error({ err: error }, 'uncaughtException')
|
|
|
|
)
|
|
|
|
}
|
2023-08-02 09:01:29 -04:00
|
|
|
|
|
|
|
// Create ./data/dumpFolder if needed
|
|
|
|
FileWriter.ensureDumpFolderExists()
|
|
|
|
|
2019-07-15 09:09:04 -04:00
|
|
|
const port = Settings.port || Settings.internal.web.port || 3000
|
2019-05-29 05:21:06 -04:00
|
|
|
const host = Settings.internal.web.host || 'localhost'
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2019-12-18 04:25:51 -05:00
|
|
|
|
|
|
|
// We want to make sure that we provided a password through the environment.
|
2020-12-16 05:37:00 -05:00
|
|
|
if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
|
2019-12-18 04:25:51 -05:00
|
|
|
throw new Error('No API user and password provided')
|
|
|
|
}
|
2021-04-27 10:17:39 -04:00
|
|
|
|
|
|
|
PlansLocator.ensurePlansAreSetupCorrectly()
|
|
|
|
|
2021-01-18 10:29:24 -05:00
|
|
|
Promise.all([mongodb.waitForDb(), mongoose.connectionPromise])
|
2020-10-01 04:30:26 -04:00
|
|
|
.then(() => {
|
2021-04-14 09:17:21 -04:00
|
|
|
Server.server.listen(port, host, function () {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(`web starting up, listening on ${host}:${port}`)
|
|
|
|
logger.debug(
|
|
|
|
`${require('http').globalAgent.maxSockets} sockets enabled`
|
|
|
|
)
|
2020-10-01 04:30:26 -04:00
|
|
|
// wait until the process is ready before monitoring the event loop
|
|
|
|
metrics.event_loop.monitor(logger)
|
|
|
|
})
|
2023-02-23 08:14:25 -05:00
|
|
|
if (process.env.QUEUE_PROCESSING_ENABLED === 'true') {
|
|
|
|
QueueWorkers.start()
|
|
|
|
}
|
2020-10-01 04:30:26 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2023-02-08 10:19:18 -05:00
|
|
|
// monitor site maintenance file
|
|
|
|
SiteAdminHandler.initialise()
|
|
|
|
|
2020-01-13 08:13:18 -05:00
|
|
|
// handle SIGTERM for graceful shutdown in kubernetes
|
2021-04-14 09:17:21 -04:00
|
|
|
process.on('SIGTERM', function (signal) {
|
2022-08-04 04:16:38 -04:00
|
|
|
triggerGracefulShutdown(Server.server, signal)
|
2020-01-13 08:13:18 -05:00
|
|
|
})
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
module.exports = Server.server
|