aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rw-r--r--server.js324
1 files changed, 161 insertions, 163 deletions
diff --git a/server.js b/server.js
index c40513047..6312a4048 100644
--- a/server.js
+++ b/server.js
@@ -1,171 +1,169 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3// ----------- Node modules -----------
4 // ----------- Node modules ----------- 4var bodyParser = require('body-parser')
5 var bodyParser = require('body-parser') 5var express = require('express')
6 var express = require('express') 6var expressValidator = require('express-validator')
7 var expressValidator = require('express-validator') 7var http = require('http')
8 var http = require('http') 8var morgan = require('morgan')
9 var morgan = require('morgan') 9var path = require('path')
10 var path = require('path') 10var TrackerServer = require('bittorrent-tracker').Server
11 var TrackerServer = require('bittorrent-tracker').Server 11var WebSocketServer = require('ws').Server
12 var WebSocketServer = require('ws').Server 12
13 13// Create our main app
14 // Create our main app 14var app = express()
15 var app = express() 15
16 16// ----------- Checker -----------
17 // ----------- Checker ----------- 17var checker = require('./initializers/checker')
18 var checker = require('./initializers/checker') 18
19 19var miss = checker.checkConfig()
20 var miss = checker.checkConfig() 20if (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 26checker.createDirectoriesIfNotExist()
27 checker.createDirectoriesIfNotExist() 27
28 28// ----------- PeerTube modules -----------
29 // ----------- PeerTube modules ----------- 29var config = require('config')
30 var config = require('config') 30var constants = require('./initializers/constants')
31 var constants = require('./initializers/constants') 31var customValidators = require('./helpers/customValidators')
32 var customValidators = require('./helpers/customValidators') 32var database = require('./initializers/database')
33 var database = require('./initializers/database') 33var logger = require('./helpers/logger')
34 var logger = require('./helpers/logger') 34var peertubeCrypto = require('./helpers/peertubeCrypto')
35 var peertubeCrypto = require('./helpers/peertubeCrypto') 35var poolRequests = require('./lib/poolRequests')
36 var poolRequests = require('./lib/poolRequests') 36var routes = require('./controllers')
37 var routes = require('./controllers') 37var utils = require('./helpers/utils')
38 var utils = require('./helpers/utils') 38var videos = require('./lib/videos')
39 var videos = require('./lib/videos') 39var webtorrent = require('./lib/webtorrent')
40 var webtorrent = require('./lib/webtorrent') 40
41 41// Get configurations
42 // Get configurations 42var port = config.get('listen.port')
43 var port = config.get('listen.port') 43
44 44// ----------- Database -----------
45 // ----------- Database ----------- 45database.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 52app.use(morgan('combined', { stream: logger.stream }))
53 app.use(morgan('combined', { stream: logger.stream })) 53// For body requests
54 // For body requests 54app.use(bodyParser.json())
55 app.use(bodyParser.json()) 55app.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 57app.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 64app.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 69require('segfault-handler').registerHandler()
70 require('segfault-handler').registerHandler() 70
71 71// Static files
72 // Static files 72app.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 75app.set('views', path.join(__dirname, '/views'))
76 app.set('views', path.join(__dirname, '/views')) 76app.set('view engine', 'jade')
77 app.set('view engine', 'jade') 77
78 78// API routes
79 // API routes 79var api_route = '/api/' + constants.API_VERSION
80 var api_route = '/api/' + constants.API_VERSION 80app.use(api_route, routes.api)
81 app.use(api_route, routes.api) 81
82 82// Views routes
83 // Views routes 83app.use('/', routes.views)
84 app.use('/', routes.views) 84
85 85// ----------- Tracker -----------
86 // ----------- Tracker ----------- 86
87 87var 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 94trackerServer.on('error', function (err) {
95 trackerServer.on('error', function (err) { 95 logger.error(err)
96})
97
98trackerServer.on('warning', function (err) {
99 logger.error(err)
100})
101
102var server = http.createServer(app)
103var wss = new WebSocketServer({server: server, path: '/tracker/socket'})
104wss.on('connection', function (ws) {
105 trackerServer.onWebSocketConnection(ws)
106})
107
108// ----------- Errors -----------
109
110// Catch 404 and forward to error handler
111app.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
118if (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', { 139peertubeCrypto.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 169module.exports = app
171})()