text: /**
   * Copyright 2016, Sumit Rai
   * Copyrights licensed under the New MIT License. See the accompanying LICENSE file for terms.
   */
  
  import express from 'express';
  import compression from 'compression';
  import bodyParser from 'body-parser';
  import path from 'path';
  import serialize from 'serialize-javascript';
  import { navigateAction } from 'fluxible-router';
  import debugLib from 'debug';
  import React from 'react';
  import ReactDOM from 'react-dom/server';
  import app from './app';
  import HtmlComponent from './components/Html';
  import Error from './components/Error';
  import { createElementWithContext } from 'fluxible-addons-react';
  import appConfig from './configs/app';
  const env = process.env.NODE_ENV;
  
  const debug = debugLib('git-issue-viewer');
  
  const server = express();
  delete process.env.BROWSER;
  
  server.use('/public', express.static(path.join(__dirname, '/build')));
  server.use(compression());
  server.use(bodyParser.json());
  
  // Get access to the fetchr plugin instance
  var fetchrPlugin = app.getPlugin('FetchrPlugin');
  
  // Register our REST services
  fetchrPlugin.registerService(require('./services/issueService'));
  
  // Set up the fetchr middleware
  server.use(fetchrPlugin.getXhrPath(), fetchrPlugin.getMiddleware());
  
  // Create a default page
  server.get('/', function (req, res) {
      res.redirect('/' + appConfig.initialGitOwner + '/' + appConfig.initialGitRepo +
          '/' + appConfig.initialPageNumber);
  });
  
  server.use((req, res, next) => {
      const context = app.createContext({
          req: req
      });
  
      debug('Executing navigate action');
      context.getActionContext().executeAction(navigateAction, {
          url: req.url
      }, (err) => {
          if (err) {
              if (err.statusCode && err.statusCode === 404) {
                  // Pass through to next middleware
                  res.status(err.statusCode).send(
                      '<!DOCTYPE html>' + ReactDOM.renderToString(
                          <Error error={err} />
                      )
                  );
                  next();
              } else {
                  res.status(err.statusCode).send(
                      '<!DOCTYPE html>' + ReactDOM.renderToString(
                          <Error error={err} />
                      )
                  );
                  next(err);
              }
              return;
          }
  
          debug('Exposing context state');
          const exposed = 'window.App=' + serialize(app.dehydrate(context)) + ';';
  
          debug('Rendering Application component into html');
          const markup = ReactDOM.renderToString(createElementWithContext(context));
          const htmlElement = React.createElement(HtmlComponent, {
              clientFile: env === 'production' ? 'main.min.js' : 'main.js',
              context: context.getComponentContext(),
              state: exposed,
              markup: markup
          });
          const html = ReactDOM.renderToStaticMarkup(htmlElement);
  
          debug('Sending markup');
          res.type('html');
          res.write('<!DOCTYPE html>' + html);
          res.end();
      });
  });
  
  const port = process.env.PORT || 3000;
  server.listen(port);
  console.log('Application listening on port ' + port);
  
  export default server;
  