]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
3 // ----------- Node modules -----------
4 var bodyParser
= require('body-parser')
5 var express
= require('express')
6 var expressValidator
= require('express-validator')
7 var http
= require('http')
8 var morgan
= require('morgan')
9 var path
= require('path')
10 var TrackerServer
= require('bittorrent-tracker').Server
11 var WebSocketServer
= require('ws').Server
13 // Create our main app
16 // ----------- Checker -----------
17 var checker
= require('./server/initializers/checker')
19 var miss
= checker
.checkConfig()
20 if (miss
.length
!== 0) {
21 throw new Error('Miss some configurations keys : ' + miss
)
24 checker
.createDirectoriesIfNotExist()
26 // ----------- PeerTube modules -----------
27 var config
= require('config')
28 var constants
= require('./server/initializers/constants')
29 var customValidators
= require('./server/helpers/customValidators')
30 var database
= require('./server/initializers/database')
31 var logger
= require('./server/helpers/logger')
32 var peertubeCrypto
= require('./server/helpers/peertubeCrypto')
33 var poolRequests
= require('./server/lib/poolRequests')
34 var routes
= require('./server/controllers')
35 var utils
= require('./server/helpers/utils')
36 var videos
= require('./server/lib/videos')
37 var webtorrent
= require('./server/lib/webtorrent')
40 var port
= config
.get('listen.port')
42 // ----------- Database -----------
45 // ----------- Command line -----------
47 // ----------- App -----------
50 app
.use(morgan('combined', { stream: logger
.stream
}))
52 app
.use(bodyParser
.json())
53 app
.use(bodyParser
.urlencoded({ extended: false }))
54 // Validate some params for the API
55 app
.use(expressValidator({
56 customValidators: customValidators
59 // ----------- Views, routes and static files -----------
62 app
.use(require('connect-livereload')({
67 require('segfault-handler').registerHandler()
70 var api_route
= '/api/' + constants
.API_VERSION
71 app
.use(api_route
, routes
.api
)
74 app
.use('/app', express
.static(path
.join(__dirname
, '/client'), { maxAge: 0 }))
75 // 404 for static files not found
76 app
.use('/app/*', function (req
, res
, next
) {
81 app
.use('/*', function (req
, res
, next
) {
82 res
.sendFile(path
.join(__dirname
, 'client/index.html'))
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 app
.use(function (err
, req
, res
, next
) {
119 res
.sendStatus(err
.status
|| 500)
122 // ----------- Create the certificates if they don't already exist -----------
123 peertubeCrypto
.createCertsIfNotExist(function (err
) {
125 // Create/activate the webtorrent module
126 webtorrent
.create(function () {
127 function cleanForExit () {
128 utils
.cleanForExit(webtorrent
.app
)
131 function exitGracefullyOnSignal () {
135 process
.on('exit', cleanForExit
)
136 process
.on('SIGINT', exitGracefullyOnSignal
)
137 process
.on('SIGTERM', exitGracefullyOnSignal
)
139 // ----------- Make the server listening -----------
140 server
.listen(port
, function () {
141 // Activate the pool requests
142 poolRequests
.activate()
144 videos
.seedAllExisting(function () {
145 logger
.info('Seeded all the videos')
146 logger
.info('Server listening on port %d', port
)