]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
3 // ----------- Node modules -----------
4 const bodyParser
= require('body-parser')
5 const express
= require('express')
6 const expressValidator
= require('express-validator')
7 const http
= require('http')
8 const morgan
= require('morgan')
9 const path
= require('path')
10 const TrackerServer
= require('bittorrent-tracker').Server
11 const WebSocketServer
= require('ws').Server
13 // Create our main app
16 // ----------- Database -----------
17 const config
= require('config')
18 const constants
= require('./server/initializers/constants')
19 const database
= require('./server/initializers/database')
20 const logger
= require('./server/helpers/logger')
24 // ----------- Checker -----------
25 const checker
= require('./server/initializers/checker')
27 const miss
= checker
.checkConfig()
28 if (miss
.length
!== 0) {
29 throw new Error('Miss some configurations keys : ' + miss
)
32 // ----------- PeerTube modules -----------
33 const customValidators
= require('./server/helpers/custom-validators')
34 const installer
= require('./server/initializers/installer')
35 const migrator
= require('./server/initializers/migrator')
36 const mongoose
= require('mongoose')
37 const routes
= require('./server/controllers')
38 const Request
= mongoose
.model('Request')
41 const port
= config
.get('listen.port')
43 // ----------- Command line -----------
45 // ----------- App -----------
48 app
.use(morgan('combined', { stream: logger
.stream
}))
50 app
.use(bodyParser
.json({ limit: '500kb' }))
51 app
.use(bodyParser
.urlencoded({ extended: false }))
52 // Validate some params for the API
53 app
.use(expressValidator({
54 customValidators: Object
.assign(
56 customValidators
.misc
,
57 customValidators
.pods
,
58 customValidators
.users
,
59 customValidators
.videos
63 // ----------- Views, routes and static files -----------
66 const apiRoute
= '/api/' + constants
.API_VERSION
67 app
.use(apiRoute
, routes
.api
)
70 app
.use('/client', express
.static(path
.join(__dirname
, '/client/dist'), { maxAge: 0 }))
71 // 404 for static files not found
72 app
.use('/client/*', function (req
, res
, next
) {
76 const torrentsPhysicalPath
= path
.join(__dirname
, config
.get('storage.torrents'))
77 app
.use(constants
.STATIC_PATHS
.TORRENTS
, express
.static(torrentsPhysicalPath
, { maxAge: 0 }))
79 // Uploads path for webseeding
80 const uploadsPhysicalPath
= path
.join(__dirname
, config
.get('storage.uploads'))
81 app
.use(constants
.STATIC_PATHS
.WEBSEED
, express
.static(uploadsPhysicalPath
, { maxAge: 0 }))
83 // Thumbnails path for express
84 const thumbnailsPhysicalPath
= path
.join(__dirname
, config
.get('storage.thumbnails'))
85 app
.use(constants
.STATIC_PATHS
.THUMBNAILS
, express
.static(thumbnailsPhysicalPath
, { maxAge: 0 }))
88 app
.use('/*', function (req
, res
, next
) {
89 res
.sendFile(path
.join(__dirname
, 'client/dist/index.html'))
92 // ----------- Tracker -----------
94 const trackerServer
= new TrackerServer({
101 trackerServer
.on('error', function (err
) {
105 trackerServer
.on('warning', function (err
) {
109 const server
= http
.createServer(app
)
110 const wss
= new WebSocketServer({server: server
, path: '/tracker/socket'})
111 wss
.on('connection', function (ws
) {
112 trackerServer
.onWebSocketConnection(ws
)
115 // ----------- Errors -----------
117 // Catch 404 and forward to error handler
118 app
.use(function (req
, res
, next
) {
119 const err
= new Error('Not Found')
124 app
.use(function (err
, req
, res
, next
) {
126 res
.sendStatus(err
.status
|| 500)
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('Seeded all the videos')
142 logger
.info('Server listening on port %d', port
)