Split misc middleware

This commit is contained in:
Chocobozzz 2016-02-04 21:16:27 +01:00
parent c173e56520
commit a5fa04b0cc
7 changed files with 52 additions and 39 deletions

View File

@ -7,21 +7,22 @@
var logger = require('../../../helpers/logger')
var friends = require('../../../lib/friends')
var middleware = require('../../../middlewares')
var miscMiddleware = middleware.misc
var cacheMiddleware = middleware.cache
var Pods = require('../../../models/pods')
var reqValidator = middleware.reqValidators.pods
var secureMiddleware = middleware.secure
var secureRequest = middleware.reqValidators.remote.secureRequest
var utils = require('../../../helpers/utils')
var Videos = require('../../../models/videos')
var router = express.Router()
router.get('/', miscMiddleware.cache(false), listPods)
router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods)
router.get('/makefriends', reqValidator.makeFriends, miscMiddleware.cache(false), makeFriends)
router.get('/quitfriends', miscMiddleware.cache(false), quitFriends)
router.get('/', cacheMiddleware.cache(false), listPods)
router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
// Post because this is a secured request
router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods)
router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
// ---------------------------------------------------------------------------

View File

@ -5,7 +5,8 @@
var pluck = require('lodash-node/compat/collection/pluck')
var middleware = require('../../../middlewares')
var miscMiddleware = middleware.misc
var secureMiddleware = middleware.secure
var cacheMiddleware = middleware.cache
var reqValidator = middleware.reqValidators.remote
var videos = require('../../../models/videos')
@ -13,17 +14,17 @@
router.post('/add',
reqValidator.secureRequest,
miscMiddleware.decryptBody,
secureMiddleware.decryptBody,
reqValidator.remoteVideosAdd,
miscMiddleware.cache(false),
cacheMiddleware.cache(false),
addRemoteVideos
)
router.post('/remove',
reqValidator.secureRequest,
miscMiddleware.decryptBody,
secureMiddleware.decryptBody,
reqValidator.remoteVideosRemove,
miscMiddleware.cache(false),
cacheMiddleware.cache(false),
removeRemoteVideo
)

View File

@ -9,7 +9,7 @@
var logger = require('../../../helpers/logger')
var friends = require('../../../lib/friends')
var middleware = require('../../../middlewares')
var miscMiddleware = middleware.misc
var cacheMiddleware = middleware.cache
var reqValidator = middleware.reqValidators.videos
var Videos = require('../../../models/videos') // model
var videos = require('../../../lib/videos')
@ -38,11 +38,11 @@
var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
router.get('/', miscMiddleware.cache(false), listVideos)
router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideo)
router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
router.get('/', cacheMiddleware.cache(false), listVideos)
router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo)
router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos)
router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo)
router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos)
// ---------------------------------------------------------------------------

View File

@ -3,12 +3,12 @@
var express = require('express')
var middleware = require('../middlewares').misc
var cacheMiddleware = require('../middlewares').cache
var router = express.Router()
router.get(/^\/(index)?$/, middleware.cache(), getIndex)
router.get('/partials/:directory/:name', middleware.cache(), getPartial)
router.get(/^\/(index)?$/, cacheMiddleware.cache(), getIndex)
router.get('/partials/:directory/:name', cacheMiddleware.cache(), getPartial)
// ---------------------------------------------------------------------------

25
middlewares/cache.js Normal file
View File

@ -0,0 +1,25 @@
;(function () {
'use strict'
var cacheMiddleware = {
cache: cache
}
function cache (cache) {
return function (req, res, next) {
// If we want explicitly a cache
// Or if we don't specify if we want a cache or no and we are in production
if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) {
res.setHeader('Cache-Control', 'public')
} else {
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
}
next()
}
}
// ---------------------------------------------------------------------------
module.exports = cacheMiddleware
})()

View File

@ -2,8 +2,9 @@
'use strict'
var middlewares = {
misc: require('./misc'),
reqValidators: require('./reqValidators')
cache: require('./cache'),
reqValidators: require('./reqValidators'),
secure: require('./secure')
}
// ---------------------------------------------------------------------------

View File

@ -8,25 +8,10 @@
var Pods = require('../models/pods')
var utils = require('../helpers/utils')
var miscMiddleware = {
cache: cache,
var secureMiddleware = {
decryptBody: decryptBody
}
function cache (cache) {
return function (req, res, next) {
// If we want explicitly a cache
// Or if we don't specify if we want a cache or no and we are in production
if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) {
res.setHeader('Cache-Control', 'public')
} else {
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
}
next()
}
}
function decryptBody (req, res, next) {
var url = req.body.signature.url
Pods.findByUrl(url, function (err, pod) {
@ -61,5 +46,5 @@
// ---------------------------------------------------------------------------
module.exports = miscMiddleware
module.exports = secureMiddleware
})()