Skip to content

Commit

Permalink
Finished Docker Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekTBrown committed Jun 30, 2016
1 parent 3b7fd68 commit fc58ce8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
7 changes: 6 additions & 1 deletion examples/docker/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// Import RedRouter Core
var redrouter = require('../../').create;
var fs = require('fs');

// Import RedRouter Components
var backend_etcd = require('redrouter.backend.etcd');
Expand All @@ -15,6 +16,10 @@ var middleware_docker = require('redrouter.middleware.docker');
Define a RedRouter Instance
*/
var proxy = new redrouter({
ssl:{
key: fs.readFileSync('local/host.key'),
cert: fs.readFileSync('local/host.key.pub')
},
backend : {
constructor: backend_etcd,
options: {
Expand All @@ -34,7 +39,7 @@ var proxy = new redrouter({
middleware: [
{ constructor: middleware_docker,
options: {
docker_url: "http://localhost.com/2375"
docker_url: "tcp://localhost:2375"
}
}
],
Expand Down
27 changes: 16 additions & 11 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,22 @@ RedRouter.create = function(opts){
res.resolve(req, opts.resolver_opts, cb1);
},
function(record){
async.forEachOf(_this.middleware, function(mdw, priority2, cb2){
if(typeof opts.middleware_opts === "undefined"){
opts.middleware_opts = {};
}

opts.middleware_opts.priority = priority2;
mdw.resolve(record, opts.middleware_opts, cb2);

}, function(route){
callback(route);
});
if (record != null){
async.forEachOf(_this.middleware, function(mdw, priority2, cb2){
if(typeof opts.middleware_opts === "undefined"){
opts.middleware_opts = {};
}

opts.middleware_opts.priority = priority2;
mdw.resolve(record, opts.middleware_opts, cb2);

}, function(route){
callback(route);
});
}
else{
callback(null);
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/middleware/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Docker Middleware for RedRouter
The Docker Middleware module for RedRouter allows you to create records using container ids as opposed to host names; the host names will be injected automatically at resolution. Simply create a `container_id` field in a given record, and a `host` field will be dynamically generated. NOTE: having the host set will override this module; so do not set the host statically if you wish to use this module.
22 changes: 14 additions & 8 deletions lib/middleware/docker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var method = Docker.prototype = Object.create( _super );
function Docker(redrouter, opts){
_super.constructor.apply(this, arguments);

// Save Redrouter instance
this.backend = redrouter.backend;
// Save log
this.log = redrouter.log;

// Start Dolphin Instance
this.dolphin = new Dolphin(opts.docker_url);
Expand All @@ -34,19 +34,25 @@ function Docker(redrouter, opts){
call with route if resolved
**/
method.resolve = function(record, opts, callback){
var _this = this;

// Check if Docker Record
if (typeof record.docker_container !== "undefined" && typeof record.host === "undefined"){

// Query Existing Docker Containers for
this.dolphin.inspect(record.docker_container).then(function(container){
_this.dolphin.containers.inspect(record.docker_container).then(function(container){

// Set IP Address
record.host = container.Container.NetworkSettings.IPAddress;

// Pass Record
callback(record);

record.host = container.NetworkSettings.IPAddress;
if (typeof record.host !== "undefined"){
callback(record);
}
else{
callback(null);
}

}).catch(function(err){
_this.log.warn(err);
});

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "modular reverse proxy for nodejs",
"main": "index.js",
"scripts": {
"install": "./install.sh",
"key-gen": "./install.sh",
"test": "./node_modules/mocha/bin/mocha"
},
"repository": {
Expand Down

0 comments on commit fc58ce8

Please sign in to comment.