1 // FIXME: https://github.com/nodejs/node/pull/16853
2 import { VideosCaptionCache } from './server/lib/cache/videos-caption-cache'
4 require('tls').DEFAULT_ECDH_CURVE = 'auto'
6 import { isTestInstance } from './server/helpers/core-utils'
8 if (isTestInstance()) {
9 require('source-map-support').install()
12 // ----------- Node modules -----------
13 import * as bodyParser from 'body-parser'
14 import * as express from 'express'
15 import * as morgan from 'morgan'
16 import * as cors from 'cors'
17 import * as cookieParser from 'cookie-parser'
18 import * as helmet from 'helmet'
20 process.title = 'peertube'
22 // Create our main app
25 // ----------- Core checker -----------
26 import { checkMissedConfig, checkFFmpeg, checkConfig, checkActivityPubUrls } from './server/initializers/checker'
28 // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
29 import { logger } from './server/helpers/logger'
30 import { API_VERSION, CONFIG, STATIC_PATHS, CACHE, REMOTE_SCHEME } from './server/initializers/constants'
32 const missed = checkMissedConfig()
33 if (missed.length !== 0) {
34 logger.error('Your configuration files miss keys: ' + missed)
40 logger.error('Error in ffmpeg check.', { err })
44 const errorMessage = checkConfig()
45 if (errorMessage !== null) {
46 throw new Error(errorMessage)
49 // Trust our proxy (IP forwarding...)
50 app.set('trust proxy', CONFIG.TRUST_PROXY)
52 // Security middleware
55 action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
59 // ----------- Database -----------
61 // Initialize database and models
62 import { initDatabaseModels } from './server/initializers/database'
63 import { migrate } from './server/initializers/migrator'
65 .then(() => initDatabaseModels(false))
66 .then(() => startApplication())
68 logger.error('Cannot start application.', { err })
72 // ----------- PeerTube modules -----------
73 import { installApplication } from './server/initializers'
74 import { Emailer } from './server/lib/emailer'
75 import { JobQueue } from './server/lib/job-queue'
76 import { VideosPreviewCache } from './server/lib/cache'
87 } from './server/controllers'
88 import { Redis } from './server/lib/redis'
89 import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler'
90 import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
91 import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
93 // ----------- Command line -----------
95 // ----------- App -----------
97 // Enable CORS for develop
98 if (isTestInstance()) {
101 exposedHeaders: 'Retry-After',
107 app.use(morgan('combined', {
108 stream: { write: logger.info.bind(logger) }
111 app.use(bodyParser.urlencoded({ extended: false }))
112 app.use(bodyParser.json({
113 type: [ 'application/json', 'application/*+json' ],
117 app.use(cookieParser())
119 // ----------- Views, routes and static files -----------
122 const apiRoute = '/api/' + API_VERSION
123 app.use(apiRoute, apiRouter)
125 // Services (oembed...)
126 app.use('/services', servicesRouter)
128 app.use('/', activityPubRouter)
129 app.use('/', feedsRouter)
130 app.use('/', webfingerRouter)
131 app.use('/', trackerRouter)
134 app.use('/', staticRouter)
136 // Client files, last valid routes!
137 app.use('/', clientsRouter)
139 // ----------- Errors -----------
141 // Catch 404 and forward to error handler
142 app.use(function (req, res, next) {
143 const err = new Error('Not Found')
148 app.use(function (err, req, res, next) {
149 let error = 'Unknown error.'
151 error = err.stack || err.message || err
154 logger.error('Error in controller.', { error })
155 return res.status(err.status || 500).end()
158 const server = createWebsocketServer(app)
160 // ----------- Run -----------
162 async function startApplication () {
163 const port = CONFIG.LISTEN.PORT
164 const hostname = CONFIG.LISTEN.HOSTNAME
166 await installApplication()
168 // Check activity pub urls are valid
169 checkActivityPubUrls()
171 logger.error('Error in ActivityPub URLs checker.', { err })
175 // Email initialization
176 Emailer.Instance.init()
177 await Emailer.Instance.checkConnectionOrDie()
179 await JobQueue.Instance.init()
181 // Caches initializations
182 VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, CACHE.PREVIEWS.MAX_AGE)
183 VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, CACHE.VIDEO_CAPTIONS.MAX_AGE)
186 BadActorFollowScheduler.Instance.enable()
187 RemoveOldJobsScheduler.Instance.enable()
188 UpdateVideosScheduler.Instance.enable()
190 // Redis initialization
191 Redis.Instance.init()
193 // Make server listening
194 server.listen(port, hostname, () => {
195 logger.info('Server listening on %s:%d', hostname, port)
196 logger.info('Web server: %s', CONFIG.WEBSERVER.URL)