Permalink
Browse files

initial version

  • Loading branch information...
0 parents commit 55a8e3df5f6b3ecdfb3cc5229f53af574cc56323 @dan-manges committed Jun 4, 2011
@@ -0,0 +1,2 @@
+/node_modules
+
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2011 by Dan Manges
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,55 @@
+m2node
+======
+
+m2node is a mongrel2 handler for node
+
+install
+-------
+
+ npm install m2node
+
+example
+-------
+
+```javascript
+var http = require('http'),
+ m2node = require('m2node');
+
+var server = http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('Hello World\n');
+});
+
+m2node.run(server, {
+ send_spec: 'tcp://127.0.0.1:9996',
+ recv_spec: 'tcp://127.0.0.1:9997'
+});
+```
+
+configuration
+-------------
+
+The configuration is from the perspective of the handler, so the send_spec in your mongrel2 config should match the recv_spec in your node config.
+
+example - express
+-----------------
+
+here's an example of serving an app built using the [express framework](http://expressjs.com/) with m2node
+
+```javascript
+var express = require('express'),
+ m2node = require('m2node');
+
+var app = express.createServer();
+
+app.get('/', function (req, res) {
+ res.send('Hello World')
+});
+
+m2node.run(app, {
+ send_spec: 'tcp://127.0.0.1:9996'
+ recv_spec: 'tcp://127.0.0.1:9997'
+});
+```
+
+
@@ -0,0 +1,35 @@
+SPEC_DIR = File.dirname(__FILE__) + "/spec"
+
+task :default => %w[compile test]
+
+desc "compile coffee to js"
+task :compile do
+ sh "coffee -c examples lib"
+end
+
+desc "run the tests"
+task :test do
+ sh "vows spec/m2node_spec.js"
+end
+
+namespace :test do
+ namespace :app do
+ task :start => :compile do
+ sh "coffee spec/app.coffee"
+ end
+ end
+
+ namespace :m2 do
+ task :load do
+ Dir.chdir(SPEC_DIR) do
+ sh "m2sh load --config mongrel2.conf --db config.sqlite"
+ end
+ end
+
+ task :start => :load do
+ Dir.chdir(SPEC_DIR) do
+ sh "m2sh start -name m2node_tests --db config.sqlite"
+ end
+ end
+ end
+end
@@ -0,0 +1 @@
+config.sqlite
@@ -0,0 +1,14 @@
+express = require 'express'
+m2node = require 'm2node'
+
+app = express.createServer()
+
+app.get('/', (req, res) ->
+ res.send('Hello World')
+)
+
+m2node.run(
+ app,
+ send_spec: 'tcp://127.0.0.1:9996'
+ recv_spec: 'tcp://127.0.0.1:9997'
+)
@@ -0,0 +1,13 @@
+(function() {
+ var app, express, m2node;
+ express = require('express');
+ m2node = require('m2node');
+ app = express.createServer();
+ app.get('/', function(req, res) {
+ return res.send('Hello World');
+ });
+ m2node.run(app, {
+ send_spec: 'tcp://127.0.0.1:9996',
+ recv_spec: 'tcp://127.0.0.1:9997'
+ });
+}).call(this);
@@ -0,0 +1,2 @@
+*.log
+
@@ -0,0 +1,24 @@
+m2node_example = Handler(
+ send_spec = 'tcp://127.0.0.1:9997',
+ send_ident = '81b7114c-534c-4107-9f17-b317cfd59f62',
+ recv_spec = 'tcp://127.0.0.1:9996',
+ recv_ident = ''
+)
+
+localhost = Host(name = 'localhost', routes = {
+ '/': m2node_example
+})
+
+main = Server(
+ name = "m2node_examples",
+ port = 9000,
+ uuid = '5dc1fbe7-d9db-4602-8d19-80c7ef2b1b11',
+ access_log = "/logs/access.log",
+ error_log = "/logs/error.log",
+ chroot = ".",
+ default_host = "localhost",
+ pid_file = "/run/mongrel2.pid",
+ hosts = [localhost]
+)
+
+servers = [main]
@@ -0,0 +1,15 @@
+http = require 'http'
+m2node = require 'm2node'
+
+server = http.createServer((req, res) ->
+ console.log("#{req.method} #{req.url}")
+ res.writeHead(200, {'Content-Type': 'text/plain'})
+ res.end('Hello World\n')
+)
+
+m2node.run(
+ server,
+ send_spec: 'tcp://127.0.0.1:9996'
+ recv_spec: 'tcp://127.0.0.1:9997'
+)
+
@@ -0,0 +1,16 @@
+(function() {
+ var http, m2node, server;
+ http = require('http');
+ m2node = require('m2node');
+ server = http.createServer(function(req, res) {
+ console.log("" + req.method + " " + req.url);
+ res.writeHead(200, {
+ 'Content-Type': 'text/plain'
+ });
+ return res.end('Hello World\n');
+ });
+ m2node.run(server, {
+ send_spec: 'tcp://127.0.0.1:9996',
+ recv_spec: 'tcp://127.0.0.1:9997'
+ });
+}).call(this);
@@ -0,0 +1 @@
+*.pid
@@ -0,0 +1 @@
+
@@ -0,0 +1,14 @@
+{FakeSocket} = require './m2node/fake_socket'
+{Handler} = require './m2node/handler'
+
+exports.version = '0.1.0'
+
+exports.run = (server, options) ->
+ handler = new Handler(options)
+ handler.on 'request', (request) ->
+ fakeSocket = new FakeSocket()
+ fakeSocket.on 'write', ->
+ handler.sendResponse(request, fakeSocket.writeBuffer)
+ server.emit 'connection', fakeSocket
+ fakeSocket.emitData(request.toFullHttpRequest())
+
@@ -0,0 +1,19 @@
+(function() {
+ var FakeSocket, Handler;
+ FakeSocket = require('./m2node/fake_socket').FakeSocket;
+ Handler = require('./m2node/handler').Handler;
+ exports.version = '0.1.0';
+ exports.run = function(server, options) {
+ var handler;
+ handler = new Handler(options);
+ return handler.on('request', function(request) {
+ var fakeSocket;
+ fakeSocket = new FakeSocket();
+ fakeSocket.on('write', function() {
+ return handler.sendResponse(request, fakeSocket.writeBuffer);
+ });
+ server.emit('connection', fakeSocket);
+ return fakeSocket.emitData(request.toFullHttpRequest());
+ });
+ };
+}).call(this);
@@ -0,0 +1,28 @@
+sys = require 'sys'
+util = require 'util'
+events = require('events')
+
+class FakeSocket extends events.EventEmitter
+ constructor: ->
+ @writeBuffer = new Buffer('')
+ @writable = true
+
+ destroy: -> # noop
+ destroySoon: -> # noop
+
+ emitData: (buffer) ->
+ if (@_events && this._events['data'])
+ @emit('data', buffer)
+ if (@ondata)
+ @ondata(buffer, 0, buffer.length)
+
+ setTimeout: (timeout, callback) -> # noop
+
+ write: (data) ->
+ combinedBuffer = new Buffer(@writeBuffer.length + data.length)
+ @writeBuffer.copy(combinedBuffer)
+ combinedBuffer.write(data.toString(), @writeBuffer.length)
+ @writeBuffer = combinedBuffer
+ @emit('write')
+
+exports.FakeSocket = FakeSocket

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,27 @@
+events = require 'events'
+zeromq = require 'zeromq'
+
+{MongrelRequest} = require './mongrel_request'
+
+class Handler extends events.EventEmitter
+ constructor: (options) ->
+ @pullSocket = zeromq.createSocket('pull')
+ @pullSocket.connect(options.recv_spec)
+ @pullSocket.on 'message', (message) =>
+ @emit 'request', new MongrelRequest(message)
+
+ @pubSocket = zeromq.createSocket('pub')
+ @pubSocket.connect(options.send_spec)
+
+ sendResponse: (request, response) ->
+ header = [
+ request.uuid, ' ',
+ request.connectionId.length, ':', request.connectionId,
+ ', '
+ ].join('')
+ outBuffer = new Buffer(header.length + response.length)
+ outBuffer.write(header, 'ascii')
+ response.copy(outBuffer, header.length)
+ @pubSocket.send(outBuffer)
+
+exports.Handler = Handler

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 55a8e3d

Please sign in to comment.