aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server.ts')
-rw-r--r--server.ts66
1 files changed, 44 insertions, 22 deletions
diff --git a/server.ts b/server.ts
index b50151859..aa4382ee7 100644
--- a/server.ts
+++ b/server.ts
@@ -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)
30import { logger } from './server/helpers/logger' 30import { logger } from './server/helpers/logger'
31import { API_VERSION, CONFIG, CACHE } from './server/initializers/constants' 31import { API_VERSION, FILES_CACHE, WEBSERVER, loadLanguages } from './server/initializers/constants'
32import { CONFIG } from './server/initializers/config'
32 33
33const missed = checkMissedConfig() 34const missed = checkMissedConfig()
34if (missed.length !== 0) { 35if (missed.length !== 0) {
@@ -53,15 +54,17 @@ if (errorMessage !== null) {
53app.set('trust proxy', CONFIG.TRUST_PROXY) 54app.set('trust proxy', CONFIG.TRUST_PROXY)
54 55
55// Security middleware 56// Security middleware
56import { baseCSP } from './server/middlewares' 57import { baseCSP } from './server/middlewares/csp'
57 58
58app.use(baseCSP) 59if (CONFIG.CSP.ENABLED) {
59app.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 -----------
83loadLanguages()
84
79// ----------- PeerTube modules ----------- 85// ----------- PeerTube modules -----------
80import { installApplication } from './server/initializers' 86import { installApplication } from './server/initializers'
81import { Emailer } from './server/lib/emailer' 87import { Emailer } from './server/lib/emailer'
82import { JobQueue } from './server/lib/job-queue' 88import { JobQueue } from './server/lib/job-queue'
83import { VideosPreviewCache, VideosCaptionCache } from './server/lib/cache' 89import { VideosPreviewCache, VideosCaptionCache } from './server/lib/files-cache'
84import { 90import {
85 activityPubRouter, 91 activityPubRouter,
86 apiRouter, 92 apiRouter,
@@ -95,12 +101,15 @@ import {
95import { advertiseDoNotTrack } from './server/middlewares/dnt' 101import { advertiseDoNotTrack } from './server/middlewares/dnt'
96import { Redis } from './server/lib/redis' 102import { Redis } from './server/lib/redis'
97import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' 103import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler'
104import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler'
98import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' 105import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
99import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' 106import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
100import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' 107import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
101import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler' 108import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler'
109import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-history-scheduler'
102import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto' 110import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto'
103import { PeerTubeSocket } from './server/lib/peertube-socket' 111import { PeerTubeSocket } from './server/lib/peertube-socket'
112import { 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
122morgan.token('remote-addr', req => { 132morgan.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})
139morgan.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})
130morgan.token('user-agent', req => (req.get('DNT') === '1') ?
131 useragent.parse(req.get('user-agent')).family : req.get('user-agent'))
132app.use(morgan('combined', { 146app.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
136app.use(bodyParser.urlencoded({ extended: false })) 151app.use(bodyParser.urlencoded({ extended: false }))
137app.use(bodyParser.json({ 152app.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
146app.use(cookieParser()) 162app.use(cookieParser())
163
147// W3C DNT Tracking Status 164// W3C DNT Tracking Status
148app.use(advertiseDoNotTrack) 165app.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', () => {