diff --git a/config/server.js b/config/server.js index ef9e5019b..7779a2ec1 100644 --- a/config/server.js +++ b/config/server.js @@ -35,7 +35,7 @@ const config = { password: process.env.RCTF_REDIS_PASSWORD, database: process.env.RCTF_REDIS_DATABASE }, - migrate: process.env.RCTF_DATABASE_MIGRATE || 'never' + migrate: process.env.RCTF_DATABASE_MIGRATE || 'never' // enum: never, before, only }, email: { smtpUrl: process.env.RCTF_SMTP_URL, @@ -57,7 +57,8 @@ const config = { ctftimeClientSecret: process.env.RCTF_CTFTIME_CLIENT_SECRET, loginTimeout: 10 * 60 * 1000, startTime: parseInt(process.env.RCTF_START_TIME) || Date.now() - 24 * 60 * 60 * 1000, - endTime: parseInt(process.env.RCTF_END_TIME) || Date.now() + 24 * 60 * 60 * 1000 + endTime: parseInt(process.env.RCTF_END_TIME) || Date.now() + 24 * 60 * 60 * 1000, + instanceType: process.env.RCTF_INSTANCE_TYPE || 'all' // enum: all, frontend, leaderboard } module.exports = config diff --git a/server/app.js b/server/app.js index fdc238733..b42f0399d 100644 --- a/server/app.js +++ b/server/app.js @@ -3,11 +3,8 @@ import express from 'express' import helmet from 'helmet' import { enableCORS, serveIndex } from './util' import { init as uploadProviderInit } from './uploads' -import { startUpdater as leaderboardStartUpdater } from './leaderboard' import api from './api' -leaderboardStartUpdater() - const app = express() // Compression testing should be done in development only diff --git a/server/index.js b/server/index.js index 5b61409ab..bf862b1fa 100644 --- a/server/index.js +++ b/server/index.js @@ -2,6 +2,7 @@ import 'dotenv/config' import config from '../config/server' import migrate from './database/migrate' +import { init as uploadProviderInit } from './uploads' (async () => { if (config.database.migrate === 'before') { @@ -13,8 +14,17 @@ import migrate from './database/migrate' throw new Error('migration config not recognized') } - const PORT = process.env.PORT || 3000 + if (config.instanceType === 'frontend' || config.instanceType === 'all') { + const port = process.env.PORT || 3000 - const { default: app } = await import('./app') - app.listen(PORT, () => console.log(`Started server at port ${PORT}`)) + const { default: app } = await import('./app') + app.listen(port, () => console.log(`Started server at port ${port}`)) + } else { + uploadProviderInit(null) + } + if (config.instanceType === 'leaderboard' || config.instanceType === 'all') { + const { startUpdater } = await import('./leaderboard') + startUpdater() + console.log('Started leaderboard updater') + } })() diff --git a/server/providers/uploads/dummy/index.ts b/server/providers/uploads/dummy/index.ts new file mode 100644 index 000000000..b448c86fb --- /dev/null +++ b/server/providers/uploads/dummy/index.ts @@ -0,0 +1,5 @@ +import { Provider } from '../../../uploads/types' + +export default class DummyProvider implements Provider { + upload = async (): Promise => '' +} diff --git a/server/uploads/index.ts b/server/uploads/index.ts index 0d0fe679f..989a5747b 100644 --- a/server/uploads/index.ts +++ b/server/uploads/index.ts @@ -5,8 +5,9 @@ import { Provider } from './types' let provider: Provider = null -export const init = (app: express.Application): void => { - const ProviderClass = require(path.join('../providers', config.uploadProvider.name)).default +export const init = (app: express.Application | null): void => { + const name = app === null ? 'uploads/dummy' : config.uploadProvider.name + const ProviderClass = require(path.join('../providers', name)).default provider = new ProviderClass(config.uploadProvider.options, app) }