Git Product home page Git Product logo

node-ember-cli-deploy-redis's Introduction

node-ember-cli-deploy-redis Build Status

ExpressJS middleware to fetch the current (or specified) revision of your Ember App deployed by ember-cli-deploy.

Why?

ember-cli-deploy is great. It allows you to run multiple versions in production at the same time and view revisions without impacting users. However, the example provided uses koa and many of us are not. This package allows you to easily fetch current and specified index.html revisions from redis with Express and other Node servers.

Usage

There are two main ways of using this library. For most simple Express servers, you'll want to simply use the middleware. However, if you need more flexibility, you'll want to use the internal fetch methods, with custom logic.

ExpressJS Middleware

  1. require the package
  2. use the package in your app

Example

var express = require('express');
var app = express();

var nodeEmberCliDeployRedis = require('node-ember-cli-deploy-redis');
app.use('/*', nodeEmberCliDeployRedis('myapp', {
  host: 'redis.example.org',
  port: 6929,
  password: 'passw0rd!',
  database: 0
}));

Custom Fetch Method

  1. require the package
  2. Use the fetchIndex method
  3. Render the index string as you wish.

Example

var express = require('express');
var app = express();

var fetchIndex = require('node-ember-cli-deploy-redis/fetch');

app.get('/', function(req, res) {
    fetchIndex(req, 'myapp', {
      host: 'redis.example.org',
      port: 6929,
      password: 'passw0rd!',
      database: 0
    }).then(function (indexHtml) {
    indexHtml = serverVarInjectHelper.injectServerVariables(indexHtml, req);
    res.status(200).send(indexHtml);
  }).catch(function(err) {
    res.status(500).send('Oh noes!\n' + err.message);
  });
});

Check out location-aware-ember-server for a running example.

Documentation

nodeEmberCliDeployRedis(appName, connectionInfo, options) (middleware constructor)

  • appName (required) - the application name, specified for ember deploy
    the keys in redis are prefaced with this name. For instance, if your redis keys are my-app:current, you'd pass my-app.
  • connectionInfo (required) - the configuration to connect to redis.
    internally, this library uses then-redis, so pass a configuration supported by then-redis. please see their README for more information.
  • options (optional) - a hash of params to override the defaults

fetchIndex(request, appName, connectionInfo, options)

Arguments

  • request (required) - the request object
    the request object is used to check for the presence of revisionQueryParam
  • appName (required) - the application name, specified for ember deploy
    the keys in redis are prefaced with this name. For instance, if your redis keys are my-app:current, you'd pass my-app.
  • connectionInfo (required) - the configuration to connect to redis.
    internally, this library uses then-redis, so pass a configuration supported by then-redis.
  • options (optional) - a hash of params to override the defaults Returns
  • a Promise
    when resolved, it returns the requested index.html string
    when failed, it returns an EmberCliDeployError.

options

  • revisionQueryParam (defaults to index_key)
    the query parameter to specify a revision (e.g. http://example.org/?index_key=abc123). the key will be automatically prefaced with your appName for security.

Testing

In order to facilitate unit testing and/or integration testing this library exports a mockable redis api. You will need to use a dependency injection framework such as rewire to activate this testing api.

Usage with rewire (mocha syntax)

// my-module.js
var fetchIndex = require('node-ember-cli-deploy-redis/fetch');
var indexWrapper = function(req, res) {
  return fetchIndex(req, 'app', {
    // real redis config
  }).then(function (indexHtml)) {
    // do something with index
  });
};
module.exports = indexWrapper;

// my-module-test.js
var redisTestApi = require('node-ember-cli-deploy-redis/test/helpers/test-api');
var fetchIndex = rewire('node-ember-cli-deploy-redis/fetch');
var redis = redisTestApi.ThenRedisClientApi;
var myModule = rewire('my-module');

describe('my module', function() {
  afterEach(function() {
    fetchIndex.__set__('_initialized', false);
  });

  it('grabs my content', function() {
    // inject mocked content
    myModule.__set__('fetchIndex', fetchIndex);
    fetchIndex.__set__('ThenRedis', redisTestApi.ThenRedisApi);
    redis.set('app:abc123', "<html><body><h1>hello test world</h1></body></html>");
    myModule(req, res).then(function(){
      // assertions here
    })
  });
});

Notes

  • Don't create any other redis keys you don't want exposed to the public under your appName.

Contributing

Comments/PRs/Issues are welcome!

node-ember-cli-deploy-redis's People

Contributors

blimmer avatar ronco avatar fklingler avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.