app.init = function()

in server/index.js [55:173]


app.init = function({
  logErrors,
  peers = process.env.CADENCE_TCHANNEL_PEERS || PEERS_DEFAULT,
  retryFlags = REQUEST_RETRY_FLAGS_DEFAULT,
  retryLimit = process.env.CADENCE_TCHANNEL_RETRY_LIMIT ||
    REQUEST_RETRY_LIMIT_DEFAULT,
  serviceName = process.env.CADENCE_TCHANNEL_SERVICE || SERVICE_NAME_DEFAULT,
  timeout = REQUEST_TIMEOUT_DEFAULT,
  transportClientType = process.env.TRANSPORT_CLIENT_TYPE ||
    TRANSPORT_CLIENT_TYPE_DEFAULT, // 'tchannel', 'grpc'
  useWebpack = process.env.NODE_ENV !== 'production',
  enableAuth = process.env.ENABLE_AUTH === 'true',
  authType = process.env.AUTH_TYPE,
  authAdminJwtPrivateKey = process.env.AUTH_ADMIN_JWT_PRIVATE_KEY,
} = {}) {
  const requestConfig = {
    retryFlags,
    retryLimit,
    serviceName,
    timeout,
  };

  const transportClient = transportClients[transportClientType];

  if (!transportClient) {
    throw new Error(
      `Unexpected transport client "${transportClientType}". Only support 'tchannel' or 'grpc'.`
    );
  }

  let compiler;

  if (useWebpack) {
    compiler = webpack(app.webpackConfig);
  }

  process.on('unhandledRejection', (reason, promise) => {
    console.log('Unhandled Rejection at:', promise, 'reason:', reason);
  });

  app
    .use(async (ctx, next) => {
      try {
        await next();
      } catch (err) {
        if (
          logErrors !== false &&
          (typeof err.statusCode !== 'number' || err.statusCode >= 500)
        ) {
          console.error(err);
        }

        ctx.status = err.statusCode || err.status || 500;
        ctx.body = { message: err.message };
      }
    })
    .use(koaBodyparser())
    .use(
      koaCompress({
        filter: contentType => !contentType.startsWith('text/event-stream'),
      })
    )
    .use(async function(ctx, next) {
      if (enableAuth && authType === 'ADMIN_JWT' && authAdminJwtPrivateKey) {
        ctx.authTokenHeaders = ctx.authTokenHeaders || {};
        const token = jwt.sign(
          { admin: true, ttl: 10 },
          authAdminJwtPrivateKey,
          {
            algorithm: 'RS256',
            expiresIn: '10s',
          }
        );

        ctx.authTokenHeaders['cadence-authorization'] = token;
      }

      await next();
    })
    .use(transportClient({ peers, requestConfig }))
    .use(
      useWebpack
        ? koaWebpack({
            compiler,
            dev: { stats: { colors: true } },
            hot: { port: process.env.TEST_RUN ? 8082 : 8081 },
          })
        : koaStatic(staticRoot)
    )
    .use(router.routes())
    .use(router.allowedMethods())
    .use(async function(ctx, next) {
      if (
        ['HEAD', 'GET'].includes(ctx.method) &&
        !ctx.path.startsWith('/api')
      ) {
        try {
          ctx.set('X-Content-Type-Options', 'nosniff');
          ctx.set('X-Frame-Options', 'SAMEORIGIN');
          ctx.set('X-XSS-Protection', '1; mode=block');

          if (useWebpack) {
            const filename = path.join(compiler.outputPath, 'index.html');

            ctx.set('content-type', 'text/html');
            ctx.body = compiler.outputFileSystem.readFileSync(filename);
          } else {
            await koaSend(ctx, 'index.html', { root: staticRoot });
          }
        } catch (err) {
          if (err.status !== 404) {
            throw err;
          }
        }
      }
    });

  return app;
};