]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server.js
Fix travis
[github/Chocobozzz/PeerTube.git] / server.js
CommitLineData
a030a9b2
C
1'use strict'
2
3// ----------- Node modules -----------
13ce1d01
C
4const bodyParser = require('body-parser')
5const express = require('express')
6const expressValidator = require('express-validator')
7const http = require('http')
8const morgan = require('morgan')
9const path = require('path')
10const TrackerServer = require('bittorrent-tracker').Server
11const WebSocketServer = require('ws').Server
a030a9b2
C
12
13// Create our main app
13ce1d01 14const app = express()
a030a9b2 15
00057e85 16// ----------- Database -----------
13ce1d01
C
17const config = require('config')
18const constants = require('./server/initializers/constants')
13ce1d01 19const database = require('./server/initializers/database')
13ce1d01 20const logger = require('./server/helpers/logger')
00057e85
C
21
22database.connect()
23
69b0a27c
C
24// ----------- Checker -----------
25const checker = require('./server/initializers/checker')
26
27const miss = checker.checkConfig()
28if (miss.length !== 0) {
29 throw new Error('Miss some configurations keys : ' + miss)
30}
31
00057e85 32// ----------- PeerTube modules -----------
5f698b82 33const customValidators = require('./server/helpers/custom-validators')
00057e85 34const installer = require('./server/initializers/installer')
00d6b0dd 35const migrator = require('./server/initializers/migrator')
00057e85 36const mongoose = require('mongoose')
13ce1d01 37const routes = require('./server/controllers')
00057e85 38const Request = mongoose.model('Request')
a030a9b2
C
39
40// Get configurations
13ce1d01 41const port = config.get('listen.port')
a030a9b2 42
a030a9b2
C
43// ----------- Command line -----------
44
45// ----------- App -----------
46
47// For the logger
48app.use(morgan('combined', { stream: logger.stream }))
49// For body requests
def16d33 50app.use(bodyParser.json({ limit: '500kb' }))
a030a9b2
C
51app.use(bodyParser.urlencoded({ extended: false }))
52// Validate some params for the API
53app.use(expressValidator({
d57d6f26
C
54 customValidators: Object.assign(
55 {},
56 customValidators.misc,
57 customValidators.pods,
58 customValidators.users,
59 customValidators.videos
60 )
a030a9b2
C
61}))
62
63// ----------- Views, routes and static files -----------
64
a030a9b2 65// API routes
bc503c2a
C
66const apiRoute = '/api/' + constants.API_VERSION
67app.use(apiRoute, routes.api)
a030a9b2 68
bd324a66 69// Static files
4a6995be 70app.use('/client', express.static(path.join(__dirname, '/client/dist'), { maxAge: 0 }))
bd324a66 71// 404 for static files not found
41a2aee3 72app.use('/client/*', function (req, res, next) {
bd324a66
C
73 res.sendStatus(404)
74})
75
052937db
C
76const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents'))
77app.use(constants.STATIC_PATHS.TORRENTS, express.static(torrentsPhysicalPath, { maxAge: 0 }))
78
79// Uploads path for webseeding
80const uploadsPhysicalPath = path.join(__dirname, config.get('storage.uploads'))
81app.use(constants.STATIC_PATHS.WEBSEED, express.static(uploadsPhysicalPath, { maxAge: 0 }))
82
cbe2f7c3 83// Thumbnails path for express
bc503c2a 84const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails'))
052937db 85app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: 0 }))
cbe2f7c3 86
6f4e2522
C
87// Client application
88app.use('/*', function (req, res, next) {
4a6995be 89 res.sendFile(path.join(__dirname, 'client/dist/index.html'))
6f4e2522 90})
a030a9b2
C
91
92// ----------- Tracker -----------
93
13ce1d01 94const trackerServer = new TrackerServer({
a030a9b2
C
95 http: false,
96 udp: false,
97 ws: false,
98 dht: false
99})
100
101trackerServer.on('error', function (err) {
102 logger.error(err)
103})
104
105trackerServer.on('warning', function (err) {
106 logger.error(err)
107})
108
13ce1d01
C
109const server = http.createServer(app)
110const wss = new WebSocketServer({server: server, path: '/tracker/socket'})
a030a9b2
C
111wss.on('connection', function (ws) {
112 trackerServer.onWebSocketConnection(ws)
113})
114
115// ----------- Errors -----------
116
117// Catch 404 and forward to error handler
118app.use(function (req, res, next) {
13ce1d01 119 const err = new Error('Not Found')
a030a9b2
C
120 err.status = 404
121 next(err)
122})
123
6f4e2522
C
124app.use(function (err, req, res, next) {
125 logger.error(err)
126 res.sendStatus(err.status || 500)
127})
a030a9b2 128
37dc07b2 129installer.installApplication(function (err) {
9457bf88 130 if (err) throw err
9457bf88 131
00d6b0dd
C
132 // Run the migration scripts if needed
133 migrator.migrate(function (err) {
134 if (err) throw err
a030a9b2 135
a6375e69
C
136 // ----------- Make the server listening -----------
137 server.listen(port, function () {
138 // Activate the pool requests
139 Request.activate()
052937db 140
a6375e69
C
141 logger.info('Seeded all the videos')
142 logger.info('Server listening on port %d', port)
143 app.emit('ready')
8c308c2b
C
144 })
145 })
a030a9b2 146})
8c308c2b 147
a030a9b2 148module.exports = app