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