]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
Improve progress bar
[github/Chocobozzz/PeerTube.git] / server.js
1 'use strict'
2
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
12
13 // Create our main app
14 var app = express()
15
16 // ----------- Checker -----------
17 var checker = require('./server/initializers/checker')
18
19 var miss = checker.checkConfig()
20 if (miss.length !== 0) {
21 throw new Error('Miss some configurations keys : ' + miss)
22 }
23
24 checker.createDirectoriesIfNotExist()
25
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')
38
39 // Get configurations
40 var port = config.get('listen.port')
41
42 // ----------- Database -----------
43 database.connect()
44
45 // ----------- Command line -----------
46
47 // ----------- App -----------
48
49 // For the logger
50 app.use(morgan('combined', { stream: logger.stream }))
51 // For body requests
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
57 }))
58
59 // ----------- Views, routes and static files -----------
60
61 // Livereload
62 app.use(require('connect-livereload')({
63 port: 35729
64 }))
65
66 // Catch sefaults
67 require('segfault-handler').registerHandler()
68
69 // API routes
70 var api_route = '/api/' + constants.API_VERSION
71 app.use(api_route, routes.api)
72
73 // Static files
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) {
77 res.sendStatus(404)
78 })
79
80 // Client application
81 app.use('/*', function (req, res, next) {
82 res.sendFile(path.join(__dirname, 'client/index.html'))
83 })
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 app.use(function (err, req, res, next) {
118 logger.error(err)
119 res.sendStatus(err.status || 500)
120 })
121
122 // ----------- Create the certificates if they don't already exist -----------
123 peertubeCrypto.createCertsIfNotExist(function (err) {
124 if (err) throw err
125 // Create/activate the webtorrent module
126 webtorrent.create(function () {
127 function cleanForExit () {
128 utils.cleanForExit(webtorrent.app)
129 }
130
131 function exitGracefullyOnSignal () {
132 process.exit(-1)
133 }
134
135 process.on('exit', cleanForExit)
136 process.on('SIGINT', exitGracefullyOnSignal)
137 process.on('SIGTERM', exitGracefullyOnSignal)
138
139 // ----------- Make the server listening -----------
140 server.listen(port, function () {
141 // Activate the pool requests
142 poolRequests.activate()
143
144 videos.seedAllExisting(function () {
145 logger.info('Seeded all the videos')
146 logger.info('Server listening on port %d', port)
147 app.emit('ready')
148 })
149 })
150 })
151 })
152
153 module.exports = app