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