]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.ts
b49d10344bb5a38b83739afe30fe7b350a85322d
[github/Chocobozzz/PeerTube.git] / server.ts
1 import { resolve } from 'path'
2
3 const tsConfig = require('./tsconfig.json')
4 const tsConfigPaths = require('tsconfig-paths')
5
6 // Thanks: https://github.com/dividab/tsconfig-paths/issues/75#issuecomment-458936883
7 tsConfigPaths.register({
8 baseUrl: resolve(tsConfig.compilerOptions.baseUrl || '', tsConfig.compilerOptions.outDir || ''),
9 paths: tsConfig.compilerOptions.paths
10 })
11
12 // FIXME: https://github.com/nodejs/node/pull/16853
13 require('tls').DEFAULT_ECDH_CURVE = 'auto'
14
15 import { isTestInstance } from './server/helpers/core-utils'
16 if (isTestInstance()) {
17 require('source-map-support').install()
18 }
19
20 // ----------- Node modules -----------
21 import * as bodyParser from 'body-parser'
22 import * as express from 'express'
23 import * as morgan from 'morgan'
24 import * as cors from 'cors'
25 import * as cookieParser from 'cookie-parser'
26 import * as helmet from 'helmet'
27 import * as useragent from 'useragent'
28 import * as anonymize from 'ip-anonymize'
29 import * as cli from 'commander'
30
31 process.title = 'peertube'
32
33 // Create our main app
34 const app = express()
35
36 // ----------- Core checker -----------
37 import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './server/initializers/checker-before-init'
38
39 // Do not use barrels because we don't want to load all modules here (we need to initialize database first)
40 import { CONFIG } from './server/initializers/config'
41 import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants'
42 import { logger } from './server/helpers/logger'
43
44 const missed = checkMissedConfig()
45 if (missed.length !== 0) {
46 logger.error('Your configuration files miss keys: ' + missed)
47 process.exit(-1)
48 }
49
50 checkFFmpeg(CONFIG)
51 .catch(err => {
52 logger.error('Error in ffmpeg check.', { err })
53 process.exit(-1)
54 })
55
56 checkNodeVersion()
57
58 import { checkConfig, checkActivityPubUrls } from './server/initializers/checker-after-init'
59
60 const errorMessage = checkConfig()
61 if (errorMessage !== null) {
62 throw new Error(errorMessage)
63 }
64
65 // Trust our proxy (IP forwarding...)
66 app.set('trust proxy', CONFIG.TRUST_PROXY)
67
68 // Security middleware
69 import { baseCSP } from './server/middlewares/csp'
70
71 if (CONFIG.CSP.ENABLED) {
72 app.use(baseCSP)
73 app.use(helmet({
74 frameguard: {
75 action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
76 },
77 hsts: false
78 }))
79 }
80
81 // ----------- Database -----------
82
83 // Initialize database and models
84 import { initDatabaseModels } from './server/initializers/database'
85 import { migrate } from './server/initializers/migrator'
86 migrate()
87 .then(() => initDatabaseModels(false))
88 .then(() => startApplication())
89 .catch(err => {
90 logger.error('Cannot start application.', { err })
91 process.exit(-1)
92 })
93
94 // ----------- Initialize -----------
95 loadLanguages()
96
97 // ----------- PeerTube modules -----------
98 import { installApplication } from './server/initializers'
99 import { Emailer } from './server/lib/emailer'
100 import { JobQueue } from './server/lib/job-queue'
101 import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache'
102 import {
103 activityPubRouter,
104 apiRouter,
105 clientsRouter,
106 feedsRouter,
107 staticRouter,
108 lazyStaticRouter,
109 servicesRouter,
110 pluginsRouter,
111 webfingerRouter,
112 trackerRouter,
113 createWebsocketTrackerServer, botsRouter
114 } from './server/controllers'
115 import { advertiseDoNotTrack } from './server/middlewares/dnt'
116 import { Redis } from './server/lib/redis'
117 import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler'
118 import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler'
119 import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
120 import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
121 import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
122 import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler'
123 import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler'
124 import { AutoFollowIndexInstances } from './server/lib/schedulers/auto-follow-index-instances'
125 import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto'
126 import { PeerTubeSocket } from './server/lib/peertube-socket'
127 import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls'
128 import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler'
129 import { Hooks } from './server/lib/plugins/hooks'
130 import { PluginManager } from './server/lib/plugins/plugin-manager'
131
132 // ----------- Command line -----------
133
134 cli
135 .option('--no-client', 'Start PeerTube without client interface')
136 .option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
137 .parse(process.argv)
138
139 // ----------- App -----------
140
141 // Enable CORS for develop
142 if (isTestInstance()) {
143 app.use(cors({
144 origin: '*',
145 exposedHeaders: 'Retry-After',
146 credentials: true
147 }))
148 }
149
150 // For the logger
151 morgan.token('remote-addr', req => {
152 if (req.get('DNT') === '1') {
153 return anonymize(req.ip, 16, 16)
154 }
155
156 return req.ip
157 })
158 morgan.token('user-agent', req => {
159 if (req.get('DNT') === '1') {
160 return useragent.parse(req.get('user-agent')).family
161 }
162
163 return req.get('user-agent')
164 })
165 app.use(morgan('combined', {
166 stream: { write: logger.info.bind(logger) }
167 }))
168
169 // For body requests
170 app.use(bodyParser.urlencoded({ extended: false }))
171 app.use(bodyParser.json({
172 type: [ 'application/json', 'application/*+json' ],
173 limit: '500kb',
174 verify: (req: express.Request, _, buf: Buffer) => {
175 const valid = isHTTPSignatureDigestValid(buf, req)
176 if (valid !== true) throw new Error('Invalid digest')
177 }
178 }))
179
180 // Cookies
181 app.use(cookieParser())
182
183 // W3C DNT Tracking Status
184 app.use(advertiseDoNotTrack)
185
186 // ----------- Views, routes and static files -----------
187
188 // API
189 const apiRoute = '/api/' + API_VERSION
190 app.use(apiRoute, apiRouter)
191
192 // Services (oembed...)
193 app.use('/services', servicesRouter)
194
195 // Plugins & themes
196 app.use('/', pluginsRouter)
197
198 app.use('/', activityPubRouter)
199 app.use('/', feedsRouter)
200 app.use('/', webfingerRouter)
201 app.use('/', trackerRouter)
202 app.use('/', botsRouter)
203
204 // Static files
205 app.use('/', staticRouter)
206 app.use('/', lazyStaticRouter)
207
208 // Client files, last valid routes!
209 if (cli.client) app.use('/', clientsRouter)
210
211 // ----------- Errors -----------
212
213 // Catch 404 and forward to error handler
214 app.use(function (req, res, next) {
215 const err = new Error('Not Found')
216 err['status'] = 404
217 next(err)
218 })
219
220 app.use(function (err, req, res, next) {
221 let error = 'Unknown error.'
222 if (err) {
223 error = err.stack || err.message || err
224 }
225
226 // Sequelize error
227 const sql = err.parent ? err.parent.sql : undefined
228
229 logger.error('Error in controller.', { err: error, sql })
230 return res.status(err.status || 500).end()
231 })
232
233 const server = createWebsocketTrackerServer(app)
234
235 // ----------- Run -----------
236
237 async function startApplication () {
238 const port = CONFIG.LISTEN.PORT
239 const hostname = CONFIG.LISTEN.HOSTNAME
240
241 await installApplication()
242
243 // Check activity pub urls are valid
244 checkActivityPubUrls()
245 .catch(err => {
246 logger.error('Error in ActivityPub URLs checker.', { err })
247 process.exit(-1)
248 })
249
250 // Email initialization
251 Emailer.Instance.init()
252
253 await Promise.all([
254 Emailer.Instance.checkConnectionOrDie(),
255 JobQueue.Instance.init()
256 ])
257
258 // Caches initializations
259 VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
260 VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
261
262 // Enable Schedulers
263 ActorFollowScheduler.Instance.enable()
264 RemoveOldJobsScheduler.Instance.enable()
265 UpdateVideosScheduler.Instance.enable()
266 YoutubeDlUpdateScheduler.Instance.enable()
267 VideosRedundancyScheduler.Instance.enable()
268 RemoveOldHistoryScheduler.Instance.enable()
269 RemoveOldViewsScheduler.Instance.enable()
270 PluginsCheckScheduler.Instance.enable()
271 AutoFollowIndexInstances.Instance.enable()
272
273 // Redis initialization
274 Redis.Instance.init()
275
276 PeerTubeSocket.Instance.init(server)
277
278 updateStreamingPlaylistsInfohashesIfNeeded()
279 .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
280
281 if (cli.plugins) await PluginManager.Instance.registerPluginsAndThemes()
282
283 // Make server listening
284 server.listen(port, hostname, () => {
285 logger.info('Server listening on %s:%d', hostname, port)
286 logger.info('Web server: %s', WEBSERVER.URL)
287
288 Hooks.runAction('action:application.listening')
289 })
290
291 process.on('exit', () => {
292 JobQueue.Instance.terminate()
293 })
294
295 process.on('SIGINT', () => process.exit(0))
296 }