]>
Commit | Line | Data |
---|---|---|
a030a9b2 C |
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 ----------- | |
b9a3e09a | 17 | var checker = require('./server/initializers/checker') |
a030a9b2 C |
18 | |
19 | var miss = checker.checkConfig() | |
20 | if (miss.length !== 0) { | |
ac2f99eb | 21 | throw new Error('Miss some configurations keys : ' + miss) |
a030a9b2 C |
22 | } |
23 | ||
24 | checker.createDirectoriesIfNotExist() | |
25 | ||
26 | // ----------- PeerTube modules ----------- | |
27 | var config = require('config') | |
b9a3e09a C |
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') | |
a030a9b2 C |
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 | ||
a030a9b2 C |
69 | // API routes |
70 | var api_route = '/api/' + constants.API_VERSION | |
71 | app.use(api_route, routes.api) | |
72 | ||
bd324a66 C |
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 | ||
6f4e2522 C |
80 | // Client application |
81 | app.use('/*', function (req, res, next) { | |
82 | res.sendFile(path.join(__dirname, 'client/index.html')) | |
83 | }) | |
a030a9b2 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 | ||
6f4e2522 C |
117 | app.use(function (err, req, res, next) { |
118 | logger.error(err) | |
119 | res.sendStatus(err.status || 500) | |
120 | }) | |
a030a9b2 | 121 | |
9457bf88 C |
122 | // TODO: move into initializer |
123 | require('./server/models/users').createClient('coucou', [ 'password' ], function (err, id) { | |
124 | if (err) throw err | |
125 | logger.info('Client id: ' + id) | |
126 | ||
127 | require('./server/models/users').createUser('floflo', 'coucou', function () {}) | |
128 | }) | |
129 | ||
a030a9b2 C |
130 | // ----------- Create the certificates if they don't already exist ----------- |
131 | peertubeCrypto.createCertsIfNotExist(function (err) { | |
132 | if (err) throw err | |
133 | // Create/activate the webtorrent module | |
134 | webtorrent.create(function () { | |
135 | function cleanForExit () { | |
136 | utils.cleanForExit(webtorrent.app) | |
137 | } | |
138 | ||
139 | function exitGracefullyOnSignal () { | |
ac2f99eb | 140 | process.exit(-1) |
a030a9b2 C |
141 | } |
142 | ||
143 | process.on('exit', cleanForExit) | |
144 | process.on('SIGINT', exitGracefullyOnSignal) | |
145 | process.on('SIGTERM', exitGracefullyOnSignal) | |
146 | ||
147 | // ----------- Make the server listening ----------- | |
148 | server.listen(port, function () { | |
149 | // Activate the pool requests | |
150 | poolRequests.activate() | |
151 | ||
152 | videos.seedAllExisting(function () { | |
153 | logger.info('Seeded all the videos') | |
154 | logger.info('Server listening on port %d', port) | |
155 | app.emit('ready') | |
8c308c2b C |
156 | }) |
157 | }) | |
158 | }) | |
a030a9b2 | 159 | }) |
8c308c2b | 160 | |
a030a9b2 | 161 | module.exports = app |