]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/pods.js
Server: finish old jobs at startup
[github/Chocobozzz/PeerTube.git] / server / middlewares / pods.js
1 'use strict'
2
3 const constants = require('../initializers/constants')
4
5 const podsMiddleware = {
6 setBodyHostsPort,
7 setBodyHostPort
8 }
9
10 function setBodyHostsPort (req, res, next) {
11 if (!req.body.hosts) return next()
12
13 for (let i = 0; i < req.body.hosts.length; i++) {
14 const hostWithPort = getHostWithPort(req.body.hosts[i])
15
16 // Problem with the url parsing?
17 if (hostWithPort === null) {
18 return res.sendStatus(500)
19 }
20
21 req.body.hosts[i] = hostWithPort
22 }
23
24 return next()
25 }
26
27 function setBodyHostPort (req, res, next) {
28 if (!req.body.host) return next()
29
30 const hostWithPort = getHostWithPort(req.body.host)
31
32 // Problem with the url parsing?
33 if (hostWithPort === null) {
34 return res.sendStatus(500)
35 }
36
37 req.body.host = hostWithPort
38
39 return next()
40 }
41
42 // ---------------------------------------------------------------------------
43
44 module.exports = podsMiddleware
45
46 // ---------------------------------------------------------------------------
47
48 function getHostWithPort (host) {
49 const splitted = host.split(':')
50
51 // The port was not specified
52 if (splitted.length === 1) {
53 if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
54
55 return host + ':80'
56 }
57
58 return host
59 }