diff options
Diffstat (limited to 'server.ts')
-rw-r--r-- | server.ts | 66 |
1 files changed, 44 insertions, 22 deletions
@@ -28,7 +28,8 @@ import { checkMissedConfig, checkFFmpeg } from './server/initializers/checker-be | |||
28 | 28 | ||
29 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) | 29 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) |
30 | import { logger } from './server/helpers/logger' | 30 | import { logger } from './server/helpers/logger' |
31 | import { API_VERSION, CONFIG, CACHE } from './server/initializers/constants' | 31 | import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants' |
32 | import { CONFIG } from './server/initializers/config' | ||
32 | 33 | ||
33 | const missed = checkMissedConfig() | 34 | const missed = checkMissedConfig() |
34 | if (missed.length !== 0) { | 35 | if (missed.length !== 0) { |
@@ -53,15 +54,17 @@ if (errorMessage !== null) { | |||
53 | app.set('trust proxy', CONFIG.TRUST_PROXY) | 54 | app.set('trust proxy', CONFIG.TRUST_PROXY) |
54 | 55 | ||
55 | // Security middleware | 56 | // Security middleware |
56 | import { baseCSP } from './server/middlewares' | 57 | import { baseCSP } from './server/middlewares/csp' |
57 | 58 | ||
58 | app.use(baseCSP) | 59 | if (CONFIG.CSP.ENABLED) { |
59 | app.use(helmet({ | 60 | app.use(baseCSP) |
60 | frameguard: { | 61 | app.use(helmet({ |
61 | action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts | 62 | frameguard: { |
62 | }, | 63 | action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts |
63 | hsts: false | 64 | }, |
64 | })) | 65 | hsts: false |
66 | })) | ||
67 | } | ||
65 | 68 | ||
66 | // ----------- Database ----------- | 69 | // ----------- Database ----------- |
67 | 70 | ||
@@ -76,11 +79,14 @@ migrate() | |||
76 | process.exit(-1) | 79 | process.exit(-1) |
77 | }) | 80 | }) |
78 | 81 | ||
82 | // ----------- Initialize ----------- | ||
83 | loadLanguages() | ||
84 | |||
79 | // ----------- PeerTube modules ----------- | 85 | // ----------- PeerTube modules ----------- |
80 | import { installApplication } from './server/initializers' | 86 | import { installApplication } from './server/initializers' |
81 | import { Emailer } from './server/lib/emailer' | 87 | import { Emailer } from './server/lib/emailer' |
82 | import { JobQueue } from './server/lib/job-queue' | 88 | import { JobQueue } from './server/lib/job-queue' |
83 | import { VideosPreviewCache, VideosCaptionCache } from './server/lib/cache' | 89 | import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache' |
84 | import { | 90 | import { |
85 | activityPubRouter, | 91 | activityPubRouter, |
86 | apiRouter, | 92 | apiRouter, |
@@ -95,12 +101,15 @@ import { | |||
95 | import { advertiseDoNotTrack } from './server/middlewares/dnt' | 101 | import { advertiseDoNotTrack } from './server/middlewares/dnt' |
96 | import { Redis } from './server/lib/redis' | 102 | import { Redis } from './server/lib/redis' |
97 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' | 103 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' |
104 | import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' | ||
98 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' | 105 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' |
99 | import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' | 106 | import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' |
100 | import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' | 107 | import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' |
101 | import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler' | 108 | import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler' |
109 | import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler' | ||
102 | import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto' | 110 | import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto' |
103 | import { PeerTubeSocket } from './server/lib/peertube-socket' | 111 | import { PeerTubeSocket } from './server/lib/peertube-socket' |
112 | import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls' | ||
104 | 113 | ||
105 | // ----------- Command line ----------- | 114 | // ----------- Command line ----------- |
106 | 115 | ||
@@ -118,20 +127,26 @@ if (isTestInstance()) { | |||
118 | credentials: true | 127 | credentials: true |
119 | })) | 128 | })) |
120 | } | 129 | } |
130 | |||
121 | // For the logger | 131 | // For the logger |
122 | morgan.token('remote-addr', req => { | 132 | morgan.token('remote-addr', req => { |
123 | return (req.get('DNT') === '1') ? | 133 | if (req.get('DNT') === '1') { |
124 | anonymize(req.ip || (req.connection && req.connection.remoteAddress) || undefined, | 134 | return anonymize(req.ip, 16, 16) |
125 | 16, // bitmask for IPv4 | 135 | } |
126 | 16 // bitmask for IPv6 | 136 | |
127 | ) : | 137 | return req.ip |
128 | req.ip | 138 | }) |
139 | morgan.token('user-agent', req => { | ||
140 | if (req.get('DNT') === '1') { | ||
141 | return useragent.parse(req.get('user-agent')).family | ||
142 | } | ||
143 | |||
144 | return req.get('user-agent') | ||
129 | }) | 145 | }) |
130 | morgan.token('user-agent', req => (req.get('DNT') === '1') ? | ||
131 | useragent.parse(req.get('user-agent')).family : req.get('user-agent')) | ||
132 | app.use(morgan('combined', { | 146 | app.use(morgan('combined', { |
133 | stream: { write: logger.info.bind(logger) } | 147 | stream: { write: logger.info.bind(logger) } |
134 | })) | 148 | })) |
149 | |||
135 | // For body requests | 150 | // For body requests |
136 | app.use(bodyParser.urlencoded({ extended: false })) | 151 | app.use(bodyParser.urlencoded({ extended: false })) |
137 | app.use(bodyParser.json({ | 152 | app.use(bodyParser.json({ |
@@ -142,8 +157,10 @@ app.use(bodyParser.json({ | |||
142 | if (valid !== true) throw new Error('Invalid digest') | 157 | if (valid !== true) throw new Error('Invalid digest') |
143 | } | 158 | } |
144 | })) | 159 | })) |
160 | |||
145 | // Cookies | 161 | // Cookies |
146 | app.use(cookieParser()) | 162 | app.use(cookieParser()) |
163 | |||
147 | // W3C DNT Tracking Status | 164 | // W3C DNT Tracking Status |
148 | app.use(advertiseDoNotTrack) | 165 | app.use(advertiseDoNotTrack) |
149 | 166 | ||
@@ -216,8 +233,8 @@ async function startApplication () { | |||
216 | ]) | 233 | ]) |
217 | 234 | ||
218 | // Caches initializations | 235 | // Caches initializations |
219 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, CACHE.PREVIEWS.MAX_AGE) | 236 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE) |
220 | VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, CACHE.VIDEO_CAPTIONS.MAX_AGE) | 237 | VideosCaptionCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE) |
221 | 238 | ||
222 | // Enable Schedulers | 239 | // Enable Schedulers |
223 | ActorFollowScheduler.Instance.enable() | 240 | ActorFollowScheduler.Instance.enable() |
@@ -225,16 +242,21 @@ async function startApplication () { | |||
225 | UpdateVideosScheduler.Instance.enable() | 242 | UpdateVideosScheduler.Instance.enable() |
226 | YoutubeDlUpdateScheduler.Instance.enable() | 243 | YoutubeDlUpdateScheduler.Instance.enable() |
227 | VideosRedundancyScheduler.Instance.enable() | 244 | VideosRedundancyScheduler.Instance.enable() |
245 | RemoveOldHistoryScheduler.Instance.enable() | ||
246 | RemoveOldViewsScheduler.Instance.enable() | ||
228 | 247 | ||
229 | // Redis initialization | 248 | // Redis initialization |
230 | Redis.Instance.init() | 249 | Redis.Instance.init() |
231 | 250 | ||
232 | PeerTubeSocket.Instance.init(server) | 251 | PeerTubeSocket.Instance.init(server) |
233 | 252 | ||
253 | updateStreamingPlaylistsInfohashesIfNeeded() | ||
254 | .catch(err => logger.error('Cannot update streaming playlist infohashes.', { err })) | ||
255 | |||
234 | // Make server listening | 256 | // Make server listening |
235 | server.listen(port, hostname, () => { | 257 | server.listen(port, hostname, () => { |
236 | logger.info('Server listening on %s:%d', hostname, port) | 258 | logger.info('Server listening on %s:%d', hostname, port) |
237 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) | 259 | logger.info('Web server: %s', WEBSERVER.URL) |
238 | }) | 260 | }) |
239 | 261 | ||
240 | process.on('exit', () => { | 262 | process.on('exit', () => { |