Skip to content

Commit

Permalink
lint the entire repo with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
beckpaul committed Dec 12, 2023
1 parent 5cd1c4f commit 5c686a0
Show file tree
Hide file tree
Showing 91 changed files with 2,357 additions and 1,209 deletions.
95 changes: 61 additions & 34 deletions src/backend/AppKernel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { appContainer } = require('./dependency-injection/AppContainer')
const { RequestContainer } = require('./dependency-injection/RequestContainer')
const { RequestContainerCompilerPass } = require('./dependency-injection/RequestContainerCompilerPass')
const {
RequestContainerCompilerPass,
} = require('./dependency-injection/RequestContainerCompilerPass')
const { webpackAsset } = require('./middleware/webpackAsset')
const { bootPassport } = require('./security/bootPassport')
const express = require('./ExpressApp')
Expand All @@ -22,71 +24,82 @@ const accountRouter = require('./routes/views/accountRouter')
const dataRouter = require('./routes/views/dataRouter')

class AppKernel {
constructor (nodeEnv = 'production') {
constructor(nodeEnv = 'production') {
this.env = nodeEnv
this.config = appConfig
this.expressApp = null
this.appContainer = null
this.schedulers = []
}

async boot () {
async boot() {
await this.compileContainer(this.config)
this.bootstrapExpress()
return this
}

async compileContainer (config) {
async compileContainer(config) {
this.appContainer = appContainer(config)
await this.appContainer.compile()
}

bootstrapExpress () {
bootstrapExpress() {
this.expressApp = express()

this.expressApp.locals.clanInvitations = {}
this.expressApp.use((req, res, next) => {
res.locals.navLinks = []
res.locals.cNavLinks = []
res.locals.appGlobals = {
loggedInUser: null
loggedInUser: null,
}
next()
})

this.expressApp.set('views', 'src/backend/templates/views')
this.expressApp.set('view engine', 'pug')
this.expressApp.use(express.static('public', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000 // 4 hours
}))
this.expressApp.use(
express.static('public', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000, // 4 hours
})
)

this.expressApp.use('/dist', express.static('dist', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000 // 4 hours, could be longer since we got cache-busting
}))
this.expressApp.use(
'/dist',
express.static('dist', {
immutable: true,
maxAge: 4 * 60 * 60 * 1000, // 4 hours, could be longer since we got cache-busting
})
)

this.expressApp.use(express.json())
this.expressApp.use(bodyParser.json())
this.expressApp.use(bodyParser.urlencoded({ extended: false }))
this.expressApp.use(webpackAsset(this.appContainer.getParameter('webpackManifestJS')))

this.expressApp.use(session({
resave: false,
saveUninitialized: true,
secret: appConfig.session.key,
store: new FileStore({
retries: 0,
ttl: appConfig.session.tokenLifespan,
secret: appConfig.session.key
this.expressApp.use(
webpackAsset(this.appContainer.getParameter('webpackManifestJS'))
)

this.expressApp.use(
session({
resave: false,
saveUninitialized: true,
secret: appConfig.session.key,
store: new FileStore({
retries: 0,
ttl: appConfig.session.tokenLifespan,
secret: appConfig.session.key,
}),
})
}))
)
bootPassport(this.expressApp, this.config)

this.expressApp.use(async (req, res, next) => {
req.appContainer = this.appContainer
req.requestContainer = RequestContainer(this.appContainer, req)
req.requestContainer.addCompilerPass(new RequestContainerCompilerPass(this.config, req))
req.requestContainer.addCompilerPass(
new RequestContainerCompilerPass(this.config, req)
)
await req.requestContainer.compile()

if (req.requestContainer.fafThrownException) {
Expand All @@ -100,7 +113,7 @@ class AppKernel {
this.expressApp.use(function (req, res, next) {
req.asyncFlash = async function () {
const result = req.flash(...arguments)
await new Promise(resolve => req.session.save(resolve))
await new Promise((resolve) => req.session.save(resolve))

Check warning on line 116 in src/backend/AppKernel.js

View check run for this annotation

Codecov / codecov/patch

src/backend/AppKernel.js#L116

Added line #L116 was not covered by tests

return result
}
Expand All @@ -114,19 +127,27 @@ class AppKernel {

this.expressApp.use(function (req, res, next) {
if (req.isAuthenticated()) {
res.locals.appGlobals.loggedInUser = req.requestContainer.get('UserService').getUser()
res.locals.appGlobals.loggedInUser = req.requestContainer
.get('UserService')
.getUser()
}
next()
})
}

startCronJobs () {
this.schedulers.push(leaderboardCacheCrawler(this.appContainer.get('LeaderboardService')))
this.schedulers.push(wordpressCacheCrawler(this.appContainer.get('WordpressService')))
this.schedulers.push(clanCacheCrawler(this.appContainer.get('ClanService')))
startCronJobs() {
this.schedulers.push(

Check warning on line 139 in src/backend/AppKernel.js

View check run for this annotation

Codecov / codecov/patch

src/backend/AppKernel.js#L138-L139

Added lines #L138 - L139 were not covered by tests
leaderboardCacheCrawler(this.appContainer.get('LeaderboardService'))
)
this.schedulers.push(

Check warning on line 142 in src/backend/AppKernel.js

View check run for this annotation

Codecov / codecov/patch

src/backend/AppKernel.js#L142

Added line #L142 was not covered by tests
wordpressCacheCrawler(this.appContainer.get('WordpressService'))
)
this.schedulers.push(

Check warning on line 145 in src/backend/AppKernel.js

View check run for this annotation

Codecov / codecov/patch

src/backend/AppKernel.js#L145

Added line #L145 was not covered by tests
clanCacheCrawler(this.appContainer.get('ClanService'))
)
}

loadControllers () {
loadControllers() {
this.expressApp.use('/', defaultRouter)
this.expressApp.use('/', authRouter)
this.expressApp.use('/', staticMarkdownRouter)
Expand All @@ -140,7 +161,13 @@ class AppKernel {
res.status(404).render('errors/404')
})
this.expressApp.use((err, req, res, next) => {
console.error('[error] Incoming request to"', req.originalUrl, '"failed with error "', err.toString(), '"')
console.error(

Check warning on line 164 in src/backend/AppKernel.js

View check run for this annotation

Codecov / codecov/patch

src/backend/AppKernel.js#L164

Added line #L164 was not covered by tests
'[error] Incoming request to"',
req.originalUrl,
'"failed with error "',
err.toString(),
'"'
)
console.error(err.stack)

if (res.headersSent) {
Expand Down
8 changes: 4 additions & 4 deletions src/backend/config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ const appConfig = {
host: process.env.HOST || 'http://localhost',
session: {
key: process.env.SESSION_SECRET_KEY || '12345',
tokenLifespan: process.env.TOKEN_LIFESPAN || 43200
tokenLifespan: process.env.TOKEN_LIFESPAN || 43200,
},
oauth: {
strategy: 'faforever',
clientId: process.env.OAUTH_CLIENT_ID || '12345',
clientSecret: process.env.OAUTH_CLIENT_SECRET || '12345',
url: oauthUrl,
publicUrl: process.env.OAUTH_PUBLIC_URL || oauthUrl,
callback: process.env.CALLBACK || 'callback'
callback: process.env.CALLBACK || 'callback',
},
m2mOauth: {
clientId: process.env.OAUTH_M2M_CLIENT_ID || 'faf-website-public',
clientSecret: process.env.OAUTH_M2M_CLIENT_SECRET || 'banana',
url: oauthUrl
url: oauthUrl,
},
apiUrl: process.env.API_URL || 'https://api.faforever.com',
wordpressUrl: process.env.WP_URL || 'https://direct.faforever.com',
extractorInterval: process.env.EXTRACTOR_INTERVAL || 5,
playerCountInterval: process.env.PLAYER_COUNT_INTERVAL || 15,
recaptchaKey: process.env.RECAPTCHA_SITE_KEY || 'test'
recaptchaKey: process.env.RECAPTCHA_SITE_KEY || 'test',
}

module.exports = appConfig
18 changes: 13 additions & 5 deletions src/backend/cron-jobs/clanCacheCrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ const errorHandler = (e, name) => {

const warmupClans = async (clanService) => {
try {
await clanService.getAll(true)
await clanService

Check warning on line 13 in src/backend/cron-jobs/clanCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/clanCacheCrawler.js#L13

Added line #L13 was not covered by tests
.getAll(true)
.then(() => successHandler('clanService::getAll(global)'))
.catch((e) => errorHandler(e, 'clanService::getAll(global)'))
} catch (e) {
console.error('Error: clanCacheCrawler::warmupClans failed with "' + e.toString() + '"',
{ entrypoint: 'clanCacheCrawler.js' })
console.error(

Check warning on line 18 in src/backend/cron-jobs/clanCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/clanCacheCrawler.js#L18

Added line #L18 was not covered by tests
'Error: clanCacheCrawler::warmupClans failed with "' +
e.toString() +
'"',
{ entrypoint: 'clanCacheCrawler.js' }
)
console.error(e.stack)
}
}
Expand All @@ -27,8 +32,11 @@ const warmupClans = async (clanService) => {
module.exports = (clanService) => {
warmupClans(clanService).then(() => {})

const clansScheduler = new Scheduler('createClanCache', // Refresh cache every 59 minutes
() => warmupClans(clanService).then(() => {}), 60 * 59 * 1000)
const clansScheduler = new Scheduler(

Check warning on line 35 in src/backend/cron-jobs/clanCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/clanCacheCrawler.js#L35

Added line #L35 was not covered by tests
'createClanCache', // Refresh cache every 59 minutes
() => warmupClans(clanService).then(() => {}),

Check warning on line 37 in src/backend/cron-jobs/clanCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/clanCacheCrawler.js#L37

Added line #L37 was not covered by tests
60 * 59 * 1000
)
clansScheduler.start()

return clansScheduler
Expand Down
63 changes: 47 additions & 16 deletions src/backend/cron-jobs/leaderboardCacheCrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,57 @@ const successHandler = (name) => {
console.debug('[debug] Cache updated', { name })
}
const errorHandler = (e, name) => {
console.error(e.toString(), { name, entrypoint: 'leaderboardCacheCrawler.js' })
console.error(e.toString(), {

Check warning on line 7 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L7

Added line #L7 was not covered by tests
name,
entrypoint: 'leaderboardCacheCrawler.js',
})
console.error(e.stack)
}

const warmupLeaderboard = async (leaderboardService) => {
try {
await leaderboardService.getLeaderboard(1, true)
.then(() => successHandler('leaderboardService::getLeaderboard(global)'))
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(global)'))
await leaderboardService

Check warning on line 16 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L16

Added line #L16 was not covered by tests
.getLeaderboard(1, true)
.then(() =>
successHandler('leaderboardService::getLeaderboard(global)')

Check warning on line 19 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L18-L19

Added lines #L18 - L19 were not covered by tests
)
.catch((e) =>
errorHandler(e, 'leaderboardService::getLeaderboard(global)')

Check warning on line 22 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L21-L22

Added lines #L21 - L22 were not covered by tests
)

await leaderboardService.getLeaderboard(2, true)
.then(() => successHandler('leaderboardService::getLeaderboard(1v1)'))
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(1v1)'))
await leaderboardService

Check warning on line 25 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L25

Added line #L25 was not covered by tests
.getLeaderboard(2, true)
.then(() =>
successHandler('leaderboardService::getLeaderboard(1v1)')

Check warning on line 28 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L27-L28

Added lines #L27 - L28 were not covered by tests
)
.catch((e) =>
errorHandler(e, 'leaderboardService::getLeaderboard(1v1)')

Check warning on line 31 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L30-L31

Added lines #L30 - L31 were not covered by tests
)

await leaderboardService.getLeaderboard(3, true)
.then(() => successHandler('leaderboardService::getLeaderboard(2v2)'))
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(2v2)'))
await leaderboardService

Check warning on line 34 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L34

Added line #L34 was not covered by tests
.getLeaderboard(3, true)
.then(() =>
successHandler('leaderboardService::getLeaderboard(2v2)')

Check warning on line 37 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L36-L37

Added lines #L36 - L37 were not covered by tests
)
.catch((e) =>
errorHandler(e, 'leaderboardService::getLeaderboard(2v2)')

Check warning on line 40 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L39-L40

Added lines #L39 - L40 were not covered by tests
)

await leaderboardService.getLeaderboard(4, true)
.then(() => successHandler('leaderboardService::getLeaderboard(4v4)'))
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(4v4)'))
await leaderboardService

Check warning on line 43 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L43

Added line #L43 was not covered by tests
.getLeaderboard(4, true)
.then(() =>
successHandler('leaderboardService::getLeaderboard(4v4)')

Check warning on line 46 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L45-L46

Added lines #L45 - L46 were not covered by tests
)
.catch((e) =>
errorHandler(e, 'leaderboardService::getLeaderboard(4v4)')

Check warning on line 49 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L48-L49

Added lines #L48 - L49 were not covered by tests
)
} catch (e) {
console.error('Error: leaderboardCacheCrawler::warmupLeaderboard failed with "' + e.toString() + '"', { entrypoint: 'leaderboardCacheCrawler.js' })
console.error(

Check warning on line 52 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L52

Added line #L52 was not covered by tests
'Error: leaderboardCacheCrawler::warmupLeaderboard failed with "' +
e.toString() +
'"',
{ entrypoint: 'leaderboardCacheCrawler.js' }
)
console.error(e.stack)
}
}
Expand All @@ -38,8 +66,11 @@ const warmupLeaderboard = async (leaderboardService) => {
module.exports = (leaderboardService) => {
warmupLeaderboard(leaderboardService).then(() => {})

const leaderboardScheduler = new Scheduler('createLeaderboardCaches',
() => warmupLeaderboard(leaderboardService).then(() => {}), 60 * 59 * 1000)
const leaderboardScheduler = new Scheduler(

Check warning on line 69 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L69

Added line #L69 was not covered by tests
'createLeaderboardCaches',
() => warmupLeaderboard(leaderboardService).then(() => {}),

Check warning on line 71 in src/backend/cron-jobs/leaderboardCacheCrawler.js

View check run for this annotation

Codecov / codecov/patch

src/backend/cron-jobs/leaderboardCacheCrawler.js#L71

Added line #L71 was not covered by tests
60 * 59 * 1000
)
leaderboardScheduler.start()

return leaderboardScheduler
Expand Down
Loading

0 comments on commit 5c686a0

Please sign in to comment.