Fix nodeinfo endpoint
This commit is contained in:
parent
3f6d68d967
commit
98d3324db3
|
@ -16,7 +16,7 @@ import { VideoModel } from '../../models/video/video'
|
|||
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 {
|
||||
|
@ -43,7 +43,7 @@ activityPubClientRouter.get('/accounts?/:name/following',
|
|||
)
|
||||
|
||||
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))
|
||||
)
|
||||
|
|
|
@ -5,7 +5,7 @@ import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeed
|
|||
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'
|
||||
|
@ -13,7 +13,7 @@ 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)
|
||||
)
|
||||
|
@ -21,7 +21,7 @@ feedsRouter.get('/feeds/video-comments.:format',
|
|||
feedsRouter.get('/feeds/videos.:format',
|
||||
videosSortValidator,
|
||||
setDefaultSort,
|
||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)),
|
||||
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
|
||||
asyncMiddleware(videoFeedsValidator),
|
||||
asyncMiddleware(generateVideoFeed)
|
||||
)
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as cors from 'cors'
|
|||
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'
|
||||
|
@ -71,7 +71,7 @@ staticRouter.use(
|
|||
|
||||
// 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)
|
||||
|
@ -80,7 +80,7 @@ staticRouter.get('/robots.txt',
|
|||
|
||||
// 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: [
|
||||
|
@ -93,7 +93,7 @@ staticRouter.use('/.well-known/nodeinfo',
|
|||
}
|
||||
)
|
||||
staticRouter.use('/nodeinfo/:version.json',
|
||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
||||
// asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
||||
asyncMiddleware(generateNodeinfo)
|
||||
)
|
||||
|
||||
|
@ -161,13 +161,13 @@ async function generateNodeinfo (req: express.Request, res: express.Response, ne
|
|||
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) {
|
||||
|
|
|
@ -113,7 +113,7 @@ const timeTable = {
|
|||
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') {
|
||||
|
@ -130,8 +130,7 @@ export function parseDuration (duration: number | string, defaultDuration: numbe
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -6,59 +6,60 @@ import { logger } from '../helpers/logger'
|
|||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue