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