pass ip to locale/get call

This commit is contained in:
Sean Yesmunt 2020-04-08 16:47:06 -04:00
parent 449f117336
commit 8eb45a9557
2 changed files with 10 additions and 4 deletions

View file

@ -10,6 +10,8 @@ const cacheControlMiddleware = require('./middleware/cache-control');
const app = new Koa(); const app = new Koa();
const DIST_ROOT = path.resolve(__dirname, 'dist'); const DIST_ROOT = path.resolve(__dirname, 'dist');
app.proxy = true;
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
try { try {
await next(); await next();

View file

@ -31,8 +31,8 @@ function getSupportedCDN(continent) {
} }
} }
async function fetchContinentCookie() { async function fetchContinentCookie(ip) {
return Lbryio.call('locale', 'get', {}, 'post').then(result => { return Lbryio.call('locale', 'get', { ip }, 'post').then(result => {
const userContinent = getSupportedCDN(result.continent); const userContinent = getSupportedCDN(result.continent);
return userContinent; return userContinent;
}); });
@ -51,9 +51,13 @@ router.get(`/$/stream/:claimName/:claimId`, async ctx => {
router.get('*', async ctx => { router.get('*', async ctx => {
const hasContinentCookie = ctx.cookies.get(CONTINENT_COOKIE); const hasContinentCookie = ctx.cookies.get(CONTINENT_COOKIE);
const ip = ctx.ip;
if (!hasContinentCookie) { if (!hasContinentCookie) {
const continentValue = await fetchContinentCookie(); try {
ctx.cookies.set(CONTINENT_COOKIE, continentValue, { httpOnly: false }); const continentValue = await fetchContinentCookie(ip);
ctx.cookies.set(CONTINENT_COOKIE, continentValue, { httpOnly: false });
} catch (e) {}
} }
const html = await getHtml(ctx); const html = await getHtml(ctx);
ctx.body = html; ctx.body = html;