diff --git a/server/src/server.js b/server/src/server.js --- a/server/src/server.js +++ b/server/src/server.js @@ -34,90 +34,94 @@ const { baseRoutePath } = getGlobalURLFacts(); const landingBaseRoutePath = getLandingURLFacts().baseRoutePath; -if (cluster.isMaster) { - 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(); - router.use('/images', express.static('images')); - router.use('/commlanding/images', express.static('images')); - router.use('/fonts', express.static('fonts')); - router.use('/commlanding/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 }; - router.use( - '/compiled', - express.static('app_compiled', compiledFolderOptions), - ); - router.use( - '/commlanding/compiled', - express.static('landing_compiled', compiledFolderOptions), - ); - router.use('/', express.static('icons')); - router.use('/commlanding', express.static('landing_icons')); +async function serverMain() { + if (cluster.isMaster) { + 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()); - for (const endpoint in jsonEndpoints) { - // $FlowFixMe Flow thinks endpoint is string - const responder = jsonEndpoints[endpoint]; - const expectCookieInvalidation = endpoint === 'log_out'; - router.post( - `/${endpoint}`, - jsonHandler(responder, expectCookieInvalidation), + const router = express.Router(); + router.use('/images', express.static('images')); + router.use('/commlanding/images', express.static('images')); + router.use('/fonts', express.static('fonts')); + router.use('/commlanding/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 }; + router.use( + '/compiled', + express.static('app_compiled', compiledFolderOptions), + ); + router.use( + '/commlanding/compiled', + express.static('landing_compiled', compiledFolderOptions), + ); + router.use('/', express.static('icons')); + router.use('/commlanding', express.static('landing_icons')); - router.post('/commlanding/subscribe_email', emailSubscriptionResponder); + for (const endpoint in jsonEndpoints) { + // $FlowFixMe Flow thinks endpoint is string + const responder = jsonEndpoints[endpoint]; + const expectCookieInvalidation = endpoint === 'log_out'; + router.post( + `/${endpoint}`, + jsonHandler(responder, expectCookieInvalidation), + ); + } - router.get( - '/create_version/:deviceType/:codeVersion', - httpGetHandler(createNewVersionResponder), - ); - router.get( - '/mark_version_deployed/:deviceType/:codeVersion', - httpGetHandler(markVersionDeployedResponder), - ); + router.post('/commlanding/subscribe_email', emailSubscriptionResponder); - router.get( - '/download_error_report/:reportID', - downloadHandler(errorReportDownloadResponder), - ); - router.get( - '/upload/:uploadID/:secret', - downloadHandler(uploadDownloadResponder), - ); + router.get( + '/create_version/:deviceType/:codeVersion', + httpGetHandler(createNewVersionResponder), + ); + router.get( + '/mark_version_deployed/:deviceType/:codeVersion', + httpGetHandler(markVersionDeployedResponder), + ); - // $FlowFixMe express-ws has side effects that can't be typed - router.ws('/ws', onConnection); - router.get(`${landingBaseRoutePath}*`, landingHandler); - router.get('*', htmlHandler(websiteResponder)); + router.get( + '/download_error_report/:reportID', + downloadHandler(errorReportDownloadResponder), + ); + router.get( + '/upload/:uploadID/:secret', + downloadHandler(uploadDownloadResponder), + ); - router.post( - '/upload_multimedia', - multerProcessor, - uploadHandler(multimediaUploadResponder), - ); + // $FlowFixMe express-ws has side effects that can't be typed + router.ws('/ws', onConnection); + router.get(`${landingBaseRoutePath}*`, landingHandler); + router.get('*', htmlHandler(websiteResponder)); - server.use(baseRoutePath, router); - server.listen(parseInt(process.env.PORT, 10) || 3000, 'localhost'); + router.post( + '/upload_multimedia', + multerProcessor, + uploadHandler(multimediaUploadResponder), + ); + + server.use(baseRoutePath, router); + server.listen(parseInt(process.env.PORT, 10) || 3000, 'localhost'); + } } + +serverMain();