]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
e5bb5d1a4478f1b750950c1d8e09651a5e1f5df6
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 constants
= require('./server/initializers/constants')
21 const database
= require('./server/initializers/database')
22 const logger
= require('./server/helpers/logger')
26 // ----------- Checker -----------
27 const checker
= require('./server/initializers/checker')
29 const miss
= checker
.checkConfig()
30 if (miss
.length
!== 0) {
31 throw new Error('Miss some configurations keys : ' + miss
)
34 // ----------- PeerTube modules -----------
35 const customValidators
= require('./server/helpers/custom-validators')
36 const installer
= require('./server/initializers/installer')
37 const migrator
= require('./server/initializers/migrator')
38 const mongoose
= require('mongoose')
39 const routes
= require('./server/controllers')
40 const Request
= mongoose
.model('Request')
42 // ----------- Command line -----------
44 // ----------- App -----------
47 app
.use(morgan('combined', { stream: logger
.stream
}))
49 app
.use(bodyParser
.json({ limit: '500kb' }))
50 app
.use(bodyParser
.urlencoded({ extended: false }))
51 // Validate some params for the API
52 app
.use(expressValidator({
53 customValidators: Object
.assign(
55 customValidators
.misc
,
56 customValidators
.pods
,
57 customValidators
.users
,
58 customValidators
.videos
62 // ----------- Views, routes and static files -----------
65 const apiRoute
= '/api/' + constants
.API_VERSION
66 app
.use(apiRoute
, routes
.api
)
69 app
.use('/client', express
.static(path
.join(__dirname
, '/client/dist'), { maxAge: constants
.STATIC_MAX_AGE
}))
70 // 404 for static files not found
71 app
.use('/client/*', function (req
, res
, next
) {
75 const torrentsPhysicalPath
= path
.join(__dirname
, constants
.CONFIG
.STORAGE
.TORRENTS_DIR
)
76 app
.use(constants
.STATIC_PATHS
.TORRENTS
, cors(), express
.static(torrentsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
78 // Videos path for webseeding
79 const videosPhysicalPath
= path
.join(__dirname
, constants
.CONFIG
.STORAGE
.VIDEOS_DIR
)
80 app
.use(constants
.STATIC_PATHS
.WEBSEED
, cors(), express
.static(videosPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
82 // Thumbnails path for express
83 const thumbnailsPhysicalPath
= path
.join(__dirname
, constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
)
84 app
.use(constants
.STATIC_PATHS
.THUMBNAILS
, express
.static(thumbnailsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
87 app
.use('/*', function (req
, res
, next
) {
88 res
.sendFile(path
.join(__dirname
, 'client/dist/index.html'))
91 // ----------- Tracker -----------
93 const trackerServer
= new TrackerServer({
100 trackerServer
.on('error', function (err
) {
104 trackerServer
.on('warning', function (err
) {
108 const server
= http
.createServer(app
)
109 const wss
= new WebSocketServer({server: server
, path: '/tracker/socket'})
110 wss
.on('connection', function (ws
) {
111 trackerServer
.onWebSocketConnection(ws
)
114 // ----------- Errors -----------
116 // Catch 404 and forward to error handler
117 app
.use(function (req
, res
, next
) {
118 const err
= new Error('Not Found')
123 app
.use(function (err
, req
, res
, next
) {
125 res
.sendStatus(err
.status
|| 500)
128 const port
= constants
.CONFIG
.LISTEN
.PORT
129 installer
.installApplication(function (err
) {
132 // Run the migration scripts if needed
133 migrator
.migrate(function (err
) {
136 // ----------- Make the server listening -----------
137 server
.listen(port
, function () {
138 // Activate the pool requests
141 logger
.info('Server listening on port %d', port
)