]>
Commit | Line | Data |
---|---|---|
a030a9b2 C |
1 | 'use strict' |
2 | ||
3 | // ----------- Node modules ----------- | |
13ce1d01 C |
4 | const bodyParser = require('body-parser') |
5 | const express = require('express') | |
6 | const expressValidator = require('express-validator') | |
7 | const http = require('http') | |
8 | const morgan = require('morgan') | |
9 | const path = require('path') | |
10 | const TrackerServer = require('bittorrent-tracker').Server | |
11 | const WebSocketServer = require('ws').Server | |
a030a9b2 | 12 | |
9f540774 C |
13 | process.title = 'peertube' |
14 | ||
a030a9b2 | 15 | // Create our main app |
13ce1d01 | 16 | const app = express() |
a030a9b2 | 17 | |
00057e85 | 18 | // ----------- Database ----------- |
13ce1d01 | 19 | const constants = require('./server/initializers/constants') |
13ce1d01 | 20 | const logger = require('./server/helpers/logger') |
feb4bdfd C |
21 | // Initialize database and models |
22 | const db = require('./server/initializers/database') | |
5804c0db | 23 | db.init(onDatabaseInitDone) |
00057e85 | 24 | |
69b0a27c C |
25 | // ----------- Checker ----------- |
26 | const checker = require('./server/initializers/checker') | |
27 | ||
b65c27aa C |
28 | const missed = checker.checkMissedConfig() |
29 | if (missed.length !== 0) { | |
30 | throw new Error('Miss some configurations keys : ' + missed) | |
31 | } | |
4e284e97 C |
32 | checker.checkFFmpeg(function (err) { |
33 | if (err) { | |
34 | throw err | |
35 | } | |
36 | }) | |
b65c27aa C |
37 | |
38 | const errorMessage = checker.checkConfig() | |
39 | if (errorMessage !== null) { | |
40 | throw new Error(errorMessage) | |
69b0a27c C |
41 | } |
42 | ||
00057e85 | 43 | // ----------- PeerTube modules ----------- |
5f698b82 | 44 | const customValidators = require('./server/helpers/custom-validators') |
c1a7ab7f | 45 | const friends = require('./server/lib/friends') |
00057e85 | 46 | const installer = require('./server/initializers/installer') |
00d6b0dd | 47 | const migrator = require('./server/initializers/migrator') |
227d02fe | 48 | const jobScheduler = require('./server/lib/jobs/job-scheduler') |
13ce1d01 | 49 | const routes = require('./server/controllers') |
a030a9b2 | 50 | |
a030a9b2 C |
51 | // ----------- Command line ----------- |
52 | ||
53 | // ----------- App ----------- | |
54 | ||
55 | // For the logger | |
56 | app.use(morgan('combined', { stream: logger.stream })) | |
57 | // For body requests | |
def16d33 | 58 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 C |
59 | app.use(bodyParser.urlencoded({ extended: false })) |
60 | // Validate some params for the API | |
61 | app.use(expressValidator({ | |
d57d6f26 C |
62 | customValidators: Object.assign( |
63 | {}, | |
64 | customValidators.misc, | |
65 | customValidators.pods, | |
66 | customValidators.users, | |
55fa55a9 C |
67 | customValidators.videos, |
68 | customValidators.remote.videos | |
d57d6f26 | 69 | ) |
a030a9b2 C |
70 | })) |
71 | ||
72 | // ----------- Views, routes and static files ----------- | |
73 | ||
79530164 | 74 | // API |
bc503c2a C |
75 | const apiRoute = '/api/' + constants.API_VERSION |
76 | app.use(apiRoute, routes.api) | |
052937db | 77 | |
79530164 C |
78 | // Client files |
79 | app.use('/', routes.client) | |
cbe2f7c3 | 80 | |
79530164 C |
81 | // Static files |
82 | app.use('/', routes.static) | |
6a94a109 | 83 | |
79530164 | 84 | // Always serve index client page (the client is a single page application, let it handle routing) |
6f4e2522 | 85 | app.use('/*', function (req, res, next) { |
830bcd0f | 86 | res.sendFile(path.join(__dirname, './client/dist/index.html')) |
6f4e2522 | 87 | }) |
a030a9b2 C |
88 | |
89 | // ----------- Tracker ----------- | |
90 | ||
13ce1d01 | 91 | const trackerServer = new TrackerServer({ |
a030a9b2 C |
92 | http: false, |
93 | udp: false, | |
94 | ws: false, | |
95 | dht: false | |
96 | }) | |
97 | ||
98 | trackerServer.on('error', function (err) { | |
99 | logger.error(err) | |
100 | }) | |
101 | ||
102 | trackerServer.on('warning', function (err) { | |
103 | logger.error(err) | |
104 | }) | |
105 | ||
13ce1d01 C |
106 | const server = http.createServer(app) |
107 | const wss = new WebSocketServer({server: server, path: '/tracker/socket'}) | |
a030a9b2 C |
108 | wss.on('connection', function (ws) { |
109 | trackerServer.onWebSocketConnection(ws) | |
110 | }) | |
111 | ||
112 | // ----------- Errors ----------- | |
113 | ||
114 | // Catch 404 and forward to error handler | |
115 | app.use(function (req, res, next) { | |
13ce1d01 | 116 | const err = new Error('Not Found') |
a030a9b2 C |
117 | err.status = 404 |
118 | next(err) | |
119 | }) | |
120 | ||
6f4e2522 C |
121 | app.use(function (err, req, res, next) { |
122 | logger.error(err) | |
123 | res.sendStatus(err.status || 500) | |
124 | }) | |
a030a9b2 | 125 | |
79530164 C |
126 | // ----------- Run ----------- |
127 | ||
5804c0db C |
128 | function onDatabaseInitDone () { |
129 | const port = constants.CONFIG.LISTEN.PORT | |
130 | // Run the migration scripts if needed | |
00d6b0dd C |
131 | migrator.migrate(function (err) { |
132 | if (err) throw err | |
a030a9b2 | 133 | |
5804c0db C |
134 | installer.installApplication(function (err) { |
135 | if (err) throw err | |
136 | ||
137 | // ----------- Make the server listening ----------- | |
138 | server.listen(port, function () { | |
139 | // Activate the communication with friends | |
140 | friends.activate() | |
052937db | 141 | |
227d02fe C |
142 | // Activate job scheduler |
143 | jobScheduler.activate() | |
144 | ||
5804c0db C |
145 | logger.info('Server listening on port %d', port) |
146 | logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL) | |
763381de | 147 | |
5804c0db C |
148 | app.emit('ready') |
149 | }) | |
8c308c2b C |
150 | }) |
151 | }) | |
5804c0db | 152 | } |
8c308c2b | 153 | |
a030a9b2 | 154 | module.exports = app |