diff --git a/server/src/server.js b/server/src/server.js index 0e4420ba9..3501400ea 100644 --- a/server/src/server.js +++ b/server/src/server.js @@ -1,155 +1,136 @@ // @flow import cluster from 'cluster'; import cookieParser from 'cookie-parser'; import express from 'express'; import expressWs from 'express-ws'; import os from 'os'; import './cron/cron'; import { migrate } from './database/migrations'; import { jsonEndpoints } from './endpoints'; import { emailSubscriptionResponder } from './responders/comm-landing-responders'; import { jsonHandler, httpGetHandler, downloadHandler, htmlHandler, uploadHandler, } from './responders/handlers'; import landingHandler from './responders/landing-handler'; import { errorReportDownloadResponder } from './responders/report-responders'; import { createNewVersionResponder, markVersionDeployedResponder, } from './responders/version-responders'; import { websiteResponder } from './responders/website-responders'; import { onConnection } from './socket/socket'; import { multerProcessor, multimediaUploadResponder, uploadDownloadResponder, } from './uploads/uploads'; -import { - getGlobalURLFacts, - getLandingURLFacts, - generateAllRoutePaths, - generateBaseAndCommAppRoutePaths, -} from './utils/urls'; +import { getGlobalURLFacts, getLandingURLFacts } from './utils/urls'; const { baseRoutePath } = getGlobalURLFacts(); const landingBaseRoutePath = getLandingURLFacts().baseRoutePath; if (cluster.isMaster) { (async () => { const didMigrationsSucceed: boolean = await migrate(); if (!didMigrationsSucceed) { // The following line uses exit code 2 to ensure nodemon exits // in a dev environment, instead of restarting. Context provided // in https://github.com/remy/nodemon/issues/751 process.exit(2); } const cpuCount = os.cpus().length; for (let i = 0; i < cpuCount; i++) { cluster.fork(); } })(); cluster.on('exit', () => cluster.fork()); } else { const server = express(); expressWs(server); server.use(express.json({ limit: '50mb' })); server.use(cookieParser()); const router = express.Router(); - for (const routePath of generateAllRoutePaths('images')) { - router.use(routePath, express.static('images')); - } - for (const routePath of generateAllRoutePaths('fonts')) { - router.use(routePath, express.static('fonts')); - } - for (const routePath of generateBaseAndCommAppRoutePaths('misc')) { - router.use(routePath, express.static('misc')); - } + router.use('/images', express.static('images')); + router.use(`${landingBaseRoutePath}images`, express.static('images')); + router.use('/fonts', express.static('fonts')); + router.use(`${landingBaseRoutePath}fonts`, express.static('fonts')); + router.use('/misc', express.static('misc')); router.use( '/.well-known', express.static( '.well-known', // Necessary for apple-app-site-association file { setHeaders: res => res.setHeader('Content-Type', 'application/json'), }, ), ); const compiledFolderOptions = process.env.NODE_ENV === 'development' ? undefined : { maxAge: '1y', immutable: true }; - for (const routePath of generateBaseAndCommAppRoutePaths('compiled')) { - router.use( - routePath, - express.static('app_compiled', compiledFolderOptions), - ); - } + router.use( + '/compiled', + express.static('app_compiled', compiledFolderOptions), + ); router.use( `${landingBaseRoutePath}compiled`, express.static('landing_compiled', compiledFolderOptions), ); - for (const routePath of generateBaseAndCommAppRoutePaths('')) { - router.use(routePath, express.static('icons')); - } - router.use(`${landingBaseRoutePath}`, express.static('landing_icons')); + router.use('/', express.static('icons')); + router.use('/commlanding', express.static('landing_icons')); for (const endpoint in jsonEndpoints) { // $FlowFixMe Flow thinks endpoint is string const responder = jsonEndpoints[endpoint]; const expectCookieInvalidation = endpoint === 'log_out'; - for (const routePath of generateBaseAndCommAppRoutePaths(endpoint)) { - router.post(routePath, jsonHandler(responder, expectCookieInvalidation)); - } + router.post( + `/${endpoint}`, + jsonHandler(responder, expectCookieInvalidation), + ); } router.post( `${landingBaseRoutePath}subscribe_email`, emailSubscriptionResponder, ); - for (const routePath of generateBaseAndCommAppRoutePaths( - 'create_version/:deviceType/:codeVersion', - )) { - router.get(routePath, httpGetHandler(createNewVersionResponder)); - } - for (const routePath of generateBaseAndCommAppRoutePaths( - 'mark_version_deployed/:deviceType/:codeVersion', - )) { - router.get(routePath, httpGetHandler(markVersionDeployedResponder)); - } - for (const routePath of generateBaseAndCommAppRoutePaths( - 'download_error_report/:reportID', - )) { - router.get(routePath, downloadHandler(errorReportDownloadResponder)); - } - for (const routePath of generateBaseAndCommAppRoutePaths( - 'upload/:uploadID/:secret', - )) { - router.get(routePath, downloadHandler(uploadDownloadResponder)); - } + router.get( + '/create_version/:deviceType/:codeVersion', + httpGetHandler(createNewVersionResponder), + ); + router.get( + '/mark_version_deployed/:deviceType/:codeVersion', + httpGetHandler(markVersionDeployedResponder), + ); + + router.get( + '/download_error_report/:reportID', + downloadHandler(errorReportDownloadResponder), + ); + router.get( + '/upload/:uploadID/:secret', + downloadHandler(uploadDownloadResponder), + ); // $FlowFixMe express-ws has side effects that can't be typed - router.ws(`${baseRoutePath}ws`, onConnection); + router.ws('/ws', onConnection); router.get(`${landingBaseRoutePath}*`, landingHandler); router.get('*', htmlHandler(websiteResponder)); - for (const routePath of generateBaseAndCommAppRoutePaths( - 'upload_multimedia', - )) { - router.post( - routePath, - multerProcessor, - uploadHandler(multimediaUploadResponder), - ); - } + router.post( + '/upload_multimedia', + multerProcessor, + uploadHandler(multimediaUploadResponder), + ); server.use(baseRoutePath, router); server.listen(parseInt(process.env.PORT, 10) || 3000, 'localhost'); } diff --git a/server/src/utils/urls.js b/server/src/utils/urls.js index cf07a3ff3..8511df6a1 100644 --- a/server/src/utils/urls.js +++ b/server/src/utils/urls.js @@ -1,63 +1,48 @@ // @flow import commAppURLFacts from '../../facts/commapp_url'; import landingURLFacts from '../../facts/landing_url'; import squadCalURLFacts from '../../facts/squadcal_url'; import baseURLFacts from '../../facts/url'; type GlobalURLFacts = { +baseRoutePath: string, }; function getGlobalURLFacts(): GlobalURLFacts { return baseURLFacts; } export type AppURLFacts = { +baseDomain: string, +basePath: string, +https: boolean, +baseRoutePath: string, }; function getSquadCalURLFacts(): AppURLFacts { return squadCalURLFacts; } function getCommAppURLFacts(): AppURLFacts { return commAppURLFacts; } function getAppURLFactsFromRequestURL(url: string): AppURLFacts { const commURLFacts = getCommAppURLFacts(); return url.startsWith(commURLFacts.basePath) ? commURLFacts : getSquadCalURLFacts(); } function getLandingURLFacts(): AppURLFacts { return landingURLFacts; } -function generateAllRoutePaths(endpoint: string): string[] { - const landingBaseRoutePath = landingURLFacts.baseRoutePath; - const routePaths = generateBaseAndCommAppRoutePaths(endpoint); - routePaths.push(landingBaseRoutePath + endpoint); - return routePaths; -} - -function generateBaseAndCommAppRoutePaths(endpoint: string): string[] { - const { baseRoutePath } = baseURLFacts; - const commAppBaseRoutePath = commAppURLFacts.basePath; - return [baseRoutePath + endpoint, commAppBaseRoutePath + endpoint]; -} - export { getGlobalURLFacts, getSquadCalURLFacts, getCommAppURLFacts, getLandingURLFacts, getAppURLFactsFromRequestURL, - generateAllRoutePaths, - generateBaseAndCommAppRoutePaths, };