aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-24 14:35:11 +0200
committerChocobozzz <me@florianbigard.com>2018-07-24 14:36:25 +0200
commit98d3324db3b1c345fc30e5dbcef3b1e11169867e (patch)
tree789b15539307a3671722ccce71f3b0de1a9dcc8a /server
parent3f6d68d9671ddb7ba1c4f3a35021b84856dafb6a (diff)
downloadPeerTube-98d3324db3b1c345fc30e5dbcef3b1e11169867e.tar.gz
PeerTube-98d3324db3b1c345fc30e5dbcef3b1e11169867e.tar.zst
PeerTube-98d3324db3b1c345fc30e5dbcef3b1e11169867e.zip
Fix nodeinfo endpoint
Diffstat (limited to 'server')
-rw-r--r--server/controllers/activitypub/client.ts4
-rw-r--r--server/controllers/feeds.ts6
-rw-r--r--server/controllers/static.ts12
-rw-r--r--server/helpers/utils.ts5
-rw-r--r--server/middlewares/cache.ts73
5 files changed, 50 insertions, 50 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts
index ebb2c06a2..c90c3f931 100644
--- a/server/controllers/activitypub/client.ts
+++ b/server/controllers/activitypub/client.ts
@@ -16,7 +16,7 @@ import { VideoModel } from '../../models/video/video'
16import { VideoChannelModel } from '../../models/video/video-channel' 16import { VideoChannelModel } from '../../models/video/video-channel'
17import { VideoCommentModel } from '../../models/video/video-comment' 17import { VideoCommentModel } from '../../models/video/video-comment'
18import { VideoShareModel } from '../../models/video/video-share' 18import { VideoShareModel } from '../../models/video/video-share'
19import { cache } from '../../middlewares/cache' 19import { cacheRoute } from '../../middlewares/cache'
20import { activityPubResponse } from './utils' 20import { activityPubResponse } from './utils'
21import { AccountVideoRateModel } from '../../models/account/account-video-rate' 21import { AccountVideoRateModel } from '../../models/account/account-video-rate'
22import { 22import {
@@ -43,7 +43,7 @@ activityPubClientRouter.get('/accounts?/:name/following',
43) 43)
44 44
45activityPubClientRouter.get('/videos/watch/:id', 45activityPubClientRouter.get('/videos/watch/:id',
46 executeIfActivityPub(asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), 46 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
47 executeIfActivityPub(asyncMiddleware(videosGetValidator)), 47 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoController)) 48 executeIfActivityPub(asyncMiddleware(videoController))
49) 49)
diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts
index 6cbe42a2a..682f4abda 100644
--- a/server/controllers/feeds.ts
+++ b/server/controllers/feeds.ts
@@ -5,7 +5,7 @@ import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeed
5import { VideoModel } from '../models/video/video' 5import { VideoModel } from '../models/video/video'
6import * as Feed from 'pfeed' 6import * as Feed from 'pfeed'
7import { AccountModel } from '../models/account/account' 7import { AccountModel } from '../models/account/account'
8import { cache } from '../middlewares/cache' 8import { cacheRoute } from '../middlewares/cache'
9import { VideoChannelModel } from '../models/video/video-channel' 9import { VideoChannelModel } from '../models/video/video-channel'
10import { VideoCommentModel } from '../models/video/video-comment' 10import { VideoCommentModel } from '../models/video/video-comment'
11import { buildNSFWFilter } from '../helpers/express-utils' 11import { buildNSFWFilter } from '../helpers/express-utils'
@@ -13,7 +13,7 @@ import { buildNSFWFilter } from '../helpers/express-utils'
13const feedsRouter = express.Router() 13const feedsRouter = express.Router()
14 14
15feedsRouter.get('/feeds/video-comments.:format', 15feedsRouter.get('/feeds/video-comments.:format',
16 asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)), 16 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
17 asyncMiddleware(videoCommentsFeedsValidator), 17 asyncMiddleware(videoCommentsFeedsValidator),
18 asyncMiddleware(generateVideoCommentsFeed) 18 asyncMiddleware(generateVideoCommentsFeed)
19) 19)
@@ -21,7 +21,7 @@ feedsRouter.get('/feeds/video-comments.:format',
21feedsRouter.get('/feeds/videos.:format', 21feedsRouter.get('/feeds/videos.:format',
22 videosSortValidator, 22 videosSortValidator,
23 setDefaultSort, 23 setDefaultSort,
24 asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.FEEDS)), 24 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
25 asyncMiddleware(videoFeedsValidator), 25 asyncMiddleware(videoFeedsValidator),
26 asyncMiddleware(generateVideoFeed) 26 asyncMiddleware(generateVideoFeed)
27) 27)
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index ce5d0c5fa..f10427f3e 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -2,7 +2,7 @@ import * as cors from 'cors'
2import * as express from 'express' 2import * as express from 'express'
3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers' 3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
4import { VideosPreviewCache } from '../lib/cache' 4import { VideosPreviewCache } from '../lib/cache'
5import { cache } from '../middlewares/cache' 5import { cacheRoute } from '../middlewares/cache'
6import { asyncMiddleware, videosGetValidator } from '../middlewares' 6import { asyncMiddleware, videosGetValidator } from '../middlewares'
7import { VideoModel } from '../models/video/video' 7import { VideoModel } from '../models/video/video'
8import { VideosCaptionCache } from '../lib/cache/videos-caption-cache' 8import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
@@ -71,7 +71,7 @@ staticRouter.use(
71 71
72// robots.txt service 72// robots.txt service
73staticRouter.get('/robots.txt', 73staticRouter.get('/robots.txt',
74 asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.ROBOTS)), 74 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
75 (_, res: express.Response) => { 75 (_, res: express.Response) => {
76 res.type('text/plain') 76 res.type('text/plain')
77 return res.send(CONFIG.INSTANCE.ROBOTS) 77 return res.send(CONFIG.INSTANCE.ROBOTS)
@@ -80,7 +80,7 @@ staticRouter.get('/robots.txt',
80 80
81// nodeinfo service 81// nodeinfo service
82staticRouter.use('/.well-known/nodeinfo', 82staticRouter.use('/.well-known/nodeinfo',
83 asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)), 83 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
84 (_, res: express.Response) => { 84 (_, res: express.Response) => {
85 return res.json({ 85 return res.json({
86 links: [ 86 links: [
@@ -93,7 +93,7 @@ staticRouter.use('/.well-known/nodeinfo',
93 } 93 }
94) 94)
95staticRouter.use('/nodeinfo/:version.json', 95staticRouter.use('/nodeinfo/:version.json',
96 asyncMiddleware(cache(ROUTE_CACHE_LIFETIME.NODEINFO)), 96 // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
97 asyncMiddleware(generateNodeinfo) 97 asyncMiddleware(generateNodeinfo)
98) 98)
99 99
@@ -161,13 +161,13 @@ async function generateNodeinfo (req: express.Request, res: express.Response, ne
161 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION 161 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
162 } 162 }
163 } as HttpNodeinfoDiasporaSoftwareNsSchema20 163 } as HttpNodeinfoDiasporaSoftwareNsSchema20
164 res.set('Content-Type', 'application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8') 164 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
165 } else { 165 } else {
166 json = { error: 'Nodeinfo schema version not handled' } 166 json = { error: 'Nodeinfo schema version not handled' }
167 res.status(404) 167 res.status(404)
168 } 168 }
169 169
170 return res.end(JSON.stringify(json)) 170 return res.send(json).end()
171} 171}
172 172
173async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) { 173async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 834d788c8..9efc89d92 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -113,7 +113,7 @@ const timeTable = {
113 week: 3600000 * 24 * 7, 113 week: 3600000 * 24 * 7,
114 month: 3600000 * 24 * 30 114 month: 3600000 * 24 * 30
115} 115}
116export function parseDuration (duration: number | string, defaultDuration: number): number { 116export function parseDuration (duration: number | string): number {
117 if (typeof duration === 'number') return duration 117 if (typeof duration === 'number') return duration
118 118
119 if (typeof duration === 'string') { 119 if (typeof duration === 'string') {
@@ -130,8 +130,7 @@ export function parseDuration (duration: number | string, defaultDuration: numbe
130 } 130 }
131 } 131 }
132 132
133 logger.error('Duration could not be properly parsed, defaulting to ' + defaultDuration) 133 throw new Error('Duration could not be properly parsed')
134 return defaultDuration
135} 134}
136 135
137function resetSequelizeInstance (instance: Model<any>, savedFields: object) { 136function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts
index 1e5a13b2e..c671b88c9 100644
--- a/server/middlewares/cache.ts
+++ b/server/middlewares/cache.ts
@@ -6,59 +6,60 @@ import { logger } from '../helpers/logger'
6 6
7const lock = new AsyncLock({ timeout: 5000 }) 7const lock = new AsyncLock({ timeout: 5000 })
8 8
9function cacheRoute (lifetime: number) { 9function cacheRoute (lifetimeArg: string | number) {
10 return async function (req: express.Request, res: express.Response, next: express.NextFunction) { 10 return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
11 const redisKey = Redis.Instance.buildCachedRouteKey(req) 11 const redisKey = Redis.Instance.buildCachedRouteKey(req)
12 12
13 await lock.acquire(redisKey, async (done) => { 13 try {
14 const cached = await Redis.Instance.getCachedRoute(req) 14 await lock.acquire(redisKey, async (done) => {
15 const cached = await Redis.Instance.getCachedRoute(req)
15 16
16 // Not cached 17 // Not cached
17 if (!cached) { 18 if (!cached) {
18 logger.debug('No cached results for route %s.', req.originalUrl) 19 logger.debug('No cached results for route %s.', req.originalUrl)
19 20
20 const sendSave = res.send.bind(res) 21 const sendSave = res.send.bind(res)
21 22
22 res.send = (body) => { 23 res.send = (body) => {
23 if (res.statusCode >= 200 && res.statusCode < 400) { 24 if (res.statusCode >= 200 && res.statusCode < 400) {
24 const contentType = res.get('content-type') 25 const contentType = res.get('content-type')
25 Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) 26 const lifetime = parseDuration(lifetimeArg)
26 .then(() => done()) 27
27 .catch(err => { 28 Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
28 logger.error('Cannot cache route.', { err }) 29 .then(() => done())
29 return done(err) 30 .catch(err => {
30 }) 31 logger.error('Cannot cache route.', { err })
32 return done(err)
33 })
34 }
35
36 return sendSave(body)
31 } 37 }
32 38
33 return sendSave(body) 39 return next()
34 } 40 }
35 41
36 return next() 42 if (cached.contentType) res.set('content-type', cached.contentType)
37 }
38
39 if (cached.contentType) res.set('content-type', cached.contentType)
40 43
41 if (cached.statusCode) { 44 if (cached.statusCode) {
42 const statusCode = parseInt(cached.statusCode, 10) 45 const statusCode = parseInt(cached.statusCode, 10)
43 if (!isNaN(statusCode)) res.status(statusCode) 46 if (!isNaN(statusCode)) res.status(statusCode)
44 } 47 }
45 48
46 logger.debug('Use cached result for %s.', req.originalUrl) 49 logger.debug('Use cached result for %s.', req.originalUrl)
47 res.send(cached.body).end() 50 res.send(cached.body).end()
48 51
49 return done() 52 return done()
50 }) 53 })
54 } catch (err) {
55 logger.error('Cannot serve cached route.', err)
56 return next()
57 }
51 } 58 }
52} 59}
53 60
54const cache = (duration: number | string) => {
55 const _lifetime = parseDuration(duration, 3600000)
56 return cacheRoute(_lifetime)
57}
58
59// --------------------------------------------------------------------------- 61// ---------------------------------------------------------------------------
60 62
61export { 63export {
62 cacheRoute, 64 cacheRoute
63 cache
64} 65}