4 // ----------- Node modules -----------
5 var bodyParser = require('body-parser')
6 var express = require('express')
7 var expressValidator = require('express-validator')
8 var http = require('http')
9 var morgan = require('morgan')
10 var path = require('path')
11 var TrackerServer = require('bittorrent-tracker').Server
12 var WebSocketServer = require('ws').Server
14 // Create our main app
17 // ----------- Checker -----------
18 var checker = require('./src/checker')
20 var miss = checker.checkConfig()
21 if (miss.length !== 0) {
22 // Do not use logger module
23 console.error('Miss some configurations keys.', { miss: miss })
27 checker.createDirectoriesIfNotExist()
29 // ----------- Constants -----------
30 var utils = require('./src/utils')
32 global.API_VERSION = 'v1'
33 global.FRIEND_BASE_SCORE = utils.isTestInstance() ? 20 : 100
35 // ----------- PeerTube modules -----------
36 var config = require('config')
37 var customValidators = require('./src/customValidators')
38 var logger = require('./src/logger')
39 var poolRequests = require('./src/poolRequests')
40 var routes = require('./routes')
41 var videos = require('./src/videos')
42 var webtorrent = require('./src/webTorrentNode')
45 var port = config.get('listen.port')
47 // ----------- Command line -----------
49 // ----------- App -----------
52 app.use(morgan('combined', { stream: logger.stream }))
54 app.use(bodyParser.json())
55 app.use(bodyParser.urlencoded({ extended: false }))
56 // Validate some params for the API
57 app.use(expressValidator({
58 customValidators: customValidators
61 // ----------- Views, routes and static files -----------
64 app.use(require('connect-livereload')({
69 require('segfault-handler').registerHandler()
72 app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 }))
74 // Jade template from ./views directory
75 app.set('views', path.join(__dirname, '/views'))
76 app.set('view engine', 'jade')
79 var api_route = '/api/' + global.API_VERSION
80 app.use(api_route, routes.api)
83 app.use('/', routes.views)
85 // ----------- Tracker -----------
87 var trackerServer = new TrackerServer({
94 trackerServer.on('error', function (err) {
98 trackerServer.on('warning', function (err) {
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)
108 // ----------- Errors -----------
110 // Catch 404 and forward to error handler
111 app.use(function (req, res, next) {
112 var err = new Error('Not Found')
117 // Prod : no stacktraces leaked to user
118 if (process.env.NODE_ENV === 'production') {
119 app.use(function (err, req, res, next) {
120 logger.error('Error : ' + err.message, { error: err })
121 res.status(err.status || 500)
122 res.render('error', {
123 message: err.message,
128 app.use(function (err, req, res, next) {
129 logger.error('Error : ' + err.message, { error: err })
130 res.status(err.status || 500)
131 res.render('error', {
132 message: err.message,
138 // ----------- Create the certificates if they don't already exist -----------
139 utils.createCertsIfNotExist(function (err) {
141 // Create/activate the webtorrent module
142 webtorrent.create(function () {
143 function cleanForExit () {
144 utils.cleanForExit(webtorrent.app)
147 function exitGracefullyOnSignal () {
151 process.on('exit', cleanForExit)
152 process.on('SIGINT', exitGracefullyOnSignal)
153 process.on('SIGTERM', exitGracefullyOnSignal)
155 // ----------- Make the server listening -----------
156 server.listen(port, function () {
157 // Activate the pool requests
158 poolRequests.activate()
160 videos.seedAll(function () {
161 logger.info('Seeded all the videos')
162 logger.info('Server listening on port %d', port)