Skip to content

Commit

Permalink
v0.15.0
Browse files Browse the repository at this point in the history
+ Introducing app.host(location)
  • Loading branch information
adamhalasz committed Mar 5, 2017
1 parent c9eb53c commit 04242a4
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 30 deletions.
9 changes: 9 additions & 0 deletions controllers/host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict"
const utils = require('./utils')
module.exports = function(app, servers){
return function(location, options){
app.emit('host', { app: app, location: location, options: options })
let host = utils.getHost(location, options)
app.hosts[host.location.host] = app
}
}
15 changes: 9 additions & 6 deletions controllers/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,45 @@
// ===========================================================================
// Exports
// ===========================================================================

module.exports = function(app, servers){

return function(location, options, callback){
var callback = typeof options == 'function' ? options : callback ;
var options = !options || typeof options == 'function' ? {} : options ;
var protocolName = (typeof options == 'object' && options.cert || options.key || options.ca) ? 'https' : 'http' ;
var host = utils.getHost(location, options)
app.location = host.location
app.port = host.port

/*
// define location
if(!isNaN(location)) {
app.location = url.parse(protocolName+'://0.0.0.0:'+location);
} else if(typeof location == 'string') {
var location = location.indexOf('://') == -1 ? 'http://' + location : location ;
var location = location.indexOf('://') == -1 ? 'http://' + location : location ;
app.location = url.parse(location)
} else if(typeof location == 'object') {
app.location = location;
} else if(!utils.isset(location)){
app.location = url.parse(protocolName+'://0.0.0.0:80/');
}
} */

// define protocol
//var protocol = app.location.protocol === 'http:' ? require('http') : require('https') ;

// define port
app.port = app.location.protocol === 'http:' && (!options.cert && !options.key) ? (app.location.port || 80) : (app.location.port || 443) ;


// create route containers
app.routes = typeof app.routes != "undefined" ? app.routes : { get: [], post: [], options: [], put: [], patch: [], head: [], delete: [], trace: [], header: [], footer: [], missing: [], error: [] }

//console.log(isNaN(location), location, typeof location, app.location);
// define host
app.location.host = app.location.host.split(':')[1] ? app.location.host : app.location.host + ':' + app.port;
app.host = app.location.host.split(':')[0];
//app.host = app.location.host.split(':')[0];

// save host to hosts
app.hosts[app.location.host] = app
Expand Down
2 changes: 1 addition & 1 deletion controllers/protocols/http/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
var hostname = utils.isset(port) ? host : host + ':' + protocol.globalAgent.defaultPort ;

// get app (host controller) handling this hostname
var app = App.hosts[hostname] || App.hosts[hostname+':'+port] || App.hosts['0.0.0.0:'+port]
var app = App.hosts[hostname] || App.hosts[hostname+':'+port] || App.hosts['0.0.0.0:'+port] || App.hosts[port]

// if the app (host controller) exists and it has routes for this method
if(app && app.routes && app.routes[method]){
Expand Down
4 changes: 2 additions & 2 deletions controllers/protocols/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module.exports = function(app, options, callback){

// define http or https server
if(protocolName === 'http'){
var server = protocol.createServer(host).listen(app.port, '0.0.0.0', callback) ;
var server = protocol.createServer(host).listen(app.port, callback) ;

} else if (protocolName === 'https') {
var server = protocol.createServer(options, host).listen(app.port, '0.0.0.0', callback)
var server = protocol.createServer(options, host).listen(app.port, callback)

} else {
throw new Error('Cannot start a HTTPS server without Options');
Expand Down
2 changes: 1 addition & 1 deletion controllers/protocols/http/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
}
}
},
end : function(input, isLast){
end: function(input, isLast){
if(!signal.responded && !signal.stopped){
signal.responded = true
if(input && typeof input == 'object' || signal.header('x-requested-with') == 'XMLHttpRequest' || ( signal.header('authorization') && (signal.header('authorization').indexOf('Bearer') != -1 || signal.header('authorization').indexOf('Token') != -1 ))) {
Expand Down
35 changes: 33 additions & 2 deletions controllers/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
module.exports.isset = function isset(object){
var url = require('url')
var Utils = new Object()

Utils.isset = function isset(object){
return (object != "undefined" && object != undefined && object != null && object != "" && typeof(object) != 'undefined') ? true : false ;
}
}

Utils.getHost = function(inputLocation, options){
var options = !options || typeof options == 'function' ? {} : options ;
var protocolName = (typeof options == 'object' && options.cert || options.key || options.ca) ? 'https' : 'http' ;

// define location
if(!isNaN(inputLocation)) {
var location = url.parse(protocolName+'://0.0.0.0:'+inputLocation);

} else if(typeof inputLocation == 'string') {
var location = inputLocation.indexOf('://') == -1 ? 'http://' + inputLocation : inputLocation ;
location = url.parse(location)

} else if(typeof inputLocation == 'object') {
var location = inputLocation;

} else if(!Utils.isset(inputLocation)){
var location = url.parse(protocolName+'://0.0.0.0:80/');
}

var port = location.protocol === 'http:' && (!options || (!options.cert && !options.key))
? (location.port || 80)
: (location.port || 443) ;

return { port: port, location: location }
}

module.exports = Utils
19 changes: 19 additions & 0 deletions examples/host_proxy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var server = require('../../')
var request = require('request')

var proxy = server()
proxy.listen('test.local.com:3000')
proxy.header(function($){
request.get('http://test2.local.com:5000/', function(error, response, body){
if(error) throw error;
$.send('\nPort 3000 reached\n\nResponse from http://test2.local.com/:\n')
$.end(body)
});
})

var app = server()
app.listen('test.local.com:5000')
app.host('test2.local.com:5000')
app.get('/', function($){
$.end('Hello world!')
})
28 changes: 11 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ===========================================================================
// Diet v0.10
// Diet
// ===========================================================================

// ===========================================================================
Expand All @@ -11,17 +11,20 @@
const util = require('util');
const EventEmitter = require('eventemitter2').EventEmitter2;


// ===========================================================================
// Internal Dependencies
// ===========================================================================

const Protocol = require('./controllers/protocol')
const Listener = require('./controllers/listen')
const Host = require('./controllers/host')
const Router = require('./controllers/router')
const Resource = require('./controllers/resource')
const Construct = require('./controllers/construct')
const httpProtocol = require('./controllers/protocols/http')


// ===========================================================================
// Exports
// ===========================================================================
Expand All @@ -35,6 +38,7 @@
module.exports.emit = events.emit;
module.exports.onAny = events.onAny;


// ===========================================================================
// Server
// ===========================================================================
Expand Down Expand Up @@ -89,22 +93,11 @@
app.emit = events.emit;
app.onAny = events.onAny;


// -----------------------------------------------------------------------
// Attach HTTP Protocol Handler by default
// -----------------------------------------------------------------------
app.protocol('http', httpProtocol)


// -----------------------------------------------------------------------
// Observe Changes (coming soon...)
// -----------------------------------------------------------------------
/*
Object.observe(app, function(changes){
changes.forEach(function(change){
//console.log('server', change.name)
})
});*/

module.exports.emit('create', { path: path, options: options, app: app })


Expand Down Expand Up @@ -134,7 +127,7 @@
function App(path, options){

// -----------------------------------------------------------------------
// Variables
// Variables
// -----------------------------------------------------------------------

this.silent = options.silent
Expand All @@ -144,11 +137,11 @@
this.dir = this.path.match(/([^\/]*)\/*$/)[1]
this.hosts = hosts
this.protocols = new Array();
this.host = '0.0.0.0'
//this.host = '0.0.0.0'


// -----------------------------------------------------------------------
// Methods
// Methods
// -----------------------------------------------------------------------

this.get = new Router('get' , 'method' , this)
Expand All @@ -163,12 +156,13 @@
this.footer = new Router('footer' , 'api' , this)
this.missing = new Router('missing' , 'api' , this)
this.error = new Router('error' , 'api' , this)
this.resource = new Resource(this)
this.resource = new Resource(this)
this.model = new Construct('model', 'models', this)
this.view = new Construct('view', 'views', this)
this.controller = new Construct('controller', 'controllers', this)
this.protocol = new Protocol(this)
this.listen = new Listener(this, servers)
this.host = new Host(this, servers)


// -----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diet",
"version": "0.14.1",
"version": "0.15.0",
"description": "A tiny, fast and modular node.js web framework. Good for making fast & scalable apps and apis.",
"homepage": "http://dietjs.com/",
"keywords": [
Expand Down
38 changes: 38 additions & 0 deletions tests/host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var server = require('../');
var assert = require('assert');
var request = require('request');
var subject = 'Test'.cyan+' → '.grey+ 'Events'.yellow + ': '.grey;

describe(subject + 'initialize', function(){


var proxy = server()
proxy.listen('test.local.com:9079')
proxy.header(function($){
request.get('http://test2.local.com:9078/', function(error, response, body){
if(error) throw error;
assert.equal(response.headers['content-type'], 'text/plain');
assert.equal(response.statusCode, 200);
assert.equal(body, 'PROXY_HOST_TEST');
$.end(body)
});
})

var app = server()
app.listen('test.local.com:9078')
app.host('test2.local.com:9078')
app.get('/', function($){
$.end('PROXY_HOST_TEST')
})

it('should test app.host()', function(done){

request.get('http://test.local.com:9079/', function(error, response, body){
if(error) throw error;
assert.equal(body, 'PROXY_HOST_TEST');
assert.equal(response.headers['content-type'], 'text/plain');
assert.equal(response.statusCode, 200);
done();
});
});
})

0 comments on commit 04242a4

Please sign in to comment.