]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
28ad4fc0893055d70610ced6d17ec93106bd1203
3 // ----------- Node modules -----------
4 const bodyParser
= require('body-parser')
5 const cors
= require('cors')
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
14 process
.title
= 'peertube'
16 // Create our main app
19 // ----------- Database -----------
20 const config
= require('config')
21 const constants
= require('./server/initializers/constants')
22 const database
= require('./server/initializers/database')
23 const logger
= require('./server/helpers/logger')
27 // ----------- Checker -----------
28 const checker
= require('./server/initializers/checker')
30 const miss
= checker
.checkConfig()
31 if (miss
.length
!== 0) {
32 throw new Error('Miss some configurations keys : ' + miss
)
35 // ----------- PeerTube modules -----------
36 const customValidators
= require('./server/helpers/custom-validators')
37 const installer
= require('./server/initializers/installer')
38 const migrator
= require('./server/initializers/migrator')
39 const mongoose
= require('mongoose')
40 const routes
= require('./server/controllers')
41 const Request
= mongoose
.model('Request')
44 const port
= config
.get('listen.port')
46 // ----------- Command line -----------
48 // ----------- App -----------
51 app
.use(morgan('combined', { stream: logger
.stream
}))
53 app
.use(bodyParser
.json({ limit: '500kb' }))
54 app
.use(bodyParser
.urlencoded({ extended: false }))
55 // Validate some params for the API
56 app
.use(expressValidator({
57 customValidators: Object
.assign(
59 customValidators
.misc
,
60 customValidators
.pods
,
61 customValidators
.users
,
62 customValidators
.videos
66 // ----------- Views, routes and static files -----------
69 const apiRoute
= '/api/' + constants
.API_VERSION
70 app
.use(apiRoute
, routes
.api
)
73 app
.use('/client', express
.static(path
.join(__dirname
, '/client/dist'), { maxAge: constants
.STATIC_MAX_AGE
}))
74 // 404 for static files not found
75 app
.use('/client/*', function (req
, res
, next
) {
79 const torrentsPhysicalPath
= path
.join(__dirname
, config
.get('storage.torrents'))
80 app
.use(constants
.STATIC_PATHS
.TORRENTS
, cors(), express
.static(torrentsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
82 // Videos path for webseeding
83 const videosPhysicalPath
= path
.join(__dirname
, config
.get('storage.videos'))
84 app
.use(constants
.STATIC_PATHS
.WEBSEED
, cors(), express
.static(videosPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
86 // Thumbnails path for express
87 const thumbnailsPhysicalPath
= path
.join(__dirname
, config
.get('storage.thumbnails'))
88 app
.use(constants
.STATIC_PATHS
.THUMBNAILS
, express
.static(thumbnailsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
91 app
.use('/*', function (req
, res
, next
) {
92 res
.sendFile(path
.join(__dirname
, 'client/dist/index.html'))
95 // ----------- Tracker -----------
97 const trackerServer
= new TrackerServer({
104 trackerServer
.on('error', function (err
) {
108 trackerServer
.on('warning', function (err
) {
112 const server
= http
.createServer(app
)
113 const wss
= new WebSocketServer({server: server
, path: '/tracker/socket'})
114 wss
.on('connection', function (ws
) {
115 trackerServer
.onWebSocketConnection(ws
)
118 // ----------- Errors -----------
120 // Catch 404 and forward to error handler
121 app
.use(function (req
, res
, next
) {
122 const err
= new Error('Not Found')
127 app
.use(function (err
, req
, res
, next
) {
129 res
.sendStatus(err
.status
|| 500)
132 installer
.installApplication(function (err
) {
135 // Run the migration scripts if needed
136 migrator
.migrate(function (err
) {
139 // ----------- Make the server listening -----------
140 server
.listen(port
, function () {
141 // Activate the pool requests
144 logger
.info('Server listening on port %d', port
)