]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
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 missed
= checker
.checkMissedConfig()
30 if (missed
.length
!== 0) {
31 throw new Error('Miss some configurations keys : ' + missed
)
34 const errorMessage
= checker
.checkConfig()
35 if (errorMessage
!== null) {
36 throw new Error(errorMessage
)
39 // ----------- PeerTube modules -----------
40 const customValidators
= require('./server/helpers/custom-validators')
41 const installer
= require('./server/initializers/installer')
42 const migrator
= require('./server/initializers/migrator')
43 const mongoose
= require('mongoose')
44 const routes
= require('./server/controllers')
45 const Request
= mongoose
.model('Request')
47 // ----------- Command line -----------
49 // ----------- App -----------
52 app
.use(morgan('combined', { stream: logger
.stream
}))
54 app
.use(bodyParser
.json({ limit: '500kb' }))
55 app
.use(bodyParser
.urlencoded({ extended: false }))
56 // Validate some params for the API
57 app
.use(expressValidator({
58 customValidators: Object
.assign(
60 customValidators
.misc
,
61 customValidators
.pods
,
62 customValidators
.users
,
63 customValidators
.videos
67 // ----------- Views, routes and static files -----------
70 const apiRoute
= '/api/' + constants
.API_VERSION
71 app
.use(apiRoute
, routes
.api
)
72 app
.use('/', routes
.client
)
74 // Static client files
75 // TODO: move in client
76 app
.use('/client', express
.static(path
.join(__dirname
, '/client/dist'), { maxAge: constants
.STATIC_MAX_AGE
}))
77 // 404 for static files not found
78 app
.use('/client/*', function (req
, res
, next
) {
82 const torrentsPhysicalPath
= constants
.CONFIG
.STORAGE
.TORRENTS_DIR
83 app
.use(constants
.STATIC_PATHS
.TORRENTS
, cors(), express
.static(torrentsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
85 // Videos path for webseeding
86 const videosPhysicalPath
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
87 app
.use(constants
.STATIC_PATHS
.WEBSEED
, cors(), express
.static(videosPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
89 // Thumbnails path for express
90 const thumbnailsPhysicalPath
= constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
91 app
.use(constants
.STATIC_PATHS
.THUMBNAILS
, express
.static(thumbnailsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
93 // Video previews path for express
94 const previewsPhysicalPath
= constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
95 app
.use(constants
.STATIC_PATHS
.PREVIEWS
, express
.static(previewsPhysicalPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
97 // Always serve index client page
98 app
.use('/*', function (req
, res
, next
) {
99 res
.sendFile(path
.join(__dirname
, './client/dist/index.html'))
102 // ----------- Tracker -----------
104 const trackerServer
= new TrackerServer({
111 trackerServer
.on('error', function (err
) {
115 trackerServer
.on('warning', function (err
) {
119 const server
= http
.createServer(app
)
120 const wss
= new WebSocketServer({server: server
, path: '/tracker/socket'})
121 wss
.on('connection', function (ws
) {
122 trackerServer
.onWebSocketConnection(ws
)
125 // ----------- Errors -----------
127 // Catch 404 and forward to error handler
128 app
.use(function (req
, res
, next
) {
129 const err
= new Error('Not Found')
134 app
.use(function (err
, req
, res
, next
) {
136 res
.sendStatus(err
.status
|| 500)
139 const port
= constants
.CONFIG
.LISTEN
.PORT
140 installer
.installApplication(function (err
) {
143 // Run the migration scripts if needed
144 migrator
.migrate(function (err
) {
147 // ----------- Make the server listening -----------
148 server
.listen(port
, function () {
149 // Activate the pool requests
152 logger
.info('Server listening on port %d', port
)
153 logger
.info('Webserver: %s', constants
.CONFIG
.WEBSERVER
.URL
)