X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server.ts;h=66dcb3c400cb584df8a342f245785219b4960e84;hb=7d3316dc823106ac9cef53fc8e433af21ef49fb7;hp=b14ebf623297a9f3d56154379b737839ec5b5067;hpb=c2777c1dfe688c8fab1ef2fed50e360100fa9198;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server.ts b/server.ts index b14ebf623..66dcb3c40 100644 --- a/server.ts +++ b/server.ts @@ -20,7 +20,7 @@ import * as cli from 'commander' process.title = 'peertube' // Create our main app -const app = express() +const app = express().disable("x-powered-by") // ----------- Core checker ----------- import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './server/initializers/checker-before-init' @@ -70,7 +70,9 @@ if (CONFIG.CSP.ENABLED) { // ----------- Database ----------- // Initialize database and models -import { initDatabaseModels } from './server/initializers/database' +import { initDatabaseModels, checkDatabaseConnectionOrDie } from './server/initializers/database' +checkDatabaseConnectionOrDie() + import { migrate } from './server/initializers/migrator' migrate() .then(() => initDatabaseModels(false)) @@ -84,7 +86,7 @@ migrate() loadLanguages() // ----------- PeerTube modules ----------- -import { installApplication } from './server/initializers' +import { installApplication } from './server/initializers/installer' import { Emailer } from './server/lib/emailer' import { JobQueue } from './server/lib/job-queue' import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache' @@ -96,10 +98,12 @@ import { staticRouter, lazyStaticRouter, servicesRouter, + liveRouter, pluginsRouter, webfingerRouter, trackerRouter, - createWebsocketTrackerServer, botsRouter + createWebsocketTrackerServer, + botsRouter } from './server/controllers' import { advertiseDoNotTrack } from './server/middlewares/dnt' import { Redis } from './server/lib/redis' @@ -117,6 +121,8 @@ import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls' import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler' import { Hooks } from './server/lib/plugins/hooks' import { PluginManager } from './server/lib/plugins/plugin-manager' +import { LiveManager } from './server/lib/live-manager' +import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' // ----------- Command line ----------- @@ -137,14 +143,14 @@ if (isTestInstance()) { } // For the logger -morgan.token('remote-addr', req => { +morgan.token('remote-addr', (req: express.Request) => { if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') { return anonymize(req.ip, 16, 16) } return req.ip }) -morgan.token('user-agent', req => { +morgan.token('user-agent', (req: express.Request) => { if (req.get('DNT') === '1') { return useragent.parse(req.get('user-agent')).family } @@ -152,7 +158,8 @@ morgan.token('user-agent', req => { return req.get('user-agent') }) app.use(morgan('combined', { - stream: { write: logger.info.bind(logger) } + stream: { write: logger.info.bind(logger) }, + skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' })) // For body requests @@ -181,6 +188,9 @@ app.use(apiRoute, apiRouter) // Services (oembed...) app.use('/services', servicesRouter) +// Live streaming +app.use('/live', liveRouter) + // Plugins & themes app.use('/', pluginsRouter) @@ -195,14 +205,15 @@ app.use('/', staticRouter) app.use('/', lazyStaticRouter) // Client files, last valid routes! -if (cli.client) app.use('/', clientsRouter) +const cliOptions = cli.opts() +if (cliOptions.client) app.use('/', clientsRouter) // ----------- Errors ----------- // Catch 404 and forward to error handler app.use(function (req, res, next) { const err = new Error('Not Found') - err['status'] = 404 + err['status'] = HttpStatusCode.NOT_FOUND_404 next(err) }) @@ -216,7 +227,7 @@ app.use(function (err, req, res, next) { const sql = err.parent ? err.parent.sql : undefined logger.error('Error in controller.', { err: error, sql }) - return res.status(err.status || 500).end() + return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() }) const server = createWebsocketTrackerServer(app) @@ -240,7 +251,7 @@ async function startApplication () { Emailer.Instance.init() await Promise.all([ - Emailer.Instance.checkConnectionOrDie(), + Emailer.Instance.checkConnection(), JobQueue.Instance.init() ]) @@ -267,11 +278,14 @@ async function startApplication () { updateStreamingPlaylistsInfohashesIfNeeded() .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err })) - if (cli.plugins) await PluginManager.Instance.registerPluginsAndThemes() + if (cliOptions.plugins) await PluginManager.Instance.registerPluginsAndThemes() + + LiveManager.Instance.init() + if (CONFIG.LIVE.ENABLED) LiveManager.Instance.run() // Make server listening server.listen(port, hostname, () => { - logger.info('Server listening on %s:%d', hostname, port) + logger.info('HTTP server listening on %s:%d', hostname, port) logger.info('Web server: %s', WEBSERVER.URL) Hooks.runAction('action:application.listening')