module.exports = function ApplicationWeb(configuration) {
  var app = koa();
  var visitor = ua(configuration.global.applications.api.googleanalytics.key);

  app.keys = [configuration.global.redis.key];
  app.name = configuration.application;
  app.poweredBy = false;

  app.use(responseTime());
  app.use(cors());

  // var optionsSSL;
  //
  // if (environment === 'production') {
  //   optionsSSL = {
  //     key: fs.readFileSync('server.key'),
  //     cert: fs.readFileSync('server.crt')
  //   };
  //
  //   app.use(forceSSL());
  // }

  app.use(function* (next) {
    this.locals = this.locals || {};
    this.locals.body = {};
    this.locals.date = new Date();
    this.locals.lang = (typeof this.query.lang !== 'undefined') ? this.query.lang : 'en';
    this.locals.messages = new Messages(this.locals.lang);
    this.locals.querystringParameters = Qs.parse(this.querystring);

    yield next;
  });

  app.use(function* (next) {
    this.locals = this.locals || {};
    yield next;
  });

  app.use(function* (next) {
    if (configuration.environment !== 'development') {
      visitor.pageview(this.request.originalUrl, function(err) {
        console.log('err', err);
      });
    }

    yield next;
  });

  app.use(router(app));

  app.use(gzip());
  app.use(fresh());
  app.use(etag());

  return app;
};
Exemplo n.º 2
0
  Server.prototype.init = function () {
    var layoutPage = config.isDev ? '_layout' : '_layout.production';
    var staticMaxAge = 0;

    this.app.keys = ['fnjklhjh89347932kejlqw'];
    this.app.use(bodyParser());
    this.app.use(methodOverride());
    this.app.use(session({
      path: '/',
      httpOnly: true,
      maxage: null,
      rewrite: true,
      signed: true
    }));

    render(this.app, {
      root: path.join(this.rootFolder, 'views'),
      layout: layoutPage,
      viewExt: 'html',
      cache: !config.isDev,
      debug: config.isDev,
      locals: {},
      filters: {}
    });

    if (!config.isDev) {
      this.app.use(gzip());
      this.app.use(fresh());
      this.app.use(etag());
      staticMaxAge = 365 * 24 * 60 * 60 * 1000;
    }

    this.app.use(serveStatic(path.join(this.rootFolder, '/public/bower'), {
      maxage: staticMaxAge
    }));
    this.app.use(serveStatic(path.join(this.rootFolder, config.server.assets), {
      maxage: staticMaxAge
    }));


    return this;
  };
Exemplo n.º 3
0
});
var geocoder = require( './lib/geocoder' )({
  geocode: maybeGeocode
});
var locator = require( './lib/locator' )();

var koa = require( 'koa' );
var path = require( 'path' );
var app = module.exports = koa();

// Calculate response time, at the top before any other middleware,
// to wrap all subsequent middlewares.
app.use( responseTime() );
app.use( logger() );
app.use( compress() );
app.use( fresh() );
app.use( etag() );
app.use( jsonp() );
// Support `fields` query string to reduce response, @see json-mask.
app.use( mask() );
app.use( selector() );

// Small fix to prevent request for favicon.ico.
router.get( '/favicon.ico', function *() {
  this.status = 304;
  this.type = "image/x-icon";
} );

// The routes are first handled by geocoder to ensure lng and lat params.
// It then goes through the first cache layer that will check for locations
// cache to return. If not, if then check if the map KML is cached and set
module.exports = function ApplicationWww(configuration, app, router, db, models, routes, render) {
  // var logger = new Logger(configuration, app);

  app.version = configuration.version;

  app.use(responseTime());
  app.use(helmet.defaults());
  // app.use(health());
  app.use(bodyParser());
  // app.use(session());

  if (configuration.type === 'version') {
    app.use(function* (next) {
      this.locals = this.locals || {};
      this.locals.db = this.locals.db || db;
      this.locals.models = this.locals.models || models;
      this.locals.body = this.locals.body || {};
      this.locals.lang = this.locals.lang || (typeof this.query.lang !== 'undefined') ? this.query.lang : 'en';
      this.locals.messages = this.locals.messages || new Messages(this.locals.lang);
      this.locals.querystringParameters = this.locals.querystringParameters || Qs.parse(this.querystring);
      this.locals.queryOperators = this.locals.queryOperators || new Operators(this.locals.querystringParameters);
      yield next;
    });

    app.use(auth(configuration.global.http.auth));
    app.use(authSetup());
  }

  // app.use(session({
  //   store: redisStore({
  //     host: 'session.7vappv.ng.0001.euw1.cache.amazonaws.com',
  //     port: 6379
  //   })
  // }));
  //
  // app.use(passport.initialize());
  // app.use(passport.session());
  //
  // app.use(function *(next) {
  //   this.session.name = 'koa-redis';
  //   yield next;
  // });

  app.use(serve('build/' + configuration.application));

  app.use(function* (next) {
    this.locals.defaults = {
      application: configuration.global.applications[configuration.application],
      url: {
        assets: configuration.local.url.assets
      },
      lang: 'en',
      year: this.locals.date.getFullYear(),
      title: 'I Love Stage',
      description: 'ILOVESTAGE is a great way to find tickets for the top 10 West End shows at affordable group-rate prices.',
      preview: false
    };

    this.locals.defaults.preview = this.query.preview;

    yield next;
  });

  app.use(body());
  app.use(router(app));

  if (routes) {
    var routesArray = routes.toArray();
    for (var i = 0; i < routesArray.length; i++) {
      try {
        // throw 'thrown message';
        var noun = new routesArray[i](configuration, router, db, models, render);
        app.use(mount('/' + noun.name, noun.middleware()));
      } catch (error) {
        console.log(error, routesArray[i]);
      }
    }
  }

  app.use(conditional());
  app.use(gzip());
  app.use(fresh());
  app.use(etag());

  if (configuration.environment === 'development') {
    app.use(require('koa-browser-sync')()); // The snippet must be provide by BROWSERSYNC_SNIPPET environment variable
  }

  return app;
};