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'
16 process.title = 'peertube'
18 // Create our main app
21 // ----------- Core checker -----------
22 import { checkMissedConfig, checkFFmpeg, checkConfig, checkActivityPubUrls } from './server/initializers/checker'
24 // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
25 import { logger } from './server/helpers/logger'
26 import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants'
28 const missed = checkMissedConfig()
29 if (missed.length !== 0) {
30 logger.error('Your configuration files miss keys: ' + missed)
36 logger.error('Error in ffmpeg check.', { err })
40 const errorMessage = checkConfig()
41 if (errorMessage !== null) {
42 throw new Error(errorMessage)
45 // Trust our proxy (IP forwarding...)
46 app.set('trust proxy', CONFIG.TRUST_PROXY)
48 // ----------- Database -----------
50 // Initialize database and models
51 import { initDatabaseModels } from './server/initializers/database'
52 import { migrate } from './server/initializers/migrator'
54 .then(() => initDatabaseModels(false))
55 .then(() => startApplication())
57 logger.error('Cannot start application.', { err })
61 // ----------- PeerTube modules -----------
62 import { installApplication } from './server/initializers'
63 import { Emailer } from './server/lib/emailer'
64 import { JobQueue } from './server/lib/job-queue'
65 import { VideosPreviewCache } from './server/lib/cache'
76 } from './server/controllers'
77 import { Redis } from './server/lib/redis'
78 import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler'
79 import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
80 import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
82 // ----------- Command line -----------
84 // ----------- App -----------
86 // Enable CORS for develop
87 if (isTestInstance()) {
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',
106 app.use(morgan('combined', {
107 stream: { write: logger.info.bind(logger) }
110 app.use(bodyParser.urlencoded({ extended: false }))
111 app.use(bodyParser.json({
112 type: [ 'application/json', 'application/*+json' ],
116 // ----------- Views, routes and static files -----------
119 const apiRoute = '/api/' + API_VERSION
120 app.use(apiRoute, apiRouter)
122 // Services (oembed...)
123 app.use('/services', servicesRouter)
125 app.use('/', activityPubRouter)
126 app.use('/', feedsRouter)
127 app.use('/', webfingerRouter)
128 app.use('/', trackerRouter)
131 app.use('/', staticRouter)
133 // Client files, last valid routes!
134 app.use('/', clientsRouter)
136 // ----------- Errors -----------
138 // Catch 404 and forward to error handler
139 app.use(function (req, res, next) {
140 const err = new Error('Not Found')
145 app.use(function (err, req, res, next) {
146 let error = 'Unknown error.'
148 error = err.stack || err.message || err
151 logger.error('Error in controller.', { error })
152 return res.status(err.status || 500).end()
155 const server = createWebsocketServer(app)
157 // ----------- Run -----------
159 async function startApplication () {
160 const port = CONFIG.LISTEN.PORT
161 const hostname = CONFIG.LISTEN.HOSTNAME
163 await installApplication()
165 // Check activity pub urls are valid
166 checkActivityPubUrls()
168 logger.error('Error in ActivityPub URLs checker.', { err })
172 // Email initialization
173 Emailer.Instance.init()
174 await Emailer.Instance.checkConnectionOrDie()
176 await JobQueue.Instance.init()
178 // Caches initializations
179 VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
182 BadActorFollowScheduler.Instance.enable()
183 RemoveOldJobsScheduler.Instance.enable()
184 UpdateVideosScheduler.Instance.enable()
186 // Redis initialization
187 Redis.Instance.init()
189 // Make server listening
190 server.listen(port, hostname, () => {
191 logger.info('Server listening on %s:%d', hostname, port)
192 logger.info('Web server: %s', CONFIG.WEBSERVER.URL)