]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | // ----------- Node modules ----------- | |
4 | const bodyParser = require('body-parser') | |
5 | const cors = require('cors') | |
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 | |
13 | ||
14 | // Create our main app | |
15 | const app = express() | |
16 | ||
17 | // ----------- Database ----------- | |
18 | const config = require('config') | |
19 | const constants = require('./server/initializers/constants') | |
20 | const database = require('./server/initializers/database') | |
21 | const logger = require('./server/helpers/logger') | |
22 | ||
23 | database.connect() | |
24 | ||
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 | ||
33 | // ----------- PeerTube modules ----------- | |
34 | const customValidators = require('./server/helpers/custom-validators') | |
35 | const installer = require('./server/initializers/installer') | |
36 | const migrator = require('./server/initializers/migrator') | |
37 | const mongoose = require('mongoose') | |
38 | const routes = require('./server/controllers') | |
39 | const Request = mongoose.model('Request') | |
40 | ||
41 | // Get configurations | |
42 | const port = config.get('listen.port') | |
43 | ||
44 | // ----------- Command line ----------- | |
45 | ||
46 | // ----------- App ----------- | |
47 | ||
48 | // For the logger | |
49 | app.use(morgan('combined', { stream: logger.stream })) | |
50 | // For body requests | |
51 | app.use(bodyParser.json({ limit: '500kb' })) | |
52 | app.use(bodyParser.urlencoded({ extended: false })) | |
53 | // Validate some params for the API | |
54 | app.use(expressValidator({ | |
55 | customValidators: Object.assign( | |
56 | {}, | |
57 | customValidators.misc, | |
58 | customValidators.pods, | |
59 | customValidators.users, | |
60 | customValidators.videos | |
61 | ) | |
62 | })) | |
63 | ||
64 | // ----------- Views, routes and static files ----------- | |
65 | ||
66 | // API routes | |
67 | const apiRoute = '/api/' + constants.API_VERSION | |
68 | app.use(apiRoute, routes.api) | |
69 | ||
70 | // Static files | |
71 | app.use('/client', express.static(path.join(__dirname, '/client/dist'), { maxAge: 0 })) | |
72 | // 404 for static files not found | |
73 | app.use('/client/*', function (req, res, next) { | |
74 | res.sendStatus(404) | |
75 | }) | |
76 | ||
77 | const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents')) | |
78 | app.use(constants.STATIC_PATHS.TORRENTS, cors(), express.static(torrentsPhysicalPath, { maxAge: '7d' })) | |
79 | ||
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: '7d' })) | |
83 | ||
84 | // Thumbnails path for express | |
85 | const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails')) | |
86 | app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: '7d' })) | |
87 | ||
88 | // Client application | |
89 | app.use('/*', function (req, res, next) { | |
90 | res.sendFile(path.join(__dirname, 'client/dist/index.html')) | |
91 | }) | |
92 | ||
93 | // ----------- Tracker ----------- | |
94 | ||
95 | const trackerServer = new TrackerServer({ | |
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 | ||
110 | const server = http.createServer(app) | |
111 | const wss = new WebSocketServer({server: server, path: '/tracker/socket'}) | |
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) { | |
120 | const err = new Error('Not Found') | |
121 | err.status = 404 | |
122 | next(err) | |
123 | }) | |
124 | ||
125 | app.use(function (err, req, res, next) { | |
126 | logger.error(err) | |
127 | res.sendStatus(err.status || 500) | |
128 | }) | |
129 | ||
130 | installer.installApplication(function (err) { | |
131 | if (err) throw err | |
132 | ||
133 | // Run the migration scripts if needed | |
134 | migrator.migrate(function (err) { | |
135 | if (err) throw err | |
136 | ||
137 | // ----------- Make the server listening ----------- | |
138 | server.listen(port, function () { | |
139 | // Activate the pool requests | |
140 | Request.activate() | |
141 | ||
142 | logger.info('Seeded all the videos') | |
143 | logger.info('Server listening on port %d', port) | |
144 | app.emit('ready') | |
145 | }) | |
146 | }) | |
147 | }) | |
148 | ||
149 | module.exports = app |