]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server.js
Prerender opengraph tags in the server
[github/Chocobozzz/PeerTube.git] / server.js
1 'use strict'
2
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
13
14 process.title = 'peertube'
15
16 // Create our main app
17 const app = express()
18
19 // ----------- Database -----------
20 const constants = require('./server/initializers/constants')
21 const database = require('./server/initializers/database')
22 const logger = require('./server/helpers/logger')
23
24 database.connect()
25
26 // ----------- Checker -----------
27 const checker = require('./server/initializers/checker')
28
29 const missed = checker.checkMissedConfig()
30 if (missed.length !== 0) {
31 throw new Error('Miss some configurations keys : ' + missed)
32 }
33
34 const errorMessage = checker.checkConfig()
35 if (errorMessage !== null) {
36 throw new Error(errorMessage)
37 }
38
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')
46
47 // ----------- Command line -----------
48
49 // ----------- App -----------
50
51 // For the logger
52 app.use(morgan('combined', { stream: logger.stream }))
53 // For body requests
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(
59 {},
60 customValidators.misc,
61 customValidators.pods,
62 customValidators.users,
63 customValidators.videos
64 )
65 }))
66
67 // ----------- Views, routes and static files -----------
68
69 // API routes
70 const apiRoute = '/api/' + constants.API_VERSION
71 app.use(apiRoute, routes.api)
72 app.use('/', routes.client)
73
74 // Static files
75 app.use('/client', express.static(path.join(__dirname, '/client/dist'), { maxAge: constants.STATIC_MAX_AGE }))
76 // 404 for static files not found
77 app.use('/client/*', function (req, res, next) {
78 res.sendStatus(404)
79 })
80
81 const torrentsPhysicalPath = constants.CONFIG.STORAGE.TORRENTS_DIR
82 app.use(constants.STATIC_PATHS.TORRENTS, cors(), express.static(torrentsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
83
84 // Videos path for webseeding
85 const videosPhysicalPath = constants.CONFIG.STORAGE.VIDEOS_DIR
86 app.use(constants.STATIC_PATHS.WEBSEED, cors(), express.static(videosPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
87
88 // Thumbnails path for express
89 const thumbnailsPhysicalPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR
90 app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
91
92 // Always serve index client page
93 app.use('/*', function (req, res, next) {
94 res.sendFile(path.join(__dirname, './client/dist/index.html'))
95 })
96
97 // ----------- Tracker -----------
98
99 const trackerServer = new TrackerServer({
100 http: false,
101 udp: false,
102 ws: false,
103 dht: false
104 })
105
106 trackerServer.on('error', function (err) {
107 logger.error(err)
108 })
109
110 trackerServer.on('warning', function (err) {
111 logger.error(err)
112 })
113
114 const server = http.createServer(app)
115 const wss = new WebSocketServer({server: server, path: '/tracker/socket'})
116 wss.on('connection', function (ws) {
117 trackerServer.onWebSocketConnection(ws)
118 })
119
120 // ----------- Errors -----------
121
122 // Catch 404 and forward to error handler
123 app.use(function (req, res, next) {
124 const err = new Error('Not Found')
125 err.status = 404
126 next(err)
127 })
128
129 app.use(function (err, req, res, next) {
130 logger.error(err)
131 res.sendStatus(err.status || 500)
132 })
133
134 const port = constants.CONFIG.LISTEN.PORT
135 installer.installApplication(function (err) {
136 if (err) throw err
137
138 // Run the migration scripts if needed
139 migrator.migrate(function (err) {
140 if (err) throw err
141
142 // ----------- Make the server listening -----------
143 server.listen(port, function () {
144 // Activate the pool requests
145 Request.activate()
146
147 logger.info('Server listening on port %d', port)
148 logger.info('Webserver: %s', constants.CONFIG.WEBSERVER.URL)
149
150 app.emit('ready')
151 })
152 })
153 })
154
155 module.exports = app