]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server.js
Server: add cache to static files
[github/Chocobozzz/PeerTube.git] / server.js
index 9322bca19e500038fced0335f8a644a09c786248..f24b25f4d96fc1501c38349563ef6d56fe75bcd1 100644 (file)
--- a/server.js
+++ b/server.js
@@ -2,6 +2,7 @@
 
 // ----------- Node modules -----------
 const bodyParser = require('body-parser')
+const cors = require('cors')
 const express = require('express')
 const expressValidator = require('express-validator')
 const http = require('http')
@@ -13,14 +14,6 @@ const WebSocketServer = require('ws').Server
 // Create our main app
 const app = express()
 
-// ----------- Checker -----------
-const checker = require('./server/initializers/checker')
-
-const miss = checker.checkConfig()
-if (miss.length !== 0) {
-  throw new Error('Miss some configurations keys : ' + miss)
-}
-
 // ----------- Database -----------
 const config = require('config')
 const constants = require('./server/initializers/constants')
@@ -29,15 +22,21 @@ const logger = require('./server/helpers/logger')
 
 database.connect()
 
+// ----------- Checker -----------
+const checker = require('./server/initializers/checker')
+
+const miss = checker.checkConfig()
+if (miss.length !== 0) {
+  throw new Error('Miss some configurations keys : ' + miss)
+}
+
 // ----------- PeerTube modules -----------
-const customValidators = require('./server/helpers/customValidators')
+const customValidators = require('./server/helpers/custom-validators')
 const installer = require('./server/initializers/installer')
+const migrator = require('./server/initializers/migrator')
 const mongoose = require('mongoose')
 const routes = require('./server/controllers')
-const utils = require('./server/helpers/utils')
-const webtorrent = require('./server/lib/webtorrent')
 const Request = mongoose.model('Request')
-const Video = mongoose.model('Video')
 
 // Get configurations
 const port = config.get('listen.port')
@@ -49,18 +48,21 @@ const port = config.get('listen.port')
 // For the logger
 app.use(morgan('combined', { stream: logger.stream }))
 // For body requests
-app.use(bodyParser.json())
+app.use(bodyParser.json({ limit: '500kb' }))
 app.use(bodyParser.urlencoded({ extended: false }))
 // Validate some params for the API
 app.use(expressValidator({
-  customValidators: customValidators
+  customValidators: Object.assign(
+    {},
+    customValidators.misc,
+    customValidators.pods,
+    customValidators.users,
+    customValidators.videos
+  )
 }))
 
 // ----------- Views, routes and static files -----------
 
-// Catch sefaults
-require('segfault-handler').registerHandler()
-
 // API routes
 const apiRoute = '/api/' + constants.API_VERSION
 app.use(apiRoute, routes.api)
@@ -72,9 +74,16 @@ app.use('/client/*', function (req, res, next) {
   res.sendStatus(404)
 })
 
+const torrentsPhysicalPath = path.join(__dirname, config.get('storage.torrents'))
+app.use(constants.STATIC_PATHS.TORRENTS, cors(), express.static(torrentsPhysicalPath, { maxAge: '7d' }))
+
+// Videos path for webseeding
+const videosPhysicalPath = path.join(__dirname, config.get('storage.videos'))
+app.use(constants.STATIC_PATHS.WEBSEED, cors(), express.static(videosPhysicalPath, { maxAge: '7d' }))
+
 // Thumbnails path for express
 const thumbnailsPhysicalPath = path.join(__dirname, config.get('storage.thumbnails'))
-app.use(constants.THUMBNAILS_STATIC_PATH, express.static(thumbnailsPhysicalPath, { maxAge: 0 }))
+app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: '7d' }))
 
 // Client application
 app.use('/*', function (req, res, next) {
@@ -121,32 +130,18 @@ app.use(function (err, req, res, next) {
 installer.installApplication(function (err) {
   if (err) throw err
 
-  // Create/activate the webtorrent module
-  webtorrent.create(function () {
-    function cleanForExit () {
-      utils.cleanForExit(webtorrent.app)
-    }
-
-    function exitGracefullyOnSignal () {
-      process.exit(-1)
-    }
-
-    process.on('exit', cleanForExit)
-    process.on('SIGINT', exitGracefullyOnSignal)
-    process.on('SIGTERM', exitGracefullyOnSignal)
+  // Run the migration scripts if needed
+  migrator.migrate(function (err) {
+    if (err) throw err
 
     // ----------- Make the server listening -----------
     server.listen(port, function () {
       // Activate the pool requests
       Request.activate()
 
-      Video.seedAllExisting(function (err) {
-        if (err) throw err
-
-        logger.info('Seeded all the videos')
-        logger.info('Server listening on port %d', port)
-        app.emit('ready')
-      })
+      logger.info('Seeded all the videos')
+      logger.info('Server listening on port %d', port)
+      app.emit('ready')
     })
   })
 })