]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server.js
Server: move requests lib in their own directory
[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 12
9f540774
C
13process.title = 'peertube'
14
a030a9b2 15// Create our main app
13ce1d01 16const app = express()
a030a9b2 17
00057e85 18// ----------- Database -----------
13ce1d01 19const constants = require('./server/initializers/constants')
13ce1d01 20const logger = require('./server/helpers/logger')
feb4bdfd
C
21// Initialize database and models
22const db = require('./server/initializers/database')
5804c0db 23db.init(onDatabaseInitDone)
00057e85 24
69b0a27c
C
25// ----------- Checker -----------
26const checker = require('./server/initializers/checker')
27
b65c27aa
C
28const missed = checker.checkMissedConfig()
29if (missed.length !== 0) {
30 throw new Error('Miss some configurations keys : ' + missed)
31}
4e284e97
C
32checker.checkFFmpeg(function (err) {
33 if (err) {
34 throw err
35 }
36})
b65c27aa
C
37
38const errorMessage = checker.checkConfig()
39if (errorMessage !== null) {
40 throw new Error(errorMessage)
69b0a27c
C
41}
42
00057e85 43// ----------- PeerTube modules -----------
5f698b82 44const customValidators = require('./server/helpers/custom-validators')
c1a7ab7f 45const friends = require('./server/lib/friends')
00057e85 46const installer = require('./server/initializers/installer')
00d6b0dd 47const migrator = require('./server/initializers/migrator')
227d02fe 48const jobScheduler = require('./server/lib/jobs/job-scheduler')
13ce1d01 49const routes = require('./server/controllers')
a030a9b2 50
a030a9b2
C
51// ----------- Command line -----------
52
53// ----------- App -----------
54
55// For the logger
56app.use(morgan('combined', { stream: logger.stream }))
57// For body requests
def16d33 58app.use(bodyParser.json({ limit: '500kb' }))
a030a9b2
C
59app.use(bodyParser.urlencoded({ extended: false }))
60// Validate some params for the API
61app.use(expressValidator({
d57d6f26
C
62 customValidators: Object.assign(
63 {},
64 customValidators.misc,
65 customValidators.pods,
66 customValidators.users,
55fa55a9
C
67 customValidators.videos,
68 customValidators.remote.videos
d57d6f26 69 )
a030a9b2
C
70}))
71
72// ----------- Views, routes and static files -----------
73
79530164 74// API
bc503c2a
C
75const apiRoute = '/api/' + constants.API_VERSION
76app.use(apiRoute, routes.api)
052937db 77
79530164
C
78// Client files
79app.use('/', routes.client)
cbe2f7c3 80
79530164
C
81// Static files
82app.use('/', routes.static)
6a94a109 83
79530164 84// Always serve index client page (the client is a single page application, let it handle routing)
6f4e2522 85app.use('/*', function (req, res, next) {
830bcd0f 86 res.sendFile(path.join(__dirname, './client/dist/index.html'))
6f4e2522 87})
a030a9b2
C
88
89// ----------- Tracker -----------
90
13ce1d01 91const trackerServer = new TrackerServer({
a030a9b2
C
92 http: false,
93 udp: false,
94 ws: false,
95 dht: false
96})
97
98trackerServer.on('error', function (err) {
99 logger.error(err)
100})
101
102trackerServer.on('warning', function (err) {
103 logger.error(err)
104})
105
13ce1d01
C
106const server = http.createServer(app)
107const wss = new WebSocketServer({server: server, path: '/tracker/socket'})
a030a9b2
C
108wss.on('connection', function (ws) {
109 trackerServer.onWebSocketConnection(ws)
110})
111
112// ----------- Errors -----------
113
114// Catch 404 and forward to error handler
115app.use(function (req, res, next) {
13ce1d01 116 const err = new Error('Not Found')
a030a9b2
C
117 err.status = 404
118 next(err)
119})
120
6f4e2522
C
121app.use(function (err, req, res, next) {
122 logger.error(err)
123 res.sendStatus(err.status || 500)
124})
a030a9b2 125
79530164
C
126// ----------- Run -----------
127
5804c0db
C
128function onDatabaseInitDone () {
129 const port = constants.CONFIG.LISTEN.PORT
130 // Run the migration scripts if needed
00d6b0dd
C
131 migrator.migrate(function (err) {
132 if (err) throw err
a030a9b2 133
5804c0db
C
134 installer.installApplication(function (err) {
135 if (err) throw err
136
137 // ----------- Make the server listening -----------
138 server.listen(port, function () {
139 // Activate the communication with friends
140 friends.activate()
052937db 141
227d02fe
C
142 // Activate job scheduler
143 jobScheduler.activate()
144
5804c0db
C
145 logger.info('Server listening on port %d', port)
146 logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL)
763381de 147
5804c0db
C
148 app.emit('ready')
149 })
8c308c2b
C
150 })
151 })
5804c0db 152}
8c308c2b 153
a030a9b2 154module.exports = app