1 // FIXME: https://github.com/nodejs/node/pull/16853
2 require('tls').DEFAULT_ECDH_CURVE = 'auto'
4 import { isTestInstance } from './server/helpers/core-utils'
6 if (isTestInstance()) {
7 require('source-map-support').install()
10 // ----------- Node modules -----------
11 import * as bodyParser from 'body-parser'
12 import * as express from 'express'
13 import * as morgan from 'morgan'
14 import * as cors from 'cors'
15 import * as cookieParser from 'cookie-parser'
17 process.title = 'peertube'
19 // Create our main app
22 // ----------- Core checker -----------
23 import { checkMissedConfig, checkFFmpeg, checkConfig, checkActivityPubUrls } from './server/initializers/checker'
25 // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
26 import { logger } from './server/helpers/logger'
27 import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants'
29 const missed = checkMissedConfig()
30 if (missed.length !== 0) {
31 logger.error('Your configuration files miss keys: ' + missed)
37 logger.error('Error in ffmpeg check.', { err })
41 const errorMessage = checkConfig()
42 if (errorMessage !== null) {
43 throw new Error(errorMessage)
46 // Trust our proxy (IP forwarding...)
47 app.set('trust proxy', CONFIG.TRUST_PROXY)
49 // ----------- Database -----------
51 // Initialize database and models
52 import { initDatabaseModels } from './server/initializers/database'
53 import { migrate } from './server/initializers/migrator'
55 .then(() => initDatabaseModels(false))
56 .then(() => startApplication())
58 logger.error('Cannot start application.', { err })
62 // ----------- PeerTube modules -----------
63 import { installApplication } from './server/initializers'
64 import { Emailer } from './server/lib/emailer'
65 import { JobQueue } from './server/lib/job-queue'
66 import { VideosPreviewCache } from './server/lib/cache'
77 } from './server/controllers'
78 import { Redis } from './server/lib/redis'
79 import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler'
80 import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
81 import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
83 // ----------- Command line -----------
85 // ----------- App -----------
88 app.use((req, res, next) => {
89 // These routes have already cors
91 req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 &&
92 req.path.indexOf(STATIC_PATHS.WEBSEED) === -1
96 exposedHeaders: 'Retry-After',
105 app.use(morgan('combined', {
106 stream: { write: logger.info.bind(logger) }
109 app.use(bodyParser.urlencoded({ extended: false }))
110 app.use(bodyParser.json({
111 type: [ 'application/json', 'application/*+json' ],
115 app.use(cookieParser())
117 // ----------- Views, routes and static files -----------
120 const apiRoute = '/api/' + API_VERSION
121 app.use(apiRoute, apiRouter)
123 // Services (oembed...)
124 app.use('/services', servicesRouter)
126 app.use('/', activityPubRouter)
127 app.use('/', feedsRouter)
128 app.use('/', webfingerRouter)
129 app.use('/', trackerRouter)
132 app.use('/', staticRouter)
134 // Client files, last valid routes!
135 app.use('/', clientsRouter)
137 // ----------- Errors -----------
139 // Catch 404 and forward to error handler
140 app.use(function (req, res, next) {
141 const err = new Error('Not Found')
146 app.use(function (err, req, res, next) {
147 let error = 'Unknown error.'
149 error = err.stack || err.message || err
152 logger.error('Error in controller.', { error })
153 return res.status(err.status || 500).end()
156 const server = createWebsocketServer(app)
158 // ----------- Run -----------
160 async function startApplication () {
161 const port = CONFIG.LISTEN.PORT
162 const hostname = CONFIG.LISTEN.HOSTNAME
164 await installApplication()
166 // Check activity pub urls are valid
167 checkActivityPubUrls()
169 logger.error('Error in ActivityPub URLs checker.', { err })
173 // Email initialization
174 Emailer.Instance.init()
175 await Emailer.Instance.checkConnectionOrDie()
177 await JobQueue.Instance.init()
179 // Caches initializations
180 VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
183 BadActorFollowScheduler.Instance.enable()
184 RemoveOldJobsScheduler.Instance.enable()
185 UpdateVideosScheduler.Instance.enable()
187 // Redis initialization
188 Redis.Instance.init()
190 // Make server listening
191 server.listen(port, hostname, () => {
192 logger.info('Server listening on %s:%d', hostname, port)
193 logger.info('Web server: %s', CONFIG.WEBSERVER.URL)