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