From 79530164b6c8f96a6ccf6d33b591d565f1575e67 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 Nov 2016 12:32:21 +0100 Subject: Server: move static/client routes in controllers/ --- server/controllers/client.js | 13 +++++++++++-- server/controllers/index.js | 4 +++- server/controllers/static.js | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 server/controllers/static.js (limited to 'server/controllers') diff --git a/server/controllers/client.js b/server/controllers/client.js index 61e094980..572db6133 100644 --- a/server/controllers/client.js +++ b/server/controllers/client.js @@ -13,8 +13,9 @@ const Video = mongoose.model('Video') const router = express.Router() const opengraphComment = '' -const embedPath = path.join(__dirname, '../../client/dist/standalone/videos/embed.html') -const indexPath = path.join(__dirname, '../../client/dist/index.html') +const distPath = path.join(__dirname, '../../client/dist') +const embedPath = path.join(distPath, 'standalone/videos/embed.html') +const indexPath = path.join(distPath, 'index.html') // Special route that add OpenGraph tags // Do not use a template engine for a so little thing @@ -24,6 +25,14 @@ router.use('/videos/embed', function (req, res, next) { res.sendFile(embedPath) }) +// Static HTML/CSS/JS client files +router.use('/client', express.static(distPath, { maxAge: constants.STATIC_MAX_AGE })) + +// 404 for static files not found +router.use('/client/*', function (req, res, next) { + res.sendStatus(404) +}) + // --------------------------------------------------------------------------- module.exports = router diff --git a/server/controllers/index.js b/server/controllers/index.js index 90b481e52..c9ca297ef 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -2,8 +2,10 @@ const apiController = require('./api/') const clientController = require('./client') +const staticController = require('./static') module.exports = { api: apiController, - client: clientController + client: clientController, + static: staticController } diff --git a/server/controllers/static.js b/server/controllers/static.js new file mode 100644 index 000000000..a91ff044d --- /dev/null +++ b/server/controllers/static.js @@ -0,0 +1,46 @@ +'use strict' + +const express = require('express') +const cors = require('cors') + +const constants = require('../initializers/constants') + +const router = express.Router() + +/* + Cors is very important to let other pods access torrent and video files +*/ + +const torrentsPhysicalPath = constants.CONFIG.STORAGE.TORRENTS_DIR +router.use( + constants.STATIC_PATHS.TORRENTS, + cors(), + express.static(torrentsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }) +) + +// Videos path for webseeding +const videosPhysicalPath = constants.CONFIG.STORAGE.VIDEOS_DIR +router.use( + constants.STATIC_PATHS.WEBSEED, + cors(), + express.static(videosPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }) +) + +// Thumbnails path for express +const thumbnailsPhysicalPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR +router.use( + constants.STATIC_PATHS.THUMBNAILS, + express.static(thumbnailsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }) +) + +// Video previews path for express +const previewsPhysicalPath = constants.CONFIG.STORAGE.PREVIEWS_DIR +router.use( + constants.STATIC_PATHS.PREVIEWS, + express.static(previewsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }) +) + +// --------------------------------------------------------------------------- + +module.exports = router + -- cgit v1.2.3