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 { VideoChannelModel } from '../../models/video/video-channel'
|
||||||
import { VideoCommentModel } from '../../models/video/video-comment'
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
||||||
import { VideoShareModel } from '../../models/video/video-share'
|
import { VideoShareModel } from '../../models/video/video-share'
|
||||||
import { cache } from '../../middlewares/cache'
|
import { cacheRoute } from '../../middlewares/cache'
|
||||||
import { activityPubResponse } from './utils'
|
import { activityPubResponse } from './utils'
|
||||||
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
||||||
import {
|
import {
|
||||||
|
@ -43,7 +43,7 @@ activityPubClientRouter.get('/accounts?/:name/following',
|
||||||
)
|
)
|
||||||
|
|
||||||
activityPubClientRouter.get('/videos/watch/:id',
|
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(videosGetValidator)),
|
||||||
executeIfActivityPub(asyncMiddleware(videoController))
|
executeIfActivityPub(asyncMiddleware(videoController))
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeed
|
||||||
import { VideoModel } from '../models/video/video'
|
import { VideoModel } from '../models/video/video'
|
||||||
import * as Feed from 'pfeed'
|
import * as Feed from 'pfeed'
|
||||||
import { AccountModel } from '../models/account/account'
|
import { AccountModel } from '../models/account/account'
|
||||||
import { cache } from '../middlewares/cache'
|
import { cacheRoute } from '../middlewares/cache'
|
||||||
import { VideoChannelModel } from '../models/video/video-channel'
|
import { VideoChannelModel } from '../models/video/video-channel'
|
||||||
import { VideoCommentModel } from '../models/video/video-comment'
|
import { VideoCommentModel } from '../models/video/video-comment'
|
||||||
import { buildNSFWFilter } from '../helpers/express-utils'
|
import { buildNSFWFilter } from '../helpers/express-utils'
|
||||||
|
@ -13,7 +13,7 @@ import { buildNSFWFilter } from '../helpers/express-utils'
|
||||||
const feedsRouter = express.Router()
|
const feedsRouter = express.Router()
|
||||||
|
|
||||||
feedsRouter.get('/feeds/video-comments.:format',
|
feedsRouter.get('/feeds/video-comments.:format',
|
||||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)),
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
|
||||||
asyncMiddleware(videoCommentsFeedsValidator),
|
asyncMiddleware(videoCommentsFeedsValidator),
|
||||||
asyncMiddleware(generateVideoCommentsFeed)
|
asyncMiddleware(generateVideoCommentsFeed)
|
||||||
)
|
)
|
||||||
|
@ -21,7 +21,7 @@ feedsRouter.get('/feeds/video-comments.:format',
|
||||||
feedsRouter.get('/feeds/videos.:format',
|
feedsRouter.get('/feeds/videos.:format',
|
||||||
videosSortValidator,
|
videosSortValidator,
|
||||||
setDefaultSort,
|
setDefaultSort,
|
||||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)),
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
|
||||||
asyncMiddleware(videoFeedsValidator),
|
asyncMiddleware(videoFeedsValidator),
|
||||||
asyncMiddleware(generateVideoFeed)
|
asyncMiddleware(generateVideoFeed)
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as cors from 'cors'
|
||||||
import * as express from 'express'
|
import * as express from 'express'
|
||||||
import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
|
import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
|
||||||
import { VideosPreviewCache } from '../lib/cache'
|
import { VideosPreviewCache } from '../lib/cache'
|
||||||
import { cache } from '../middlewares/cache'
|
import { cacheRoute } from '../middlewares/cache'
|
||||||
import { asyncMiddleware, videosGetValidator } from '../middlewares'
|
import { asyncMiddleware, videosGetValidator } from '../middlewares'
|
||||||
import { VideoModel } from '../models/video/video'
|
import { VideoModel } from '../models/video/video'
|
||||||
import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
|
import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
|
||||||
|
@ -71,7 +71,7 @@ staticRouter.use(
|
||||||
|
|
||||||
// robots.txt service
|
// robots.txt service
|
||||||
staticRouter.get('/robots.txt',
|
staticRouter.get('/robots.txt',
|
||||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ROBOTS)),
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
|
||||||
(_, res: express.Response) => {
|
(_, res: express.Response) => {
|
||||||
res.type('text/plain')
|
res.type('text/plain')
|
||||||
return res.send(CONFIG.INSTANCE.ROBOTS)
|
return res.send(CONFIG.INSTANCE.ROBOTS)
|
||||||
|
@ -80,7 +80,7 @@ staticRouter.get('/robots.txt',
|
||||||
|
|
||||||
// nodeinfo service
|
// nodeinfo service
|
||||||
staticRouter.use('/.well-known/nodeinfo',
|
staticRouter.use('/.well-known/nodeinfo',
|
||||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
||||||
(_, res: express.Response) => {
|
(_, res: express.Response) => {
|
||||||
return res.json({
|
return res.json({
|
||||||
links: [
|
links: [
|
||||||
|
@ -93,7 +93,7 @@ staticRouter.use('/.well-known/nodeinfo',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
staticRouter.use('/nodeinfo/:version.json',
|
staticRouter.use('/nodeinfo/:version.json',
|
||||||
asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
// asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
|
||||||
asyncMiddleware(generateNodeinfo)
|
asyncMiddleware(generateNodeinfo)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -161,13 +161,13 @@ async function generateNodeinfo (req: express.Request, res: express.Response, ne
|
||||||
nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
|
nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
|
||||||
}
|
}
|
||||||
} as HttpNodeinfoDiasporaSoftwareNsSchema20
|
} 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 {
|
} else {
|
||||||
json = { error: 'Nodeinfo schema version not handled' }
|
json = { error: 'Nodeinfo schema version not handled' }
|
||||||
res.status(404)
|
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) {
|
async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||||
|
|
|
@ -113,7 +113,7 @@ const timeTable = {
|
||||||
week: 3600000 * 24 * 7,
|
week: 3600000 * 24 * 7,
|
||||||
month: 3600000 * 24 * 30
|
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 === 'number') return duration
|
||||||
|
|
||||||
if (typeof duration === 'string') {
|
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)
|
throw new Error('Duration could not be properly parsed')
|
||||||
return defaultDuration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
|
function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
|
||||||
|
|
|
@ -6,59 +6,60 @@ import { logger } from '../helpers/logger'
|
||||||
|
|
||||||
const lock = new AsyncLock({ timeout: 5000 })
|
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) {
|
return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||||
const redisKey = Redis.Instance.buildCachedRouteKey(req)
|
const redisKey = Redis.Instance.buildCachedRouteKey(req)
|
||||||
|
|
||||||
await lock.acquire(redisKey, async (done) => {
|
try {
|
||||||
const cached = await Redis.Instance.getCachedRoute(req)
|
await lock.acquire(redisKey, async (done) => {
|
||||||
|
const cached = await Redis.Instance.getCachedRoute(req)
|
||||||
|
|
||||||
// Not cached
|
// Not cached
|
||||||
if (!cached) {
|
if (!cached) {
|
||||||
logger.debug('No cached results for route %s.', req.originalUrl)
|
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) => {
|
res.send = (body) => {
|
||||||
if (res.statusCode >= 200 && res.statusCode < 400) {
|
if (res.statusCode >= 200 && res.statusCode < 400) {
|
||||||
const contentType = res.get('content-type')
|
const contentType = res.get('content-type')
|
||||||
Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
|
const lifetime = parseDuration(lifetimeArg)
|
||||||
.then(() => done())
|
|
||||||
.catch(err => {
|
Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
|
||||||
logger.error('Cannot cache route.', { err })
|
.then(() => done())
|
||||||
return done(err)
|
.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) {
|
logger.debug('Use cached result for %s.', req.originalUrl)
|
||||||
const statusCode = parseInt(cached.statusCode, 10)
|
res.send(cached.body).end()
|
||||||
if (!isNaN(statusCode)) res.status(statusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug('Use cached result for %s.', req.originalUrl)
|
return done()
|
||||||
res.send(cached.body).end()
|
})
|
||||||
|
} catch (err) {
|
||||||
return done()
|
logger.error('Cannot serve cached route.', err)
|
||||||
})
|
return next()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const cache = (duration: number | string) => {
|
|
||||||
const _lifetime = parseDuration(duration, 3600000)
|
|
||||||
return cacheRoute(_lifetime)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
export {
|
export {
|
||||||
cacheRoute,
|
cacheRoute
|
||||||
cache
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue