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