]>
Commit | Line | Data |
---|---|---|
2aaa1a3f | 1 | import { registerTSPaths } from './server/helpers/register-ts-paths' |
2aaa1a3f | 2 | registerTSPaths() |
f023a19c | 3 | |
1840c2f7 | 4 | import { isTestInstance } from './server/helpers/core-utils' |
1840c2f7 | 5 | if (isTestInstance()) { |
e02643f3 C |
6 | require('source-map-support').install() |
7 | } | |
8 | ||
a030a9b2 | 9 | // ----------- Node modules ----------- |
4d4e5cd4 C |
10 | import * as bodyParser from 'body-parser' |
11 | import * as express from 'express' | |
4d4e5cd4 | 12 | import * as morgan from 'morgan' |
1840c2f7 | 13 | import * as cors from 'cors' |
8afc19a6 | 14 | import * as cookieParser from 'cookie-parser' |
d00e2393 | 15 | import * as helmet from 'helmet' |
aad0ec24 | 16 | import * as useragent from 'useragent' |
e5565833 | 17 | import * as anonymize from 'ip-anonymize' |
b83b8dd5 | 18 | import * as cli from 'commander' |
a030a9b2 | 19 | |
9f540774 C |
20 | process.title = 'peertube' |
21 | ||
a030a9b2 | 22 | // Create our main app |
6c8c15f9 | 23 | const app = express().disable("x-powered-by") |
a030a9b2 | 24 | |
3482688c | 25 | // ----------- Core checker ----------- |
51c35447 | 26 | import { checkMissedConfig, checkFFmpeg, checkNodeVersion } from './server/initializers/checker-before-init' |
69b0a27c | 27 | |
d5b7d911 | 28 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) |
74dc3bca | 29 | import { CONFIG } from './server/initializers/config' |
c1340a6a C |
30 | import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants' |
31 | import { logger } from './server/helpers/logger' | |
d5b7d911 | 32 | |
65fcc311 | 33 | const missed = checkMissedConfig() |
b65c27aa | 34 | if (missed.length !== 0) { |
d5b7d911 C |
35 | logger.error('Your configuration files miss keys: ' + missed) |
36 | process.exit(-1) | |
b65c27aa | 37 | } |
3482688c | 38 | |
3482688c | 39 | checkFFmpeg(CONFIG) |
d5b7d911 C |
40 | .catch(err => { |
41 | logger.error('Error in ffmpeg check.', { err }) | |
42 | process.exit(-1) | |
43 | }) | |
b65c27aa | 44 | |
51c35447 C |
45 | checkNodeVersion() |
46 | ||
e5565833 C |
47 | import { checkConfig, checkActivityPubUrls } from './server/initializers/checker-after-init' |
48 | ||
65fcc311 | 49 | const errorMessage = checkConfig() |
b65c27aa C |
50 | if (errorMessage !== null) { |
51 | throw new Error(errorMessage) | |
69b0a27c C |
52 | } |
53 | ||
490b595a C |
54 | // Trust our proxy (IP forwarding...) |
55 | app.set('trust proxy', CONFIG.TRUST_PROXY) | |
56 | ||
57c36b27 | 57 | // Security middleware |
418d092a | 58 | import { baseCSP } from './server/middlewares/csp' |
5e755fff | 59 | |
539d3f4f C |
60 | if (CONFIG.CSP.ENABLED) { |
61 | app.use(baseCSP) | |
62 | app.use(helmet({ | |
63 | frameguard: { | |
64 | action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts | |
65 | }, | |
66 | hsts: false | |
67 | })) | |
68 | } | |
d00e2393 | 69 | |
3482688c | 70 | // ----------- Database ----------- |
91fea9fc | 71 | |
3482688c | 72 | // Initialize database and models |
74055dc8 C |
73 | import { initDatabaseModels, checkDatabaseConnectionOrDie } from './server/initializers/database' |
74 | checkDatabaseConnectionOrDie() | |
75 | ||
91fea9fc C |
76 | import { migrate } from './server/initializers/migrator' |
77 | migrate() | |
78 | .then(() => initDatabaseModels(false)) | |
3d3441d6 C |
79 | .then(() => startApplication()) |
80 | .catch(err => { | |
81 | logger.error('Cannot start application.', { err }) | |
82 | process.exit(-1) | |
83 | }) | |
3482688c | 84 | |
74dc3bca C |
85 | // ----------- Initialize ----------- |
86 | loadLanguages() | |
87 | ||
00057e85 | 88 | // ----------- PeerTube modules ----------- |
80fdaf06 | 89 | import { installApplication } from './server/initializers/installer' |
ecb4e35f | 90 | import { Emailer } from './server/lib/emailer' |
94a5ff8a | 91 | import { JobQueue } from './server/lib/job-queue' |
d74d29ad | 92 | import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache' |
244e76a5 RK |
93 | import { |
94 | activityPubRouter, | |
95 | apiRouter, | |
96 | clientsRouter, | |
97 | feedsRouter, | |
98 | staticRouter, | |
557b13ae | 99 | lazyStaticRouter, |
244e76a5 | 100 | servicesRouter, |
c6c0fa6c | 101 | liveRouter, |
345da516 | 102 | pluginsRouter, |
9b67da3d C |
103 | webfingerRouter, |
104 | trackerRouter, | |
c6c0fa6c C |
105 | createWebsocketTrackerServer, |
106 | botsRouter | |
244e76a5 | 107 | } from './server/controllers' |
aad0ec24 | 108 | import { advertiseDoNotTrack } from './server/middlewares/dnt' |
ecb4e35f | 109 | import { Redis } from './server/lib/redis' |
2f5c6b2f | 110 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' |
cda03765 | 111 | import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' |
94a5ff8a | 112 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' |
2baea0c7 | 113 | import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' |
ce32426b | 114 | import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' |
c48e82b5 | 115 | import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler' |
8f0bc73d | 116 | import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler' |
6f1b4fa4 | 117 | import { AutoFollowIndexInstances } from './server/lib/schedulers/auto-follow-index-instances' |
df66d815 | 118 | import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto' |
cef534ed | 119 | import { PeerTubeSocket } from './server/lib/peertube-socket' |
ae9bbed4 | 120 | import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls' |
e0ce715a | 121 | import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler' |
89cd1275 | 122 | import { Hooks } from './server/lib/plugins/hooks' |
464687bb | 123 | import { PluginManager } from './server/lib/plugins/plugin-manager' |
2d53be02 RK |
124 | import { LiveManager } from './server/lib/live-manager' |
125 | import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' | |
a030a9b2 | 126 | |
a030a9b2 C |
127 | // ----------- Command line ----------- |
128 | ||
b83b8dd5 RK |
129 | cli |
130 | .option('--no-client', 'Start PeerTube without client interface') | |
66e001c8 | 131 | .option('--no-plugins', 'Start PeerTube without plugins/themes enabled') |
b83b8dd5 RK |
132 | .parse(process.argv) |
133 | ||
a030a9b2 C |
134 | // ----------- App ----------- |
135 | ||
12daa837 WL |
136 | // Enable CORS for develop |
137 | if (isTestInstance()) { | |
62945f06 C |
138 | app.use(cors({ |
139 | origin: '*', | |
140 | exposedHeaders: 'Retry-After', | |
141 | credentials: true | |
142 | })) | |
12daa837 | 143 | } |
74dc3bca | 144 | |
a030a9b2 | 145 | // For the logger |
3bc68dfd | 146 | morgan.token('remote-addr', (req: express.Request) => { |
2f6b5e2d | 147 | if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') { |
74dc3bca C |
148 | return anonymize(req.ip, 16, 16) |
149 | } | |
150 | ||
151 | return req.ip | |
152 | }) | |
3bc68dfd | 153 | morgan.token('user-agent', (req: express.Request) => { |
74dc3bca C |
154 | if (req.get('DNT') === '1') { |
155 | return useragent.parse(req.get('user-agent')).family | |
156 | } | |
157 | ||
158 | return req.get('user-agent') | |
aad0ec24 | 159 | }) |
e02643f3 | 160 | app.use(morgan('combined', { |
12c1e38d | 161 | stream: { write: logger.info.bind(logger) }, |
78d62f4d | 162 | skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' |
e02643f3 | 163 | })) |
74dc3bca | 164 | |
a030a9b2 | 165 | // For body requests |
bf9ae5ce | 166 | app.use(bodyParser.urlencoded({ extended: false })) |
165cdc75 | 167 | app.use(bodyParser.json({ |
86d13ec2 | 168 | type: [ 'application/json', 'application/*+json' ], |
df66d815 | 169 | limit: '500kb', |
cef534ed | 170 | verify: (req: express.Request, _, buf: Buffer) => { |
df66d815 C |
171 | const valid = isHTTPSignatureDigestValid(buf, req) |
172 | if (valid !== true) throw new Error('Invalid digest') | |
173 | } | |
165cdc75 | 174 | })) |
74dc3bca | 175 | |
8afc19a6 C |
176 | // Cookies |
177 | app.use(cookieParser()) | |
74dc3bca | 178 | |
aad0ec24 RK |
179 | // W3C DNT Tracking Status |
180 | app.use(advertiseDoNotTrack) | |
a030a9b2 | 181 | |
a96aed15 C |
182 | // ----------- Views, routes and static files ----------- |
183 | ||
184 | // API | |
185 | const apiRoute = '/api/' + API_VERSION | |
186 | app.use(apiRoute, apiRouter) | |
187 | ||
188 | // Services (oembed...) | |
189 | app.use('/services', servicesRouter) | |
190 | ||
c6c0fa6c C |
191 | // Live streaming |
192 | app.use('/live', liveRouter) | |
193 | ||
345da516 | 194 | // Plugins & themes |
b5f919ac | 195 | app.use('/', pluginsRouter) |
345da516 | 196 | |
350e31d6 | 197 | app.use('/', activityPubRouter) |
244e76a5 RK |
198 | app.use('/', feedsRouter) |
199 | app.use('/', webfingerRouter) | |
9b67da3d | 200 | app.use('/', trackerRouter) |
2feebf3e | 201 | app.use('/', botsRouter) |
350e31d6 | 202 | |
a96aed15 C |
203 | // Static files |
204 | app.use('/', staticRouter) | |
557b13ae | 205 | app.use('/', lazyStaticRouter) |
a96aed15 | 206 | |
989e526a | 207 | // Client files, last valid routes! |
b83b8dd5 | 208 | if (cli.client) app.use('/', clientsRouter) |
a96aed15 | 209 | |
a030a9b2 C |
210 | // ----------- Errors ----------- |
211 | ||
212 | // Catch 404 and forward to error handler | |
213 | app.use(function (req, res, next) { | |
13ce1d01 | 214 | const err = new Error('Not Found') |
2d53be02 | 215 | err['status'] = HttpStatusCode.NOT_FOUND_404 |
a030a9b2 C |
216 | next(err) |
217 | }) | |
218 | ||
6f4e2522 | 219 | app.use(function (err, req, res, next) { |
e3a682a8 C |
220 | let error = 'Unknown error.' |
221 | if (err) { | |
222 | error = err.stack || err.message || err | |
223 | } | |
224 | ||
328e607d C |
225 | // Sequelize error |
226 | const sql = err.parent ? err.parent.sql : undefined | |
227 | ||
228 | logger.error('Error in controller.', { err: error, sql }) | |
2d53be02 | 229 | return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() |
6f4e2522 | 230 | }) |
a030a9b2 | 231 | |
cef534ed | 232 | const server = createWebsocketTrackerServer(app) |
9b67da3d | 233 | |
79530164 C |
234 | // ----------- Run ----------- |
235 | ||
3d3441d6 | 236 | async function startApplication () { |
65fcc311 | 237 | const port = CONFIG.LISTEN.PORT |
cff8b272 | 238 | const hostname = CONFIG.LISTEN.HOSTNAME |
91fea9fc | 239 | |
3d3441d6 C |
240 | await installApplication() |
241 | ||
23687332 C |
242 | // Check activity pub urls are valid |
243 | checkActivityPubUrls() | |
244 | .catch(err => { | |
245 | logger.error('Error in ActivityPub URLs checker.', { err }) | |
246 | process.exit(-1) | |
247 | }) | |
248 | ||
3d3441d6 C |
249 | // Email initialization |
250 | Emailer.Instance.init() | |
3d3441d6 | 251 | |
0b2f03d3 | 252 | await Promise.all([ |
75594f47 | 253 | Emailer.Instance.checkConnection(), |
0b2f03d3 C |
254 | JobQueue.Instance.init() |
255 | ]) | |
3d3441d6 C |
256 | |
257 | // Caches initializations | |
d74d29ad C |
258 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE) |
259 | VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE) | |
3d3441d6 C |
260 | |
261 | // Enable Schedulers | |
2f5c6b2f | 262 | ActorFollowScheduler.Instance.enable() |
3d3441d6 | 263 | RemoveOldJobsScheduler.Instance.enable() |
2baea0c7 | 264 | UpdateVideosScheduler.Instance.enable() |
ce32426b | 265 | YoutubeDlUpdateScheduler.Instance.enable() |
c48e82b5 | 266 | VideosRedundancyScheduler.Instance.enable() |
8f0bc73d | 267 | RemoveOldHistoryScheduler.Instance.enable() |
cda03765 | 268 | RemoveOldViewsScheduler.Instance.enable() |
e0ce715a | 269 | PluginsCheckScheduler.Instance.enable() |
6f1b4fa4 | 270 | AutoFollowIndexInstances.Instance.enable() |
3d3441d6 C |
271 | |
272 | // Redis initialization | |
273 | Redis.Instance.init() | |
274 | ||
cef534ed C |
275 | PeerTubeSocket.Instance.init(server) |
276 | ||
ae9bbed4 C |
277 | updateStreamingPlaylistsInfohashesIfNeeded() |
278 | .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err })) | |
279 | ||
66e001c8 | 280 | if (cli.plugins) await PluginManager.Instance.registerPluginsAndThemes() |
f023a19c | 281 | |
c6c0fa6c C |
282 | LiveManager.Instance.init() |
283 | if (CONFIG.LIVE.ENABLED) LiveManager.Instance.run() | |
284 | ||
3d3441d6 | 285 | // Make server listening |
f55e5a7b | 286 | server.listen(port, hostname, () => { |
c2b82382 | 287 | logger.info('HTTP server listening on %s:%d', hostname, port) |
74dc3bca | 288 | logger.info('Web server: %s', WEBSERVER.URL) |
8d76959e | 289 | |
89cd1275 | 290 | Hooks.runAction('action:application.listening') |
f55e5a7b | 291 | }) |
14f2b3ad C |
292 | |
293 | process.on('exit', () => { | |
294 | JobQueue.Instance.terminate() | |
295 | }) | |
296 | ||
297 | process.on('SIGINT', () => process.exit(0)) | |
5804c0db | 298 | } |