diff options
-rw-r--r-- | controllers/api/v1/index.js | 10 | ||||
-rw-r--r-- | controllers/index.js | 7 | ||||
-rw-r--r-- | middlewares/index.js | 10 | ||||
-rw-r--r-- | middlewares/reqValidators/index.js | 10 | ||||
-rw-r--r-- | server.js | 324 |
5 files changed, 187 insertions, 174 deletions
diff --git a/controllers/api/v1/index.js b/controllers/api/v1/index.js index fc6bf4946..07a68ed9d 100644 --- a/controllers/api/v1/index.js +++ b/controllers/api/v1/index.js | |||
@@ -4,9 +4,13 @@ var express = require('express') | |||
4 | 4 | ||
5 | var router = express.Router() | 5 | var router = express.Router() |
6 | 6 | ||
7 | router.use('/pods', require('./pods')) | 7 | var podsController = require('./pods') |
8 | router.use('/remotevideos', require('./remoteVideos')) | 8 | var remoteVideosController = require('./remoteVideos') |
9 | router.use('/videos', require('./videos')) | 9 | var videosController = require('./videos') |
10 | |||
11 | router.use('/pods', podsController) | ||
12 | router.use('/remotevideos', remoteVideosController) | ||
13 | router.use('/videos', videosController) | ||
10 | 14 | ||
11 | // --------------------------------------------------------------------------- | 15 | // --------------------------------------------------------------------------- |
12 | 16 | ||
diff --git a/controllers/index.js b/controllers/index.js index 35a0191c5..858f493da 100644 --- a/controllers/index.js +++ b/controllers/index.js | |||
@@ -2,7 +2,10 @@ | |||
2 | 2 | ||
3 | var constants = require('../initializers/constants') | 3 | var constants = require('../initializers/constants') |
4 | 4 | ||
5 | var apiController = require('./api/' + constants.API_VERSION) | ||
6 | var viewsController = require('./views') | ||
7 | |||
5 | module.exports = { | 8 | module.exports = { |
6 | api: require('./api/' + constants.API_VERSION), | 9 | api: apiController, |
7 | views: require('./views') | 10 | views: viewsController |
8 | } | 11 | } |
diff --git a/middlewares/index.js b/middlewares/index.js index c76c4fc2e..c85899b0c 100644 --- a/middlewares/index.js +++ b/middlewares/index.js | |||
@@ -1,9 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | var cacheMiddleware = require('./cache') | ||
4 | var reqValidatorsMiddleware = require('./reqValidators') | ||
5 | var secureMiddleware = require('./secure') | ||
6 | |||
3 | var middlewares = { | 7 | var middlewares = { |
4 | cache: require('./cache'), | 8 | cache: cacheMiddleware, |
5 | reqValidators: require('./reqValidators'), | 9 | reqValidators: reqValidatorsMiddleware, |
6 | secure: require('./secure') | 10 | secure: secureMiddleware |
7 | } | 11 | } |
8 | 12 | ||
9 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
diff --git a/middlewares/reqValidators/index.js b/middlewares/reqValidators/index.js index 344387a80..345dbd0e2 100644 --- a/middlewares/reqValidators/index.js +++ b/middlewares/reqValidators/index.js | |||
@@ -1,9 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | var podsReqValidators = require('./pods') | ||
4 | var remoteReqValidators = require('./remote') | ||
5 | var videosReqValidators = require('./videos') | ||
6 | |||
3 | var reqValidators = { | 7 | var reqValidators = { |
4 | videos: require('./videos'), | 8 | pods: podsReqValidators, |
5 | pods: require('./pods'), | 9 | remote: remoteReqValidators, |
6 | remote: require('./remote') | 10 | videos: videosReqValidators |
7 | } | 11 | } |
8 | 12 | ||
9 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
@@ -1,171 +1,169 @@ | |||
1 | ;(function () { | 1 | 'use strict' |
2 | 'use strict' | 2 | |
3 | 3 | // ----------- Node modules ----------- | |
4 | // ----------- Node modules ----------- | 4 | var bodyParser = require('body-parser') |
5 | var bodyParser = require('body-parser') | 5 | var express = require('express') |
6 | var express = require('express') | 6 | var expressValidator = require('express-validator') |
7 | var expressValidator = require('express-validator') | 7 | var http = require('http') |
8 | var http = require('http') | 8 | var morgan = require('morgan') |
9 | var morgan = require('morgan') | 9 | var path = require('path') |
10 | var path = require('path') | 10 | var TrackerServer = require('bittorrent-tracker').Server |
11 | var TrackerServer = require('bittorrent-tracker').Server | 11 | var WebSocketServer = require('ws').Server |
12 | var WebSocketServer = require('ws').Server | 12 | |
13 | 13 | // Create our main app | |
14 | // Create our main app | 14 | var app = express() |
15 | var app = express() | 15 | |
16 | 16 | // ----------- Checker ----------- | |
17 | // ----------- Checker ----------- | 17 | var checker = require('./initializers/checker') |
18 | var checker = require('./initializers/checker') | 18 | |
19 | 19 | var miss = checker.checkConfig() | |
20 | var miss = checker.checkConfig() | 20 | if (miss.length !== 0) { |
21 | if (miss.length !== 0) { | 21 | // Do not use logger module |
22 | // Do not use logger module | 22 | console.error('Miss some configurations keys.', { miss: miss }) |
23 | console.error('Miss some configurations keys.', { miss: miss }) | 23 | process.exit(0) |
24 | process.exit(0) | 24 | } |
25 | } | 25 | |
26 | 26 | checker.createDirectoriesIfNotExist() | |
27 | checker.createDirectoriesIfNotExist() | 27 | |
28 | 28 | // ----------- PeerTube modules ----------- | |
29 | // ----------- PeerTube modules ----------- | 29 | var config = require('config') |
30 | var config = require('config') | 30 | var constants = require('./initializers/constants') |
31 | var constants = require('./initializers/constants') | 31 | var customValidators = require('./helpers/customValidators') |
32 | var customValidators = require('./helpers/customValidators') | 32 | var database = require('./initializers/database') |
33 | var database = require('./initializers/database') | 33 | var logger = require('./helpers/logger') |
34 | var logger = require('./helpers/logger') | 34 | var peertubeCrypto = require('./helpers/peertubeCrypto') |
35 | var peertubeCrypto = require('./helpers/peertubeCrypto') | 35 | var poolRequests = require('./lib/poolRequests') |
36 | var poolRequests = require('./lib/poolRequests') | 36 | var routes = require('./controllers') |
37 | var routes = require('./controllers') | 37 | var utils = require('./helpers/utils') |
38 | var utils = require('./helpers/utils') | 38 | var videos = require('./lib/videos') |
39 | var videos = require('./lib/videos') | 39 | var webtorrent = require('./lib/webtorrent') |
40 | var webtorrent = require('./lib/webtorrent') | 40 | |
41 | 41 | // Get configurations | |
42 | // Get configurations | 42 | var port = config.get('listen.port') |
43 | var port = config.get('listen.port') | 43 | |
44 | 44 | // ----------- Database ----------- | |
45 | // ----------- Database ----------- | 45 | database.connect() |
46 | database.connect() | 46 | |
47 | 47 | // ----------- Command line ----------- | |
48 | // ----------- Command line ----------- | 48 | |
49 | 49 | // ----------- App ----------- | |
50 | // ----------- App ----------- | 50 | |
51 | 51 | // For the logger | |
52 | // For the logger | 52 | app.use(morgan('combined', { stream: logger.stream })) |
53 | app.use(morgan('combined', { stream: logger.stream })) | 53 | // For body requests |
54 | // For body requests | 54 | app.use(bodyParser.json()) |
55 | app.use(bodyParser.json()) | 55 | app.use(bodyParser.urlencoded({ extended: false })) |
56 | app.use(bodyParser.urlencoded({ extended: false })) | 56 | // Validate some params for the API |
57 | // Validate some params for the API | 57 | app.use(expressValidator({ |
58 | app.use(expressValidator({ | 58 | customValidators: customValidators |
59 | customValidators: customValidators | 59 | })) |
60 | })) | 60 | |
61 | 61 | // ----------- Views, routes and static files ----------- | |
62 | // ----------- Views, routes and static files ----------- | 62 | |
63 | 63 | // Livereload | |
64 | // Livereload | 64 | app.use(require('connect-livereload')({ |
65 | app.use(require('connect-livereload')({ | 65 | port: 35729 |
66 | port: 35729 | 66 | })) |
67 | })) | 67 | |
68 | 68 | // Catch sefaults | |
69 | // Catch sefaults | 69 | require('segfault-handler').registerHandler() |
70 | require('segfault-handler').registerHandler() | 70 | |
71 | 71 | // Static files | |
72 | // Static files | 72 | app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 })) |
73 | app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 })) | 73 | |
74 | 74 | // Jade template from ./views directory | |
75 | // Jade template from ./views directory | 75 | app.set('views', path.join(__dirname, '/views')) |
76 | app.set('views', path.join(__dirname, '/views')) | 76 | app.set('view engine', 'jade') |
77 | app.set('view engine', 'jade') | 77 | |
78 | 78 | // API routes | |
79 | // API routes | 79 | var api_route = '/api/' + constants.API_VERSION |
80 | var api_route = '/api/' + constants.API_VERSION | 80 | app.use(api_route, routes.api) |
81 | app.use(api_route, routes.api) | 81 | |
82 | 82 | // Views routes | |
83 | // Views routes | 83 | app.use('/', routes.views) |
84 | app.use('/', routes.views) | 84 | |
85 | 85 | // ----------- Tracker ----------- | |
86 | // ----------- Tracker ----------- | 86 | |
87 | 87 | var trackerServer = new TrackerServer({ | |
88 | var trackerServer = new TrackerServer({ | 88 | http: false, |
89 | http: false, | 89 | udp: false, |
90 | udp: false, | 90 | ws: false, |
91 | ws: false, | 91 | dht: false |
92 | dht: false | 92 | }) |
93 | }) | 93 | |
94 | 94 | trackerServer.on('error', function (err) { | |
95 | trackerServer.on('error', function (err) { | 95 | logger.error(err) |
96 | }) | ||
97 | |||
98 | trackerServer.on('warning', function (err) { | ||
99 | logger.error(err) | ||
100 | }) | ||
101 | |||
102 | var server = http.createServer(app) | ||
103 | var wss = new WebSocketServer({server: server, path: '/tracker/socket'}) | ||
104 | wss.on('connection', function (ws) { | ||
105 | trackerServer.onWebSocketConnection(ws) | ||
106 | }) | ||
107 | |||
108 | // ----------- Errors ----------- | ||
109 | |||
110 | // Catch 404 and forward to error handler | ||
111 | app.use(function (req, res, next) { | ||
112 | var err = new Error('Not Found') | ||
113 | err.status = 404 | ||
114 | next(err) | ||
115 | }) | ||
116 | |||
117 | // Prod : no stacktraces leaked to user | ||
118 | if (process.env.NODE_ENV === 'production') { | ||
119 | app.use(function (err, req, res, next) { | ||
96 | logger.error(err) | 120 | logger.error(err) |
121 | res.status(err.status || 500) | ||
122 | res.render('error', { | ||
123 | message: err.message, | ||
124 | error: {} | ||
125 | }) | ||
97 | }) | 126 | }) |
98 | 127 | } else { | |
99 | trackerServer.on('warning', function (err) { | 128 | app.use(function (err, req, res, next) { |
100 | logger.error(err) | 129 | logger.error(err) |
101 | }) | 130 | res.status(err.status || 500) |
102 | 131 | res.render('error', { | |
103 | var server = http.createServer(app) | 132 | message: err.message, |
104 | var wss = new WebSocketServer({server: server, path: '/tracker/socket'}) | 133 | error: err |
105 | wss.on('connection', function (ws) { | ||
106 | trackerServer.onWebSocketConnection(ws) | ||
107 | }) | ||
108 | |||
109 | // ----------- Errors ----------- | ||
110 | |||
111 | // Catch 404 and forward to error handler | ||
112 | app.use(function (req, res, next) { | ||
113 | var err = new Error('Not Found') | ||
114 | err.status = 404 | ||
115 | next(err) | ||
116 | }) | ||
117 | |||
118 | // Prod : no stacktraces leaked to user | ||
119 | if (process.env.NODE_ENV === 'production') { | ||
120 | app.use(function (err, req, res, next) { | ||
121 | logger.error(err) | ||
122 | res.status(err.status || 500) | ||
123 | res.render('error', { | ||
124 | message: err.message, | ||
125 | error: {} | ||
126 | }) | ||
127 | }) | 134 | }) |
128 | } else { | 135 | }) |
129 | app.use(function (err, req, res, next) { | 136 | } |
130 | logger.error(err) | 137 | |
131 | res.status(err.status || 500) | 138 | // ----------- Create the certificates if they don't already exist ----------- |
132 | res.render('error', { | 139 | peertubeCrypto.createCertsIfNotExist(function (err) { |
133 | message: err.message, | 140 | if (err) throw err |
134 | error: err | 141 | // Create/activate the webtorrent module |
135 | }) | 142 | webtorrent.create(function () { |
136 | }) | 143 | function cleanForExit () { |
137 | } | 144 | utils.cleanForExit(webtorrent.app) |
138 | 145 | } | |
139 | // ----------- Create the certificates if they don't already exist ----------- | 146 | |
140 | peertubeCrypto.createCertsIfNotExist(function (err) { | 147 | function exitGracefullyOnSignal () { |
141 | if (err) throw err | 148 | process.exit() |
142 | // Create/activate the webtorrent module | 149 | } |
143 | webtorrent.create(function () { | 150 | |
144 | function cleanForExit () { | 151 | process.on('exit', cleanForExit) |
145 | utils.cleanForExit(webtorrent.app) | 152 | process.on('SIGINT', exitGracefullyOnSignal) |
146 | } | 153 | process.on('SIGTERM', exitGracefullyOnSignal) |
147 | 154 | ||
148 | function exitGracefullyOnSignal () { | 155 | // ----------- Make the server listening ----------- |
149 | process.exit() | 156 | server.listen(port, function () { |
150 | } | 157 | // Activate the pool requests |
151 | 158 | poolRequests.activate() | |
152 | process.on('exit', cleanForExit) | 159 | |
153 | process.on('SIGINT', exitGracefullyOnSignal) | 160 | videos.seedAllExisting(function () { |
154 | process.on('SIGTERM', exitGracefullyOnSignal) | 161 | logger.info('Seeded all the videos') |
155 | 162 | logger.info('Server listening on port %d', port) | |
156 | // ----------- Make the server listening ----------- | 163 | app.emit('ready') |
157 | server.listen(port, function () { | ||
158 | // Activate the pool requests | ||
159 | poolRequests.activate() | ||
160 | |||
161 | videos.seedAllExisting(function () { | ||
162 | logger.info('Seeded all the videos') | ||
163 | logger.info('Server listening on port %d', port) | ||
164 | app.emit('ready') | ||
165 | }) | ||
166 | }) | 164 | }) |
167 | }) | 165 | }) |
168 | }) | 166 | }) |
167 | }) | ||
169 | 168 | ||
170 | module.exports = app | 169 | module.exports = app |
171 | })() | ||