]>
Commit | Line | Data |
---|---|---|
a030a9b2 C |
1 | 'use strict' |
2 | ||
3 | // ----------- Node modules ----------- | |
13ce1d01 | 4 | const bodyParser = require('body-parser') |
b9ab2e25 | 5 | const cors = require('cors') |
13ce1d01 C |
6 | const express = require('express') |
7 | const expressValidator = require('express-validator') | |
8 | const http = require('http') | |
9 | const morgan = require('morgan') | |
10 | const path = require('path') | |
11 | const TrackerServer = require('bittorrent-tracker').Server | |
12 | const WebSocketServer = require('ws').Server | |
a030a9b2 | 13 | |
9f540774 C |
14 | process.title = 'peertube' |
15 | ||
a030a9b2 | 16 | // Create our main app |
13ce1d01 | 17 | const app = express() |
a030a9b2 | 18 | |
00057e85 | 19 | // ----------- Database ----------- |
13ce1d01 C |
20 | const config = require('config') |
21 | const constants = require('./server/initializers/constants') | |
13ce1d01 | 22 | const database = require('./server/initializers/database') |
13ce1d01 | 23 | const logger = require('./server/helpers/logger') |
00057e85 C |
24 | |
25 | database.connect() | |
26 | ||
69b0a27c C |
27 | // ----------- Checker ----------- |
28 | const checker = require('./server/initializers/checker') | |
29 | ||
30 | const miss = checker.checkConfig() | |
31 | if (miss.length !== 0) { | |
32 | throw new Error('Miss some configurations keys : ' + miss) | |
33 | } | |
34 | ||
00057e85 | 35 | // ----------- PeerTube modules ----------- |
5f698b82 | 36 | const customValidators = require('./server/helpers/custom-validators') |
00057e85 | 37 | const installer = require('./server/initializers/installer') |
00d6b0dd | 38 | const migrator = require('./server/initializers/migrator') |
00057e85 | 39 | const mongoose = require('mongoose') |
13ce1d01 | 40 | const routes = require('./server/controllers') |
00057e85 | 41 | const Request = mongoose.model('Request') |
a030a9b2 C |
42 | |
43 | // Get configurations | |
13ce1d01 | 44 | const port = config.get('listen.port') |
a030a9b2 | 45 | |
a030a9b2 C |
46 | // ----------- Command line ----------- |
47 | ||
48 | // ----------- App ----------- | |
49 | ||
50 | // For the logger | |
51 | app.use(morgan('combined', { stream: logger.stream })) | |
52 | // For body requests | |
def16d33 | 53 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 C |
54 | app.use(bodyParser.urlencoded({ extended: false })) |
55 | // Validate some params for the API | |
56 | app.use(expressValidator({ | |
d57d6f26 C |
57 | customValidators: Object.assign( |
58 | {}, | |
59 | customValidators.misc, | |
60 | customValidators.pods, | |
61 | customValidators.users, | |
62 | customValidators.videos | |
63 | ) | |
a030a9b2 C |
64 | })) |
65 | ||
66 | // ----------- Views, routes and static files ----------- | |
67 | ||
a030a9b2 | 68 | // API routes |
bc503c2a C |
69 | const apiRoute = '/api/' + constants.API_VERSION |
70 | app.use(apiRoute, routes.api) | |
a030a9b2 | 71 | |
bd324a66 | 72 | // Static files |
dc009132 | 73 | app.use('/client', express.static(path.join(__dirname, '/client/dist'), { maxAge: constants.STATIC_MAX_AGE })) |
bd324a66 | 74 | // 404 for static files not found |
41a2aee3 | 75 | app.use('/client/*', function (req, res, next) { |
bd324a66 C |
76 | res.sendStatus(404) |
77 | }) | |
78 | ||
052937db | 79 | const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents')) |
dc009132 | 80 | app.use(constants.STATIC_PATHS.TORRENTS, cors(), express.static(torrentsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })) |
052937db | 81 | |
b3d92510 C |
82 | // Videos path for webseeding |
83 | const videosPhysicalPath = path.join(__dirname, config.get('storage.videos')) | |
dc009132 | 84 | app.use(constants.STATIC_PATHS.WEBSEED, cors(), express.static(videosPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })) |
052937db | 85 | |
cbe2f7c3 | 86 | // Thumbnails path for express |
bc503c2a | 87 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) |
dc009132 | 88 | app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })) |
cbe2f7c3 | 89 | |
6f4e2522 C |
90 | // Client application |
91 | app.use('/*', function (req, res, next) { | |
4a6995be | 92 | res.sendFile(path.join(__dirname, 'client/dist/index.html')) |
6f4e2522 | 93 | }) |
a030a9b2 C |
94 | |
95 | // ----------- Tracker ----------- | |
96 | ||
13ce1d01 | 97 | const trackerServer = new TrackerServer({ |
a030a9b2 C |
98 | http: false, |
99 | udp: false, | |
100 | ws: false, | |
101 | dht: false | |
102 | }) | |
103 | ||
104 | trackerServer.on('error', function (err) { | |
105 | logger.error(err) | |
106 | }) | |
107 | ||
108 | trackerServer.on('warning', function (err) { | |
109 | logger.error(err) | |
110 | }) | |
111 | ||
13ce1d01 C |
112 | const server = http.createServer(app) |
113 | const wss = new WebSocketServer({server: server, path: '/tracker/socket'}) | |
a030a9b2 C |
114 | wss.on('connection', function (ws) { |
115 | trackerServer.onWebSocketConnection(ws) | |
116 | }) | |
117 | ||
118 | // ----------- Errors ----------- | |
119 | ||
120 | // Catch 404 and forward to error handler | |
121 | app.use(function (req, res, next) { | |
13ce1d01 | 122 | const err = new Error('Not Found') |
a030a9b2 C |
123 | err.status = 404 |
124 | next(err) | |
125 | }) | |
126 | ||
6f4e2522 C |
127 | app.use(function (err, req, res, next) { |
128 | logger.error(err) | |
129 | res.sendStatus(err.status || 500) | |
130 | }) | |
a030a9b2 | 131 | |
37dc07b2 | 132 | installer.installApplication(function (err) { |
9457bf88 | 133 | if (err) throw err |
9457bf88 | 134 | |
00d6b0dd C |
135 | // Run the migration scripts if needed |
136 | migrator.migrate(function (err) { | |
137 | if (err) throw err | |
a030a9b2 | 138 | |
a6375e69 C |
139 | // ----------- Make the server listening ----------- |
140 | server.listen(port, function () { | |
141 | // Activate the pool requests | |
142 | Request.activate() | |
052937db | 143 | |
a6375e69 C |
144 | logger.info('Server listening on port %d', port) |
145 | app.emit('ready') | |
8c308c2b C |
146 | }) |
147 | }) | |
a030a9b2 | 148 | }) |
8c308c2b | 149 | |
a030a9b2 | 150 | module.exports = app |