diff --git a/server/src/server.js b/server/src/server.js --- a/server/src/server.js +++ b/server/src/server.js @@ -30,9 +30,16 @@ multimediaUploadResponder, uploadDownloadResponder, } from './uploads/uploads'; -import { getGlobalURLFacts, getLandingURLFacts } from './utils/urls'; +import { + getGlobalURLFacts, + getLandingURLFacts, + getCommAppURLFacts, + generateAllRoutePaths, + generateBaseAndCommAppRoutePaths, +} from './utils/urls'; const { baseRoutePath } = getGlobalURLFacts(); +const commAppBasePath = getCommAppURLFacts().basePath; const landingBaseRoutePath = getLandingURLFacts().baseRoutePath; if (cluster.isMaster) { @@ -57,11 +64,15 @@ server.use(cookieParser()); const router = express.Router(); - 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')); + 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( '/.well-known', express.static( @@ -76,23 +87,27 @@ process.env.NODE_ENV === 'development' ? undefined : { maxAge: '1y', immutable: true }; - router.use( - '/compiled', - express.static('app_compiled', compiledFolderOptions), - ); + for (const routePath of generateBaseAndCommAppRoutePaths('compiled')) { + router.use( + routePath, + express.static('app_compiled', compiledFolderOptions), + ); + } router.use( `${landingBaseRoutePath}compiled`, express.static('landing_compiled', compiledFolderOptions), ); - router.use('/', express.static('icons')); - router.use('/commlanding', express.static('landing_icons')); + for (const routePath of generateBaseAndCommAppRoutePaths('')) { + router.use(routePath, express.static('icons')); + } + router.use(`${landingBaseRoutePath}`, express.static('landing_icons')); for (const endpoint in jsonEndpoints) { // $FlowFixMe Flow thinks endpoint is string const responder = jsonEndpoints[endpoint]; const expectCookieInvalidation = endpoint === 'log_out'; router.post( - `/${endpoint}`, + generateBaseAndCommAppRoutePaths(endpoint), jsonHandler(responder, expectCookieInvalidation), ); } @@ -103,30 +118,32 @@ ); router.get( - '/create_version/:deviceType/:codeVersion', + generateBaseAndCommAppRoutePaths('create_version/:deviceType/:codeVersion'), httpGetHandler(createNewVersionResponder), ); router.get( - '/mark_version_deployed/:deviceType/:codeVersion', + generateBaseAndCommAppRoutePaths( + 'mark_version_deployed/:deviceType/:codeVersion', + ), httpGetHandler(markVersionDeployedResponder), ); router.get( - '/download_error_report/:reportID', + generateBaseAndCommAppRoutePaths('download_error_report/:reportID'), downloadHandler(errorReportDownloadResponder), ); router.get( - '/upload/:uploadID/:secret', + generateBaseAndCommAppRoutePaths('upload/:uploadID/:secret'), downloadHandler(uploadDownloadResponder), ); // $FlowFixMe express-ws has side effects that can't be typed - router.ws('/ws', onConnection); + router.ws(`${baseRoutePath}ws`, onConnection); router.get(`${landingBaseRoutePath}*`, landingHandler); - router.get('*', htmlHandler(websiteResponder)); + router.get(['*', `${commAppBasePath}*`], htmlHandler(websiteResponder)); router.post( - '/upload_multimedia', + generateBaseAndCommAppRoutePaths('upload_multimedia'), multerProcessor, uploadHandler(multimediaUploadResponder), ); diff --git a/server/src/utils/urls.js b/server/src/utils/urls.js --- a/server/src/utils/urls.js +++ b/server/src/utils/urls.js @@ -41,10 +41,25 @@ return landingURLFacts; } +function generateAllRoutePaths(endpoint: string): Array { + const landingBaseRoutePath = landingURLFacts.baseRoutePath; + const routePaths = generateBaseAndCommAppRoutePaths(endpoint); + routePaths.push(landingBaseRoutePath + endpoint); + return routePaths; +} + +function generateBaseAndCommAppRoutePaths(endpoint: string): Array { + const { baseRoutePath } = baseURLFacts; + const commAppBaseRoutePath = commAppURLFacts.basePath; + return [baseRoutePath + endpoint, commAppBaseRoutePath + endpoint]; +} + export { getGlobalURLFacts, getSquadCalURLFacts, getCommAppURLFacts, getLandingURLFacts, getAppURLFactsFromRequestURL, + generateAllRoutePaths, + generateBaseAndCommAppRoutePaths, };