{"size": 1458, "ext": "js", "max_stars_repo_path": "packages/marko-web/src/express/index.js", "max_stars_repo_name": "Jlaird/base-cms", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/marko-web/src/express/index.js", "max_issues_repo_name": "Jlaird/base-cms", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/marko-web/src/express/index.js", "max_forks_repo_name": "Jlaird/base-cms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.0, "max_line_length": 72, "alphanum_fraction": 0.6618655693}
const { buildRequestHeaders } = require('@base-cms/tenant-context');
const cookieParser = require('cookie-parser');
const express = require('express');
const path = require('path');
const apollo = require('./apollo');
const marko = require('./marko');
const rss = require('./rss');
const sitemaps = require('./sitemaps');
const CoreConfig = require('../config/core');
const SiteConfig = require('../config/site');

module.exports = (config = {}) => {
  const { rootDir } = config;
  const distDir = path.resolve(rootDir, 'dist');
  const app = express();
  const serverDir = path.resolve(rootDir, 'server');

  // Add cookie parsing
  app.use(cookieParser());

  // Set the core config.
  app.locals.config = new CoreConfig({ ...config.coreConfig, distDir });

  // Set the website config to the app.
  app.locals.site = new SiteConfig(config.siteConfig);

  // Apply request origin.
  app.use((req, res, next) => {
    res.locals.requestOrigin = `${req.protocol}://${req.get('host')}`;
    next();
  });

  // Register apollo.
  const headers = buildRequestHeaders(config);
  apollo(app, config.graphqlUri, { link: { headers } });

  // Register the Marko middleware.
  marko(app);

  // Serve static assets
  app.use('/dist', express.static(distDir));

  // Register public files.
  app.use(express.static(path.join(serverDir, 'public')));

  // Register sitemaps.
  sitemaps(app, headers);

  // Register RSS Feeds.
  rss(app, headers);

  return app;
};
