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