diff options
Diffstat (limited to 'server.ts')
-rw-r--r-- | server.ts | 52 |
1 files changed, 33 insertions, 19 deletions
@@ -7,7 +7,6 @@ if (isTestInstance()) { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | // ----------- Node modules ----------- | 9 | // ----------- Node modules ----------- |
10 | import * as bodyParser from 'body-parser' | ||
11 | import * as express from 'express' | 10 | import * as express from 'express' |
12 | import * as morgan from 'morgan' | 11 | import * as morgan from 'morgan' |
13 | import * as cors from 'cors' | 12 | import * as cors from 'cors' |
@@ -15,7 +14,7 @@ import * as cookieParser from 'cookie-parser' | |||
15 | import * as helmet from 'helmet' | 14 | import * as helmet from 'helmet' |
16 | import * as useragent from 'useragent' | 15 | import * as useragent from 'useragent' |
17 | import * as anonymize from 'ip-anonymize' | 16 | import * as anonymize from 'ip-anonymize' |
18 | import * as cli from 'commander' | 17 | import { program as cli } from 'commander' |
19 | 18 | ||
20 | process.title = 'peertube' | 19 | process.title = 'peertube' |
21 | 20 | ||
@@ -107,6 +106,7 @@ import { | |||
107 | downloadRouter | 106 | downloadRouter |
108 | } from './server/controllers' | 107 | } from './server/controllers' |
109 | import { advertiseDoNotTrack } from './server/middlewares/dnt' | 108 | import { advertiseDoNotTrack } from './server/middlewares/dnt' |
109 | import { apiFailMiddleware } from './server/middlewares/error' | ||
110 | import { Redis } from './server/lib/redis' | 110 | import { Redis } from './server/lib/redis' |
111 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' | 111 | import { ActorFollowScheduler } from './server/lib/schedulers/actor-follow-scheduler' |
112 | import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' | 112 | import { RemoveOldViewsScheduler } from './server/lib/schedulers/remove-old-views-scheduler' |
@@ -124,9 +124,10 @@ import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-sch | |||
124 | import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler' | 124 | import { PeerTubeVersionCheckScheduler } from './server/lib/schedulers/peertube-version-check-scheduler' |
125 | import { Hooks } from './server/lib/plugins/hooks' | 125 | import { Hooks } from './server/lib/plugins/hooks' |
126 | import { PluginManager } from './server/lib/plugins/plugin-manager' | 126 | import { PluginManager } from './server/lib/plugins/plugin-manager' |
127 | import { LiveManager } from './server/lib/live-manager' | 127 | import { LiveManager } from './server/lib/live' |
128 | import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' | 128 | import { HttpStatusCode } from './shared/core-utils/miscs/http-error-codes' |
129 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' | 129 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' |
130 | import { 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 | }) |
164 | app.use(morgan('combined', { | 165 | app.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 | ||
173 | app.use(apiFailMiddleware) | ||
174 | |||
171 | // For body requests | 175 | // For body requests |
172 | app.use(bodyParser.urlencoded({ extended: false })) | 176 | app.use(express.urlencoded({ extended: false })) |
173 | app.use(bodyParser.json({ | 177 | app.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 |
221 | app.use(function (req, res, next) { | 231 | app.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 | ||
227 | app.use(function (err, req, res, next) { | 235 | // Catch thrown errors |
236 | app.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 | ||
240 | const server = createWebsocketTrackerServer(app) | 253 | const 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 |