aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server.ts')
-rw-r--r--server.ts52
1 files changed, 33 insertions, 19 deletions
diff --git a/server.ts b/server.ts
index 97dffe756..e46300dce 100644
--- a/server.ts
+++ b/server.ts
@@ -7,7 +7,6 @@ if (isTestInstance()) {
7} 7}
8 8
9// ----------- Node modules ----------- 9// ----------- Node modules -----------
10import * as bodyParser from 'body-parser'
11import * as express from 'express' 10import * as express from 'express'
12import * as morgan from 'morgan' 11import * as morgan from 'morgan'
13import * as cors from 'cors' 12import * as cors from 'cors'
@@ -15,7 +14,7 @@ import * as cookieParser from 'cookie-parser'
15import * as helmet from 'helmet' 14import * as helmet from 'helmet'
16import * as useragent from 'useragent' 15import * as useragent from 'useragent'
17import * as anonymize from 'ip-anonymize' 16import * as anonymize from 'ip-anonymize'
18import * as cli from 'commander' 17import { program as cli } from 'commander'
19 18
20process.title = 'peertube' 19process.title = 'peertube'
21 20
@@ -107,6 +106,7 @@ import {
107 downloadRouter 106 downloadRouter
108} from './server/controllers' 107} from './server/controllers'
109import { advertiseDoNotTrack } from './server/middlewares/dnt' 108import { advertiseDoNotTrack } from './server/middlewares/dnt'
109import { apiFailMiddleware } from './server/middlewares/error'
110import { Redis } from './server/lib/redis' 110import { Redis } from './server/lib/redis'
111import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' 111import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler'
112import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' 112import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler'
@@ -124,9 +124,10 @@ import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-sch
124import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler' 124import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler'
125import { Hooks } from './server/lib/plugins/hooks' 125import { Hooks } from './server/lib/plugins/hooks'
126import { PluginManager } from './server/lib/plugins/plugin-manager' 126import { PluginManager } from './server/lib/plugins/plugin-manager'
127import { LiveManager } from './server/lib/live-manager' 127import { LiveManager } from './server/lib/live'
128import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' 128import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes'
129import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' 129import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
130import { ServerConfigManager } from '@server/lib/server-config-manager'
130 131
131// ----------- Command line ----------- 132// ----------- Command line -----------
132 133
@@ -163,19 +164,28 @@ morgan.token('user-agent', (req: express.Request) => {
163}) 164})
164app.use(morgan('combined', { 165app.use(morgan('combined', {
165 stream: { 166 stream: {
166 write: (str: string) => logger.info(str, { tags: [ 'http' ] }) 167 write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] })
167 }, 168 },
168 skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' 169 skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
169})) 170}))
170 171
172// Add .fail() helper to response
173app.use(apiFailMiddleware)
174
171// For body requests 175// For body requests
172app.use(bodyParser.urlencoded({ extended: false })) 176app.use(express.urlencoded({ extended: false }))
173app.use(bodyParser.json({ 177app.use(express.json({
174 type: [ 'application/json', 'application/*+json' ], 178 type: [ 'application/json', 'application/*+json' ],
175 limit: '500kb', 179 limit: '500kb',
176 verify: (req: express.Request, _, buf: Buffer) => { 180 verify: (req: express.Request, res: express.Response, buf: Buffer) => {
177 const valid = isHTTPSignatureDigestValid(buf, req) 181 const valid = isHTTPSignatureDigestValid(buf, req)
178 if (valid !== true) throw new Error('Invalid digest') 182
183 if (valid !== true) {
184 res.fail({
185 status: HttpStatusCode.FORBIDDEN_403,
186 message: 'Invalid digest'
187 })
188 }
179 } 189 }
180})) 190}))
181 191
@@ -217,24 +227,27 @@ if (cliOptions.client) app.use('/', clientsRouter)
217 227
218// ----------- Errors ----------- 228// ----------- Errors -----------
219 229
220// Catch 404 and forward to error handler 230// Catch unmatched routes
221app.use(function (req, res, next) { 231app.use((req, res: express.Response) => {
222 const err = new Error('Not Found') 232 res.status(HttpStatusCode.NOT_FOUND_404).end()
223 err['status'] = HttpStatusCode.NOT_FOUND_404
224 next(err)
225}) 233})
226 234
227app.use(function (err, req, res, next) { 235// Catch thrown errors
236app.use((err, req, res: express.Response, next) => {
237 // Format error to be logged
228 let error = 'Unknown error.' 238 let error = 'Unknown error.'
229 if (err) { 239 if (err) {
230 error = err.stack || err.message || err 240 error = err.stack || err.message || err
231 } 241 }
232 242 // Handling Sequelize error traces
233 // Sequelize error
234 const sql = err.parent ? err.parent.sql : undefined 243 const sql = err.parent ? err.parent.sql : undefined
235
236 logger.error('Error in controller.', { err: error, sql }) 244 logger.error('Error in controller.', { err: error, sql })
237 return res.status(err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() 245
246 return res.fail({
247 status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
248 message: err.message,
249 type: err.name
250 })
238}) 251})
239 252
240const server = createWebsocketTrackerServer(app) 253const server = createWebsocketTrackerServer(app)
@@ -262,7 +275,8 @@ async function startApplication () {
262 275
263 await Promise.all([ 276 await Promise.all([
264 Emailer.Instance.checkConnection(), 277 Emailer.Instance.checkConnection(),
265 JobQueue.Instance.init() 278 JobQueue.Instance.init(),
279 ServerConfigManager.Instance.init()
266 ]) 280 ])
267 281
268 // Caches initializations 282 // Caches initializations