2023-10-18 04:32:14 -04:00
|
|
|
// Metrics must be initialized before importing anything else
|
2024-09-25 12:02:35 -04:00
|
|
|
import '@overleaf/metrics/initialize.js'
|
2023-10-18 04:32:14 -04:00
|
|
|
|
2024-09-25 12:02:35 -04:00
|
|
|
import Modules from './app/src/infrastructure/Modules.js'
|
|
|
|
import metrics from '@overleaf/metrics'
|
|
|
|
import Settings from '@overleaf/settings'
|
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import PlansLocator from './app/src/Features/Subscription/PlansLocator.js'
|
2024-10-10 06:30:28 -04:00
|
|
|
import SiteAdminHandler from './app/src/infrastructure/SiteAdminHandler.js'
|
2024-09-25 12:02:35 -04:00
|
|
|
import http from 'node:http'
|
|
|
|
import https from 'node:https'
|
2024-10-10 06:30:28 -04:00
|
|
|
import * as Serializers from './app/src/infrastructure/LoggerSerializers.js'
|
2024-09-25 12:16:48 -04:00
|
|
|
import Server from './app/src/infrastructure/Server.mjs'
|
2024-09-25 12:02:35 -04:00
|
|
|
import QueueWorkers from './app/src/infrastructure/QueueWorkers.js'
|
|
|
|
import mongodb from './app/src/infrastructure/mongodb.js'
|
|
|
|
import mongoose from './app/src/infrastructure/Mongoose.js'
|
|
|
|
import { triggerGracefulShutdown } from './app/src/infrastructure/GracefulShutdown.js'
|
|
|
|
import FileWriter from './app/src/infrastructure/FileWriter.js'
|
2024-11-04 04:09:11 -05:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2024-08-13 11:01:15 -04:00
|
|
|
|
2020-12-16 05:37:00 -05:00
|
|
|
logger.initialize(process.env.METRICS_APP_NAME || 'web')
|
2024-09-25 12:02:35 -04:00
|
|
|
logger.logger.serializers.user = Serializers.user
|
|
|
|
logger.logger.serializers.docs = Serializers.docs
|
|
|
|
logger.logger.serializers.files = Serializers.files
|
|
|
|
logger.logger.serializers.project = Serializers.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
|
|
|
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
|
|
|
if (Settings.catchErrors) {
|
|
|
|
process.removeAllListeners('uncaughtException')
|
2024-10-17 07:47:15 -04:00
|
|
|
process.removeAllListeners('unhandledRejection')
|
|
|
|
process
|
|
|
|
.on('uncaughtException', error =>
|
|
|
|
logger.error({ err: error }, 'uncaughtException')
|
|
|
|
)
|
|
|
|
.on('unhandledRejection', (reason, p) => {
|
|
|
|
logger.error({ err: reason }, 'unhandledRejection at Promise', p)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
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
|
2024-04-25 08:56:00 -04:00
|
|
|
const host = Settings.internal.web.host || '127.0.0.1'
|
2024-09-25 12:02:35 -04:00
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// 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()
|
|
|
|
|
2024-10-31 08:16:54 -04:00
|
|
|
Promise.all([mongodb.connectionPromise, mongoose.connectionPromise])
|
2024-08-13 11:01:15 -04:00
|
|
|
.then(async () => {
|
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}`)
|
2024-09-25 12:02:35 -04:00
|
|
|
logger.debug(`${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)
|
|
|
|
})
|
2024-08-13 11:01:15 -04:00
|
|
|
QueueWorkers.start()
|
|
|
|
await Modules.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
|
|
|
}
|
|
|
|
|
2024-10-01 08:32:35 -04:00
|
|
|
// initialise site admin tasks
|
2024-10-31 08:16:54 -04:00
|
|
|
Promise.all([mongodb.connectionPromise, mongoose.connectionPromise])
|
2024-10-01 08:32:35 -04:00
|
|
|
.then(() => SiteAdminHandler.initialise())
|
|
|
|
.catch(err => {
|
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2023-02-08 10:19:18 -05:00
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2024-09-25 12:02:35 -04:00
|
|
|
export default Server.server
|