]>
Commit | Line | Data |
---|---|---|
a030a9b2 | 1 | // ----------- Node modules ----------- |
630d0a1b C |
2 | import { registerOpentelemetryTracing } from './server/lib/opentelemetry/tracing' |
3 | registerOpentelemetryTracing() | |
4 | ||
41fb13c3 C |
5 | import express from 'express' |
6 | import morgan, { token } from 'morgan' | |
7 | import cors from 'cors' | |
8 | import cookieParser from 'cookie-parser' | |
9 | import { frameguard } from 'helmet' | |
10 | import { parse } from 'useragent' | |
11 | import anonymize from 'ip-anonymize' | |
8cc61201 | 12 | import { program as cli } from 'commander' |
a030a9b2 | 13 | |
9f540774 C |
14 | process.title = 'peertube' |
15 | ||
a030a9b2 | 16 | // Create our main app |
9270bd3a | 17 | const app = express().disable('x-powered-by') |
a030a9b2 | 18 | |
3482688c | 19 | // ----------- Core checker ----------- |
51c35447 | 20 | import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './server/initializers/checker-before-init' |
69b0a27c | 21 | |
d5b7d911 | 22 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) |
74dc3bca | 23 | import { CONFIG } from './server/initializers/config' |
c1340a6a C |
24 | import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants' |
25 | import { logger } from './server/helpers/logger' | |
d5b7d911 | 26 | |
65fcc311 | 27 | const missed = checkMissedConfig() |
b65c27aa | 28 | if (missed.length !== 0) { |
d5b7d911 C |
29 | logger.error('Your configuration files miss keys: ' + missed) |
30 | process.exit(-1) | |
b65c27aa | 31 | } |
3482688c | 32 | |
3482688c | 33 | checkFFmpeg(CONFIG) |
d5b7d911 C |
34 | .catch(err => { |
35 | logger.error('Error in ffmpeg check.', { err }) | |
36 | process.exit(-1) | |
37 | }) | |
b65c27aa | 38 | |
5a637488 C |
39 | try { |
40 | checkNodeVersion() | |
41 | } catch (err) { | |
42 | logger.error('Error in NodeJS check.', { err }) | |
43 | process.exit(-1) | |
44 | } | |
51c35447 | 45 | |
ae71acca | 46 | import { checkConfig, checkActivityPubUrls, checkFFmpegVersion } from './server/initializers/checker-after-init' |
e5565833 | 47 | |
a3e5f804 C |
48 | try { |
49 | checkConfig() | |
50 | } catch (err) { | |
51 | logger.error('Config error.', { err }) | |
52 | process.exit(-1) | |
53 | } | |
69b0a27c | 54 | |
490b595a C |
55 | // Trust our proxy (IP forwarding...) |
56 | app.set('trust proxy', CONFIG.TRUST_PROXY) | |
57 | ||
630d0a1b | 58 | app.use((_req, res, next) => { |
47653481 | 59 | // OpenTelemetry |
630d0a1b C |
60 | res.locals.requestStart = Date.now() |
61 | ||
47653481 C |
62 | if (CONFIG.SECURITY.POWERED_BY_HEADER.ENABLED === true) { |
63 | res.setHeader('x-powered-by', 'PeerTube') | |
64 | } | |
65 | ||
630d0a1b C |
66 | return next() |
67 | }) | |
68 | ||
57c36b27 | 69 | // Security middleware |
418d092a | 70 | import { baseCSP } from './server/middlewares/csp' |
5e755fff | 71 | |
539d3f4f C |
72 | if (CONFIG.CSP.ENABLED) { |
73 | app.use(baseCSP) | |
8155db66 C |
74 | } |
75 | ||
76 | if (CONFIG.SECURITY.FRAMEGUARD.ENABLED) { | |
41fb13c3 | 77 | app.use(frameguard({ |
8155db66 | 78 | action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts |
539d3f4f C |
79 | })) |
80 | } | |
d00e2393 | 81 | |
3482688c | 82 | // ----------- Database ----------- |
91fea9fc | 83 | |
3482688c | 84 | // Initialize database and models |
74055dc8 C |
85 | import { initDatabaseModels, checkDatabaseConnectionOrDie } from './server/initializers/database' |
86 | checkDatabaseConnectionOrDie() | |
87 | ||
91fea9fc C |
88 | import { migrate } from './server/initializers/migrator' |
89 | migrate() | |
90 | .then(() => initDatabaseModels(false)) | |
3d3441d6 C |
91 | .then(() => startApplication()) |
92 | .catch(err => { | |
93 | logger.error('Cannot start application.', { err }) | |
94 | process.exit(-1) | |
95 | }) | |
3482688c | 96 | |
74dc3bca C |
97 | // ----------- Initialize ----------- |
98 | loadLanguages() | |
99 | ||
00057e85 | 100 | // ----------- PeerTube modules ----------- |
80fdaf06 | 101 | import { installApplication } from './server/initializers/installer' |
ecb4e35f | 102 | import { Emailer } from './server/lib/emailer' |
94a5ff8a | 103 | import { JobQueue } from './server/lib/job-queue' |
d74d29ad | 104 | import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache' |
244e76a5 RK |
105 | import { |
106 | activityPubRouter, | |
107 | apiRouter, | |
ea8107bf | 108 | miscRouter, |
244e76a5 RK |
109 | clientsRouter, |
110 | feedsRouter, | |
111 | staticRouter, | |
ea8107bf | 112 | wellKnownRouter, |
557b13ae | 113 | lazyStaticRouter, |
244e76a5 | 114 | servicesRouter, |
9ab330b9 | 115 | objectStorageProxyRouter, |
345da516 | 116 | pluginsRouter, |
9b67da3d | 117 | trackerRouter, |
c6c0fa6c | 118 | createWebsocketTrackerServer, |
90a8bd30 C |
119 | botsRouter, |
120 | downloadRouter | |
244e76a5 | 121 | } from './server/controllers' |
aad0ec24 | 122 | import { advertiseDoNotTrack } from './server/middlewares/dnt' |
e030bfb5 | 123 | import { apiFailMiddleware } from './server/middlewares/error' |
ecb4e35f | 124 | import { Redis } from './server/lib/redis' |
2f5c6b2f | 125 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' |
cda03765 | 126 | import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' |
94a5ff8a | 127 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' |
2baea0c7 | 128 | import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' |
ce32426b | 129 | import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' |
c48e82b5 | 130 | import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler' |
8f0bc73d | 131 | import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler' |
6f1b4fa4 | 132 | import { AutoFollowIndexInstances } from './server/lib/schedulers/auto-follow-index-instances' |
f6d6e7f8 | 133 | import { RemoveDanglingResumableUploadsScheduler } from './server/lib/schedulers/remove-dangling-resumable-uploads-scheduler' |
51353d9a | 134 | import { VideoViewsBufferScheduler } from './server/lib/schedulers/video-views-buffer-scheduler' |
b2111066 | 135 | import { GeoIPUpdateScheduler } from './server/lib/schedulers/geo-ip-update-scheduler' |
0c9668f7 | 136 | import { RunnerJobWatchDogScheduler } from './server/lib/schedulers/runner-job-watch-dog-scheduler' |
df66d815 | 137 | import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto' |
cef534ed | 138 | import { PeerTubeSocket } from './server/lib/peertube-socket' |
ae9bbed4 | 139 | import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls' |
e0ce715a | 140 | import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler' |
32a18cbf | 141 | import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler' |
89cd1275 | 142 | import { Hooks } from './server/lib/plugins/hooks' |
464687bb | 143 | import { PluginManager } from './server/lib/plugins/plugin-manager' |
8ebf2a5d | 144 | import { LiveManager } from './server/lib/live' |
c0e8b12e | 145 | import { HttpStatusCode } from './shared/models/http/http-error-codes' |
90a8bd30 | 146 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' |
2539932e | 147 | import { ServerConfigManager } from '@server/lib/server-config-manager' |
b2111066 | 148 | import { VideoViewsManager } from '@server/lib/views/video-views-manager' |
9452d4fd | 149 | import { isTestOrDevInstance } from './server/helpers/core-utils' |
630d0a1b | 150 | import { OpenTelemetryMetrics } from '@server/lib/opentelemetry/metrics' |
c795e196 | 151 | import { ApplicationModel } from '@server/models/application/application' |
2a491182 | 152 | import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler' |
a030a9b2 | 153 | |
a030a9b2 C |
154 | // ----------- Command line ----------- |
155 | ||
b83b8dd5 RK |
156 | cli |
157 | .option('--no-client', 'Start PeerTube without client interface') | |
66e001c8 | 158 | .option('--no-plugins', 'Start PeerTube without plugins/themes enabled') |
a9cd881b | 159 | .option('--benchmark-startup', 'Automatically stop server when initialized') |
b83b8dd5 RK |
160 | .parse(process.argv) |
161 | ||
a030a9b2 C |
162 | // ----------- App ----------- |
163 | ||
12daa837 | 164 | // Enable CORS for develop |
9452d4fd | 165 | if (isTestOrDevInstance()) { |
62945f06 C |
166 | app.use(cors({ |
167 | origin: '*', | |
168 | exposedHeaders: 'Retry-After', | |
169 | credentials: true | |
170 | })) | |
12daa837 | 171 | } |
74dc3bca | 172 | |
a030a9b2 | 173 | // For the logger |
41fb13c3 | 174 | token('remote-addr', (req: express.Request) => { |
2f6b5e2d | 175 | if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') { |
74dc3bca C |
176 | return anonymize(req.ip, 16, 16) |
177 | } | |
178 | ||
179 | return req.ip | |
180 | }) | |
41fb13c3 | 181 | token('user-agent', (req: express.Request) => { |
74dc3bca | 182 | if (req.get('DNT') === '1') { |
41fb13c3 | 183 | return parse(req.get('user-agent')).family |
74dc3bca C |
184 | } |
185 | ||
186 | return req.get('user-agent') | |
aad0ec24 | 187 | }) |
e02643f3 | 188 | app.use(morgan('combined', { |
452b3bea | 189 | stream: { |
943dc182 | 190 | write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] }) |
452b3bea | 191 | }, |
78d62f4d | 192 | skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' |
e02643f3 | 193 | })) |
74dc3bca | 194 | |
e030bfb5 C |
195 | // Add .fail() helper to response |
196 | app.use(apiFailMiddleware) | |
1cfbdd30 | 197 | |
a030a9b2 | 198 | // For body requests |
1cfbdd30 RK |
199 | app.use(express.urlencoded({ extended: false })) |
200 | app.use(express.json({ | |
86d13ec2 | 201 | type: [ 'application/json', 'application/*+json' ], |
df66d815 | 202 | limit: '500kb', |
1cfbdd30 | 203 | verify: (req: express.Request, res: express.Response, buf: Buffer) => { |
df66d815 | 204 | const valid = isHTTPSignatureDigestValid(buf, req) |
e030bfb5 | 205 | |
1cfbdd30 RK |
206 | if (valid !== true) { |
207 | res.fail({ | |
208 | status: HttpStatusCode.FORBIDDEN_403, | |
209 | message: 'Invalid digest' | |
210 | }) | |
211 | } | |
df66d815 | 212 | } |
165cdc75 | 213 | })) |
74dc3bca | 214 | |
8afc19a6 C |
215 | // Cookies |
216 | app.use(cookieParser()) | |
74dc3bca | 217 | |
aad0ec24 RK |
218 | // W3C DNT Tracking Status |
219 | app.use(advertiseDoNotTrack) | |
a030a9b2 | 220 | |
630d0a1b C |
221 | // ----------- Open Telemetry ----------- |
222 | ||
223 | OpenTelemetryMetrics.Instance.init(app) | |
224 | ||
a96aed15 C |
225 | // ----------- Views, routes and static files ----------- |
226 | ||
227 | // API | |
228 | const apiRoute = '/api/' + API_VERSION | |
229 | app.use(apiRoute, apiRouter) | |
230 | ||
231 | // Services (oembed...) | |
232 | app.use('/services', servicesRouter) | |
233 | ||
345da516 | 234 | // Plugins & themes |
b5f919ac | 235 | app.use('/', pluginsRouter) |
345da516 | 236 | |
350e31d6 | 237 | app.use('/', activityPubRouter) |
244e76a5 | 238 | app.use('/', feedsRouter) |
9b67da3d | 239 | app.use('/', trackerRouter) |
2feebf3e | 240 | app.use('/', botsRouter) |
350e31d6 | 241 | |
a96aed15 C |
242 | // Static files |
243 | app.use('/', staticRouter) | |
ea8107bf C |
244 | app.use('/', wellKnownRouter) |
245 | app.use('/', miscRouter) | |
90a8bd30 | 246 | app.use('/', downloadRouter) |
557b13ae | 247 | app.use('/', lazyStaticRouter) |
9ab330b9 | 248 | app.use('/', objectStorageProxyRouter) |
a96aed15 | 249 | |
989e526a | 250 | // Client files, last valid routes! |
38f57175 | 251 | const cliOptions = cli.opts<{ client: boolean, plugins: boolean }>() |
ba5a8d89 | 252 | if (cliOptions.client) app.use('/', clientsRouter) |
a96aed15 | 253 | |
a030a9b2 C |
254 | // ----------- Errors ----------- |
255 | ||
1cfbdd30 | 256 | // Catch unmatched routes |
38f57175 | 257 | app.use((_req, res: express.Response) => { |
1cfbdd30 | 258 | res.status(HttpStatusCode.NOT_FOUND_404).end() |
a030a9b2 C |
259 | }) |
260 | ||
1cfbdd30 | 261 | // Catch thrown errors |
38f57175 | 262 | app.use((err, _req, res: express.Response, _next) => { |
1cfbdd30 | 263 | // Format error to be logged |
e3a682a8 C |
264 | let error = 'Unknown error.' |
265 | if (err) { | |
266 | error = err.stack || err.message || err | |
267 | } | |
37a04703 | 268 | |
1cfbdd30 | 269 | // Handling Sequelize error traces |
37a04703 C |
270 | const sql = err?.parent ? err.parent.sql : undefined |
271 | ||
272 | // Help us to debug SequelizeConnectionAcquireTimeoutError errors | |
273 | const activeRequests = err?.name === 'SequelizeConnectionAcquireTimeoutError' && typeof (process as any)._getActiveRequests !== 'function' | |
274 | ? (process as any)._getActiveRequests() | |
275 | : undefined | |
276 | ||
277 | logger.error('Error in controller.', { err: error, sql, activeRequests }) | |
1cfbdd30 | 278 | |
76148b27 RK |
279 | return res.fail({ |
280 | status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, | |
281 | message: err.message, | |
282 | type: err.name | |
283 | }) | |
6f4e2522 | 284 | }) |
a030a9b2 | 285 | |
58477244 | 286 | const { server, trackerServer } = createWebsocketTrackerServer(app) |
9b67da3d | 287 | |
79530164 C |
288 | // ----------- Run ----------- |
289 | ||
3d3441d6 | 290 | async function startApplication () { |
65fcc311 | 291 | const port = CONFIG.LISTEN.PORT |
cff8b272 | 292 | const hostname = CONFIG.LISTEN.HOSTNAME |
91fea9fc | 293 | |
3d3441d6 C |
294 | await installApplication() |
295 | ||
23687332 C |
296 | // Check activity pub urls are valid |
297 | checkActivityPubUrls() | |
298 | .catch(err => { | |
299 | logger.error('Error in ActivityPub URLs checker.', { err }) | |
300 | process.exit(-1) | |
301 | }) | |
302 | ||
ae71acca C |
303 | checkFFmpegVersion() |
304 | .catch(err => logger.error('Cannot check ffmpeg version', { err })) | |
305 | ||
dea0df90 | 306 | Redis.Instance.init() |
3d3441d6 | 307 | Emailer.Instance.init() |
3d3441d6 | 308 | |
0b2f03d3 | 309 | await Promise.all([ |
75594f47 | 310 | Emailer.Instance.checkConnection(), |
2539932e C |
311 | JobQueue.Instance.init(), |
312 | ServerConfigManager.Instance.init() | |
0b2f03d3 | 313 | ]) |
3d3441d6 C |
314 | |
315 | // Caches initializations | |
d74d29ad C |
316 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE) |
317 | VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE) | |
90a8bd30 | 318 | VideosTorrentCache.Instance.init(CONFIG.CACHE.TORRENTS.SIZE, FILES_CACHE.TORRENTS.MAX_AGE) |
3d3441d6 C |
319 | |
320 | // Enable Schedulers | |
2f5c6b2f | 321 | ActorFollowScheduler.Instance.enable() |
3d3441d6 | 322 | RemoveOldJobsScheduler.Instance.enable() |
2baea0c7 | 323 | UpdateVideosScheduler.Instance.enable() |
ce32426b | 324 | YoutubeDlUpdateScheduler.Instance.enable() |
c48e82b5 | 325 | VideosRedundancyScheduler.Instance.enable() |
8f0bc73d | 326 | RemoveOldHistoryScheduler.Instance.enable() |
cda03765 | 327 | RemoveOldViewsScheduler.Instance.enable() |
e0ce715a | 328 | PluginsCheckScheduler.Instance.enable() |
32a18cbf | 329 | PeerTubeVersionCheckScheduler.Instance.enable() |
6f1b4fa4 | 330 | AutoFollowIndexInstances.Instance.enable() |
f6d6e7f8 | 331 | RemoveDanglingResumableUploadsScheduler.Instance.enable() |
2a491182 | 332 | VideoChannelSyncLatestScheduler.Instance.enable() |
51353d9a | 333 | VideoViewsBufferScheduler.Instance.enable() |
b2111066 | 334 | GeoIPUpdateScheduler.Instance.enable() |
0c9668f7 | 335 | RunnerJobWatchDogScheduler.Instance.enable() |
58477244 C |
336 | |
337 | OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer }) | |
3d3441d6 | 338 | |
9d4c60dc C |
339 | PluginManager.Instance.init(server) |
340 | // Before PeerTubeSocket init | |
341 | PluginManager.Instance.registerWebSocketRouter() | |
342 | ||
cef534ed | 343 | PeerTubeSocket.Instance.init(server) |
b2111066 | 344 | VideoViewsManager.Instance.init() |
cef534ed | 345 | |
ae9bbed4 C |
346 | updateStreamingPlaylistsInfohashesIfNeeded() |
347 | .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err })) | |
348 | ||
c6c0fa6c | 349 | LiveManager.Instance.init() |
df1db951 | 350 | if (CONFIG.LIVE.ENABLED) await LiveManager.Instance.run() |
c6c0fa6c | 351 | |
3d3441d6 | 352 | // Make server listening |
e19fdf57 C |
353 | server.listen(port, hostname, async () => { |
354 | if (cliOptions.plugins) { | |
355 | try { | |
c795e196 C |
356 | await PluginManager.Instance.rebuildNativePluginsIfNeeded() |
357 | ||
e19fdf57 C |
358 | await PluginManager.Instance.registerPluginsAndThemes() |
359 | } catch (err) { | |
360 | logger.error('Cannot register plugins and themes.', { err }) | |
361 | } | |
362 | } | |
363 | ||
c795e196 C |
364 | ApplicationModel.updateNodeVersions() |
365 | .catch(err => logger.error('Cannot update node versions.', { err })) | |
366 | ||
4404a7c4 C |
367 | JobQueue.Instance.start() |
368 | .catch(err => { | |
369 | logger.error('Cannot start job queue.', { err }) | |
370 | process.exit(-1) | |
371 | }) | |
372 | ||
c2b82382 | 373 | logger.info('HTTP server listening on %s:%d', hostname, port) |
74dc3bca | 374 | logger.info('Web server: %s', WEBSERVER.URL) |
8d76959e | 375 | |
89cd1275 | 376 | Hooks.runAction('action:application.listening') |
a9cd881b C |
377 | |
378 | if (cliOptions['benchmarkStartup']) process.exit(0) | |
f55e5a7b | 379 | }) |
14f2b3ad C |
380 | |
381 | process.on('exit', () => { | |
382 | JobQueue.Instance.terminate() | |
5a921e7b | 383 | .catch(err => logger.error('Cannot terminate job queue.', { err })) |
14f2b3ad C |
384 | }) |
385 | ||
386 | process.on('SIGINT', () => process.exit(0)) | |
5804c0db | 387 | } |