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