import { VideoChannelModel } from '../../models/video/video-channel'
import { VideoCommentModel } from '../../models/video/video-comment'
import { VideoShareModel } from '../../models/video/video-share'
-import { cache } from '../../middlewares/cache'
+import { cacheRoute } from '../../middlewares/cache'
import { activityPubResponse } from './utils'
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
import {
)
activityPubClientRouter.get('/videos/watch/:id',
- executeIfActivityPub(asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
+ executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
executeIfActivityPub(asyncMiddleware(videoController))
)
import { VideoModel } from '../models/video/video'
import * as Feed from 'pfeed'
import { AccountModel } from '../models/account/account'
-import { cache } from '../middlewares/cache'
+import { cacheRoute } from '../middlewares/cache'
import { VideoChannelModel } from '../models/video/video-channel'
import { VideoCommentModel } from '../models/video/video-comment'
import { buildNSFWFilter } from '../helpers/express-utils'
const feedsRouter = express.Router()
feedsRouter.get('/feeds/video-comments.:format',
- asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)),
+ asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
asyncMiddleware(videoCommentsFeedsValidator),
asyncMiddleware(generateVideoCommentsFeed)
)
feedsRouter.get('/feeds/videos.:format',
videosSortValidator,
setDefaultSort,
- asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)),
+ asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
asyncMiddleware(videoFeedsValidator),
asyncMiddleware(generateVideoFeed)
)
import * as express from 'express'
import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
import { VideosPreviewCache } from '../lib/cache'
-import { cache } from '../middlewares/cache'
+import { cacheRoute } from '../middlewares/cache'
import { asyncMiddleware, videosGetValidator } from '../middlewares'
import { VideoModel } from '../models/video/video'
import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
// robots.txt service
staticRouter.get('/robots.txt',
- asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ROBOTS)),
+ asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
(_, res: express.Response) => {
res.type('text/plain')
return res.send(CONFIG.INSTANCE.ROBOTS)
// nodeinfo service
staticRouter.use('/.well-known/nodeinfo',
- asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)),
+ asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
(_, res: express.Response) => {
return res.json({
links: [
}
)
staticRouter.use('/nodeinfo/:version.json',
- asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)),
+ // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
asyncMiddleware(generateNodeinfo)
)
nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
}
} as HttpNodeinfoDiasporaSoftwareNsSchema20
- res.set('Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8')
+ res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
} else {
json = { error: 'Nodeinfo schema version not handled' }
res.status(404)
}
- return res.end(JSON.stringify(json))
+ return res.send(json).end()
}
async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
week: 3600000 * 24 * 7,
month: 3600000 * 24 * 30
}
-export function parseDuration (duration: number | string, defaultDuration: number): number {
+export function parseDuration (duration: number | string): number {
if (typeof duration === 'number') return duration
if (typeof duration === 'string') {
}
}
- logger.error('Duration could not be properly parsed, defaulting to ' + defaultDuration)
- return defaultDuration
+ throw new Error('Duration could not be properly parsed')
}
function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
const lock = new AsyncLock({ timeout: 5000 })
-function cacheRoute (lifetime: number) {
+function cacheRoute (lifetimeArg: string | number) {
return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
const redisKey = Redis.Instance.buildCachedRouteKey(req)
- await lock.acquire(redisKey, async (done) => {
- const cached = await Redis.Instance.getCachedRoute(req)
+ try {
+ await lock.acquire(redisKey, async (done) => {
+ const cached = await Redis.Instance.getCachedRoute(req)
- // Not cached
- if (!cached) {
- logger.debug('No cached results for route %s.', req.originalUrl)
+ // Not cached
+ if (!cached) {
+ logger.debug('No cached results for route %s.', req.originalUrl)
- const sendSave = res.send.bind(res)
+ const sendSave = res.send.bind(res)
- res.send = (body) => {
- if (res.statusCode >= 200 && res.statusCode < 400) {
- const contentType = res.get('content-type')
- Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
- .then(() => done())
- .catch(err => {
- logger.error('Cannot cache route.', { err })
- return done(err)
- })
+ res.send = (body) => {
+ if (res.statusCode >= 200 && res.statusCode < 400) {
+ const contentType = res.get('content-type')
+ const lifetime = parseDuration(lifetimeArg)
+
+ Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
+ .then(() => done())
+ .catch(err => {
+ logger.error('Cannot cache route.', { err })
+ return done(err)
+ })
+ }
+
+ return sendSave(body)
}
- return sendSave(body)
+ return next()
}
- return next()
- }
-
- if (cached.contentType) res.set('content-type', cached.contentType)
+ if (cached.contentType) res.set('content-type', cached.contentType)
- if (cached.statusCode) {
- const statusCode = parseInt(cached.statusCode, 10)
- if (!isNaN(statusCode)) res.status(statusCode)
- }
+ if (cached.statusCode) {
+ const statusCode = parseInt(cached.statusCode, 10)
+ if (!isNaN(statusCode)) res.status(statusCode)
+ }
- logger.debug('Use cached result for %s.', req.originalUrl)
- res.send(cached.body).end()
+ logger.debug('Use cached result for %s.', req.originalUrl)
+ res.send(cached.body).end()
- return done()
- })
+ return done()
+ })
+ } catch (err) {
+ logger.error('Cannot serve cached route.', err)
+ return next()
+ }
}
}
-const cache = (duration: number | string) => {
- const _lifetime = parseDuration(duration, 3600000)
- return cacheRoute(_lifetime)
-}
-
// ---------------------------------------------------------------------------
export {
- cacheRoute,
- cache
+ cacheRoute
}