Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

handle serving stale data snapshots #46

Merged
merged 1 commit into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ API_LEGACY_BATTLENET_KEY=''
API_LEGACY_BATTLENET_SECRET=''
API_BATTLENET_KEY=''
API_BATTLENET_SECRET=''
API_BATTLENET_APIS_DISABLED='false'
API_BATTLENET_APIS_DISABLED_JANUARY_2020='false'
API_TWITCH_EXTENSION_SHARED_SECRET=''
API_TWITCH_EXTENSION_CLIENT_ID=''
API_SNAPSHOT_DATA_DIRECTORY='../sc2pte-data-snapshot'
1 change: 1 addition & 0 deletions src/config/battlenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ bnetConfig.apiKey = env.API_BATTLENET_KEY;
/** Battle.net API secret */
bnetConfig.apiSecret = env.API_BATTLENET_SECRET;
bnetConfig.apiDisabledJanuary2020 = env.API_BATTLENET_APIS_DISABLED_JANUARY_2020 === 'true';
bnetConfig.snapshotDataDirectory = env.API_SNAPSHOT_DATA_DIRECTORY;

module.exports = bnetConfig;
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const appConfig = require('./config/app');
const dbConfig = require('./config/database');
const twitchConfig = require('./config/twitch');
const redisConfig = require('./config/redis');
const bnetConfig = require('./config/battlenet');

const getViewerRoutes = require('./routes/v1.1/viewer/get');
const saveConfigRoutes = require('./routes/v1.1/config/save');
const getConfigRoutes = require('./routes/v1.1/config/get');

const db = require('./plugins/db');

const sc2pte = require('./plugins/sc2pte');
const snapshot = require('./plugins/snapshot');

const { env } = process;

Expand Down Expand Up @@ -102,6 +103,12 @@ if (env.API_HOST_PROTOCOL === 'https') {
server.register(tlsKeygen);
}

/* Data snapshots for dark times in January 2020 when Battle.net API is down */

server.register(snapshot, {
dataDir: bnetConfig.snapshotDataDirectory,
});

/* Routes */

server.register(getViewerRoutes);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = fp(async (server, opts, next) => {
keepAlive: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
},
);

Expand Down
30 changes: 30 additions & 0 deletions src/plugins/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fp = require('fastify-plugin');
const fs = require('fs');

function snapshot(fastify, options, next) { // eslint-disable-line consistent-return
const { dataDir } = options;

function getViewerData(channelId) {
try {
const content = fs.readFileSync(`${dataDir}/${channelId}.json`, 'utf8');
const contentObject = JSON.parse(content);
if (contentObject.player && contentObject.ladders) {
return content;
}
return fs.readFileSync(`${dataDir}/blank.json`, 'utf8');
} catch (error) {
return fs.readFileSync(`${dataDir}/blank.json`, 'utf8');
}
}

fastify.decorate('snapshot', {
getViewerData,
});

next();
}

module.exports = fp(snapshot, {
fastify: '>=2.0.0',
name: 'snapshot',
});
9 changes: 8 additions & 1 deletion src/routes/v1.1/viewer/get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fp = require('fastify-plugin');
const schema = require('./schema');

const redisConfig = require('../../../../config/redis');
// const bnetConfig = require('../../../../config/battlenet');
const bnetConfig = require('../../../../config/battlenet');

module.exports = fp(async (server, opts, next) => {
server.route({
Expand Down Expand Up @@ -36,6 +36,13 @@ module.exports = fp(async (server, opts, next) => {
return reply.code(200).send(cachedReply.item);
}
server.log.info('generating and caching response...');

if (bnetConfig.apiDisabledJanuary2020) {
const responseObject = server.snapshot.getViewerData(channelId);
await server.cache.set(redisKey, responseObject, redisConfig.replyCachePeriod);
return reply.code(200).send(responseObject);
}

const channelConfigObject = await server.db.models.ChannelConfig.findOne({ channelId });

if (channelConfigObject && channelConfigObject._doc) { // eslint-disable-line
Expand Down