]>
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 // Create our main app
17 // ----------- Database -----------
18 const config
= require('config')
19 const constants
= require('./server/initializers/constants')
20 const database
= require('./server/initializers/database')
21 const logger
= require('./server/helpers/logger')
25 // ----------- Checker -----------
26 const checker
= require('./server/initializers/checker')
28 const miss
= checker
.checkConfig()
29 if (miss
.length
!== 0) {
30 throw new Error('Miss some configurations keys : ' + miss
)
33 // ----------- PeerTube modules -----------
34 const customValidators
= require('./server/helpers/custom-validators')
35 const installer
= require('./server/initializers/installer')
36 const migrator
= require('./server/initializers/migrator')
37 const mongoose
= require('mongoose')
38 const routes
= require('./server/controllers')
39 const Request
= mongoose
.model('Request')
42 const port
= config
.get('listen.port')
44 // ----------- Command line -----------
46 // ----------- App -----------
49 app
.use(morgan('combined', { stream: logger
.stream
}))
51 app
.use(bodyParser
.json({ limit: '500kb' }))
52 app
.use(bodyParser
.urlencoded({ extended: false }))
53 // Validate some params for the API
54 app
.use(expressValidator({
55 customValidators: Object
.assign(
57 customValidators
.misc
,
58 customValidators
.pods
,
59 customValidators
.users
,
60 customValidators
.videos
64 // ----------- Views, routes and static files -----------
67 const apiRoute
= '/api/' + constants
.API_VERSION
68 app
.use(apiRoute
, routes
.api
)
71 app
.use('/client', express
.static(path
.join(__dirname
, '/client/dist'), { maxAge: 0 }))
72 // 404 for static files not found
73 app
.use('/client/*', function (req
, res
, next
) {
77 const torrentsPhysicalPath
= path
.join(__dirname
, config
.get('storage.torrents'))
78 app
.use(constants
.STATIC_PATHS
.TORRENTS
, cors(), express
.static(torrentsPhysicalPath
, { maxAge: 0 }))
80 // Uploads path for webseeding
81 const uploadsPhysicalPath
= path
.join(__dirname
, config
.get('storage.uploads'))
82 app
.use(constants
.STATIC_PATHS
.WEBSEED
, cors(), express
.static(uploadsPhysicalPath
, { maxAge: 0 }))
84 // Thumbnails path for express
85 const thumbnailsPhysicalPath
= path
.join(__dirname
, config
.get('storage.thumbnails'))
86 app
.use(constants
.STATIC_PATHS
.THUMBNAILS
, express
.static(thumbnailsPhysicalPath
, { maxAge: 0 }))
89 app
.use('/*', function (req
, res
, next
) {
90 res
.sendFile(path
.join(__dirname
, 'client/dist/index.html'))
93 // ----------- Tracker -----------
95 const trackerServer
= new TrackerServer({
102 trackerServer
.on('error', function (err
) {
106 trackerServer
.on('warning', function (err
) {
110 const server
= http
.createServer(app
)
111 const wss
= new WebSocketServer({server: server
, path: '/tracker/socket'})
112 wss
.on('connection', function (ws
) {
113 trackerServer
.onWebSocketConnection(ws
)
116 // ----------- Errors -----------
118 // Catch 404 and forward to error handler
119 app
.use(function (req
, res
, next
) {
120 const err
= new Error('Not Found')
125 app
.use(function (err
, req
, res
, next
) {
127 res
.sendStatus(err
.status
|| 500)
130 installer
.installApplication(function (err
) {
133 // Run the migration scripts if needed
134 migrator
.migrate(function (err
) {
137 // ----------- Make the server listening -----------
138 server
.listen(port
, function () {
139 // Activate the pool requests
142 logger
.info('Seeded all the videos')
143 logger
.info('Server listening on port %d', port
)