]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server.js
Styling structure
[github/Chocobozzz/PeerTube.git] / server.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
4 // ----------- Node modules -----------
a1860380 5 var bodyParser = require('body-parser')
8c308c2b 6 var express = require('express')
34ca3b52 7 var expressValidator = require('express-validator')
a1860380 8 var http = require('http')
8c308c2b 9 var morgan = require('morgan')
a1860380 10 var path = require('path')
8c308c2b
C
11 var TrackerServer = require('bittorrent-tracker').Server
12 var WebSocketServer = require('ws').Server
a1860380
C
13
14 // Create our main app
8c308c2b 15 var app = express()
8c308c2b
C
16
17 // ----------- Checker -----------
18 var checker = require('./src/checker')
19
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 })
24 process.exit(0)
25 }
26
a1860380 27 checker.createDirectoriesIfNotExist()
8c308c2b 28
3bcb78b3
C
29 // ----------- Constants -----------
30 var utils = require('./src/utils')
31
32 global.API_VERSION = 'v1'
33 global.FRIEND_BASE_SCORE = utils.isTestInstance() ? 20 : 100
34
d148f3b9 35 // ----------- PeerTube modules -----------
8c308c2b 36 var config = require('config')
0b697522 37 var customValidators = require('./src/customValidators')
8c308c2b 38 var logger = require('./src/logger')
0b697522 39 var poolRequests = require('./src/poolRequests')
8c308c2b 40 var routes = require('./routes')
8c308c2b
C
41 var videos = require('./src/videos')
42 var webtorrent = require('./src/webTorrentNode')
43
a1860380 44 // Get configurations
8c308c2b 45 var port = config.get('listen.port')
8c308c2b
C
46
47 // ----------- Command line -----------
48
49 // ----------- App -----------
a1860380
C
50
51 // For the logger
8c308c2b 52 app.use(morgan('combined', { stream: logger.stream }))
a1860380 53 // For body requests
8c308c2b 54 app.use(bodyParser.json())
8c308c2b 55 app.use(bodyParser.urlencoded({ extended: false }))
a1860380 56 // Validate some params for the API
0b697522
C
57 app.use(expressValidator({
58 customValidators: customValidators
59 }))
8c308c2b
C
60
61 // ----------- Views, routes and static files -----------
62
e85782f7
C
63 // Livereload
64 app.use(require('connect-livereload')({
65 port: 35729
66 }))
8c308c2b 67
a1860380 68 // Catch sefaults
e85782f7 69 require('segfault-handler').registerHandler()
8c308c2b 70
a1860380 71 // Static files
e85782f7 72 app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 }))
8c308c2b 73
e85782f7
C
74 // Jade template from ./views directory
75 app.set('views', path.join(__dirname, '/views'))
76 app.set('view engine', 'jade')
8c308c2b 77
a1860380 78 // API routes
f5a60a51 79 var api_route = '/api/' + global.API_VERSION
e85782f7
C
80 app.use(api_route, routes.api)
81
82 // Views routes
83 app.use('/', routes.views)
8c308c2b
C
84
85 // ----------- Tracker -----------
86
87 var trackerServer = new TrackerServer({
88 http: false,
89 udp: false,
90 ws: false,
91 dht: false
92 })
93
94 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) {
120 logger.error('Error : ' + err.message, { error: err })
121 res.status(err.status || 500)
122 res.render('error', {
123 message: err.message,
124 error: {}
125 })
126 })
127 } else {
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,
133 error: err
134 })
135 })
136 }
137
138 // ----------- Create the certificates if they don't already exist -----------
139 utils.createCertsIfNotExist(function (err) {
140 if (err) throw err
141 // Create/activate the webtorrent module
142 webtorrent.create(function () {
0ae2e7f7
C
143 function cleanForExit () {
144 utils.cleanForExit(webtorrent.app)
145 }
146
147 function exitGracefullyOnSignal () {
148 process.exit()
149 }
150
151 process.on('exit', cleanForExit)
152 process.on('SIGINT', exitGracefullyOnSignal)
153 process.on('SIGTERM', exitGracefullyOnSignal)
154
8c308c2b
C
155 // ----------- Make the server listening -----------
156 server.listen(port, function () {
0b697522
C
157 // Activate the pool requests
158 poolRequests.activate()
159
8c308c2b
C
160 videos.seedAll(function () {
161 logger.info('Seeded all the videos')
162 logger.info('Server listening on port %d', port)
163 app.emit('ready')
164 })
165 })
166 })
167 })
168
169 module.exports = app
170})()