2022-12-13 12:37:49 +00:00
|
|
|
import http from 'http'
|
|
|
|
import metrics from '@overleaf/metrics'
|
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import express from 'express'
|
2023-01-04 15:07:58 +00:00
|
|
|
import exegesisExpress from 'exegesis-express'
|
|
|
|
import path from 'path'
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import * as messagesController from './Features/Messages/MessageHttpController.js'
|
|
|
|
|
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url))
|
2022-12-13 12:37:49 +00:00
|
|
|
|
2018-12-20 19:13:59 +00:00
|
|
|
metrics.initialize('chat')
|
2019-02-05 18:50:57 +00:00
|
|
|
logger.initialize('chat')
|
2023-05-22 10:36:51 +00:00
|
|
|
metrics.open_sockets.monitor()
|
2022-12-13 12:37:49 +00:00
|
|
|
|
2023-05-30 13:25:50 +00:00
|
|
|
metrics.leaked_sockets.monitor(logger)
|
|
|
|
|
2023-01-04 15:07:58 +00:00
|
|
|
export async function createServer() {
|
|
|
|
const app = express()
|
|
|
|
|
2023-01-05 11:42:34 +00:00
|
|
|
app.use(metrics.http.monitor(logger))
|
|
|
|
metrics.injectMetricsRoute(app)
|
|
|
|
|
2023-01-04 15:07:58 +00:00
|
|
|
// See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
|
|
|
|
const options = {
|
|
|
|
controllers: { messagesController },
|
|
|
|
ignoreServers: true,
|
|
|
|
allowMissingControllers: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// const exegesisMiddleware = await exegesisExpress.middleware(
|
|
|
|
const exegesisMiddleware = await exegesisExpress.middleware(
|
|
|
|
path.resolve(__dirname, '../../chat.yaml'),
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
// If you have any body parsers, this should go before them.
|
|
|
|
app.use(exegesisMiddleware)
|
|
|
|
|
|
|
|
// Return a 404
|
|
|
|
app.use((req, res) => {
|
|
|
|
res.status(404).json({ message: `Not found` })
|
|
|
|
})
|
2014-08-15 09:50:36 +00:00
|
|
|
|
2023-01-04 15:07:58 +00:00
|
|
|
// Handle any unexpected errors
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
res.status(500).json({ message: `Internal error: ${err.message}` })
|
|
|
|
})
|
2014-08-15 09:50:36 +00:00
|
|
|
|
2023-01-04 15:07:58 +00:00
|
|
|
const server = http.createServer(app)
|
|
|
|
return { app, server }
|
|
|
|
}
|