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