-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
197 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const { JavaApiM2MClientFactory } = require("./JavaApiM2MClientFactory"); | ||
const appConfig = require("../config/app"); | ||
|
||
const client = JavaApiM2MClientFactory(appConfig.m2mOauth.clientId, appConfig.m2mOauth.clientSecret, appConfig.m2mOauth.url, appConfig.apiUrl) | ||
|
||
module.exports.JavaApiM2MClient = client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const {ClientCredentials} = require("simple-oauth2"); | ||
const { Axios } = require("axios"); | ||
const { AuthFailed } = require("./ApiErrors"); | ||
|
||
const getToken = async function(clientId, clientSecret, host, scope) { | ||
const tokenClient = new ClientCredentials({ | ||
client: { | ||
id: clientId, | ||
secret: clientSecret, | ||
|
||
}, | ||
auth: { | ||
tokenHost: host, | ||
tokenPath: '/oauth2/token', | ||
revokePath: '/oauth2/revoke' | ||
}, | ||
options: { | ||
authorizationMethod: 'body' | ||
} | ||
}) | ||
|
||
try { | ||
return tokenClient.getToken({ | ||
scope: scope ?? '', | ||
}) | ||
} catch (error) { | ||
throw new AuthFailed(error.toString()) | ||
} | ||
} | ||
|
||
module.exports.JavaApiM2MClientFactory = async function(clientId, clientSecret, host, javaApiBaseURL) { | ||
let passport = await getToken(clientId, clientSecret, host, '') | ||
|
||
const client = new Axios({ | ||
baseURL: javaApiBaseURL | ||
}) | ||
|
||
client.interceptors.request.use(config => { | ||
if (passport.expired()) { | ||
passport = passport.refresh() | ||
} | ||
|
||
config.headers = { | ||
Authorization: `Bearer ${passport.token.access_token}` | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
const appConfig = require("../config/app") | ||
const WordpressServiceFactory = require("../lib/WordpressServiceFactory"); | ||
const Scheduler = require("../lib/Scheduler"); | ||
const LeaderboardService = require("../lib/LeaderboardService"); | ||
const cacheService = require("../lib/CacheService"); | ||
const LeaderboardRepository = require("../lib/LeaderboardRepository"); | ||
const {JavaApiM2MClient} = require('../lib/JavaApiM2MClient') | ||
|
||
const warmupWordpressCache = async () => { | ||
const successHandler = (name) => { | ||
console.info(name, 'cache generated') | ||
} | ||
const errorHandler = (e, name) => { | ||
console.error(name, e.toString(), 'cache failed') | ||
} | ||
|
||
const warmupWordpressCache = () => { | ||
const wordpressService = WordpressServiceFactory(appConfig.wordpressUrl) | ||
|
||
const successHandler = (name) => { | ||
console.info(name, 'cache generated') | ||
} | ||
const errorHandler = (e, name) => { | ||
console.error(name, e.toString(), 'cache failed') | ||
} | ||
|
||
wordpressService.getNews(true).then(() => successHandler('getNews')).catch((e) => errorHandler(e, 'getNews')) | ||
wordpressService.getNewshub(true).then(() => successHandler('getNewshub')).catch((e) => errorHandler(e, 'getNewshub')) | ||
wordpressService.getContentCreators(true).then(() => successHandler('getContentCreators')).catch((e) => errorHandler(e, 'getContentCreators')) | ||
wordpressService.getTournamentNews(true).then(() => successHandler('getTournamentNews')).catch((e) => errorHandler(e, 'getTournamentNews')) | ||
wordpressService.getFafTeams(true).then(() => successHandler('getFafTeams')).catch((e) => errorHandler(e, 'getFafTeams')) | ||
} | ||
|
||
const warmupLeaderboard = async () => { | ||
const leaderboardService = new LeaderboardService(cacheService, new LeaderboardRepository(await JavaApiM2MClient)) | ||
|
||
await leaderboardService.getLeaderboard(1, true).then(() => successHandler('getLeaderboard(global)')).catch((e) => errorHandler(e, 'getLeaderboard(global)')) | ||
await leaderboardService.getLeaderboard(2, true).then(() => successHandler('getLeaderboard(1v1)')).catch((e) => errorHandler(e, 'getLeaderboard(1v1)')) | ||
await leaderboardService.getLeaderboard(3, true).then(() => successHandler('getLeaderboard(2v2)')).catch((e) => errorHandler(e, 'getLeaderboard(2v2)')) | ||
await leaderboardService.getLeaderboard(4, true).then(() => successHandler('getLeaderboard(4v4)')).catch((e) => errorHandler(e, 'getLeaderboard(4v4)')) | ||
} | ||
|
||
module.exports = async () => { | ||
await warmupWordpressCache() | ||
warmupWordpressCache() | ||
await warmupLeaderboard() | ||
|
||
const wordpressScheduler = new Scheduler('createWordpressCaches', warmupWordpressCache, 60 * 59 * 1000) | ||
wordpressScheduler.start() | ||
|
||
const leaderboardScheduler = new Scheduler('createLeaderboardCaches', warmupLeaderboard, 60 * 59 * 1000) | ||
leaderboardScheduler.start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters