Skip to content
Browse files

added auto port finding for the client. also added license and readme

  • Loading branch information...
1 parent 4f6fffd commit 56cea1bf9623ee4f962063c064cf2d2d4f6bd5c7 @mattinsler committed Dec 12, 2012
Showing with 108 additions and 18 deletions.
  1. +22 −0 LICENSE-MIT
  2. +39 −0 README.md
  3. +4 −1 bin/axle
  4. +10 −2 lib/axle_client.coffee
  5. +2 −1 lib/axle_server.coffee
  6. +31 −10 package.json
  7. +0 −2 test.coffee
  8. +0 −2 test.js
  9. 0 { → test}/test_client.coffee
View
22 LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Matt Insler
+
+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.
View
39 README.md
@@ -0,0 +1,39 @@
+# axle
+
+An effortless HTTP reverse proxy for node
+
+## Installation
+```
+npm install -g axle
+```
+
+## Usage
+
+Run the axle server. This should run on the port you want all your web traffic to pass through.
+
+```bash
+$ PORT=8000 axle
+```
+
+or
+
+```bash
+$ sudo PORT=80 axle
+```
+
+Then run your node servers the way you usually would, but with `axle` before them.
+
+```bash
+$ axle caboose server
+```
+
+To specify a domain or multiple domains you would like to route to this server, use the `AXLE_DOMAINS` environment variable.
+This is a comma separated list of domains.
+
+```bash
+$ AXLE_DOMAINS=*.mattinsler.com,code.mattinsler.dev axle caboose server
+```
+
+## License
+Copyright (c) 2012 Matt Insler
+Licensed under the MIT license.
View
5 bin/axle
@@ -24,7 +24,10 @@ function start_coffee(args) {
function start_script(original_script, args) {
var script = original_script;
if (script.indexOf('/') !== 0) { script = process.cwd() + '/' + script; }
- try { script = fs.readlinkSync(script); } catch(e) {}
+ try {
+ var link = fs.readlinkSync(script);
+ script = link[0] !== '.' ? link : require('path').dirname(script) + '/' + link;
+ } catch(e) {}
var content = fs.readFileSync(script, 'utf8');
content = content.slice(content.indexOf('\n') + 1);
View
12 lib/axle_client.coffee
@@ -2,12 +2,14 @@ require 'colors'
net = require 'net'
http = require 'http'
+portfinder = require 'portfinder'
class AxleClient
constructor: (@server) ->
@log = -> console.log '[' + 'axle'.cyan + '] ' + arguments[0]
@server.once('listening', => @on_server_listening())
+ @server.on('error', => @on_server_error(arguments...))
send: (command, data) ->
@client.write(JSON.stringify($c: command, $d: data))
@@ -18,8 +20,15 @@ class AxleClient
@client.on(event, => @['on_' + event](arguments...))
on_server_listening: ->
+ @log 'Listening on port ' + @server.address().port.toString().green
@connect()
+ on_server_error: (err) ->
+ if err.code is 'EADDRINUSE'
+ portfinder.getPort (e, port) =>
+ return console.error(e.stack) if e?
+ @server.listen(port)
+
on_connect: ->
@send('register', AxleClient.DOMAINS.map (d) => {host: d, endpoint: @server.address().port})
@log 'Listening on ' + AxleClient.DOMAINS.map((d) -> d.green).join(', ')
@@ -31,7 +40,7 @@ class AxleClient
setTimeout (=> @connect()), 1000
on_error: (err) ->
- @log 'ERROR: ' + err.code.red
+ @log 'Error connecting to axle: ' + err.code.red
on_data: ->
console.log 'data'
@@ -44,5 +53,4 @@ http.createServer = ->
new AxleClient(server)
server
-
module.exports = AxleClient
View
3 lib/axle_server.coffee
@@ -31,7 +31,8 @@ class Axle
@log = -> console.log '[' + 'axle'.cyan + '] ' + arguments[0]
- @server = require('http').createServer().listen(port)
+ @server = require('http').createServer().listen port, =>
+ @log 'Running on port ' + @server.address().port.toString().green
@routes = []
@distribute = require('distribute')(@server)
View
41 package.json
@@ -1,19 +1,40 @@
{
- "author": "Matt Insler <matt.insler@gmail.com> (www.mattinsler.com)",
"name": "axle",
"description": "",
- "version": "0.0.1",
+ "version": "0.0.2",
+ "homepage": "https://github.com/mattinsler/axle"
+ "author": {
+ "name": "Matt Insler",
+ "email": "matt.insler@gmail.com",
+ "url": "http://www.mattinsler.com"
+ },
"repository": {
"type": "git",
"url": "git@github.com:mattinsler/axle.git"
},
- "dependencies": {
- "coffee-script": "latest",
- "distribute": "latest",
- "colors": "latest"
- },
- "bin": {
- "axle": "./bin/axle"
+ "bugs": {
+ "url": "https://github.com/mattinsler/axle/issues"
},
- "main": "index"
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/mattinsler/axle/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "index",
+ "bin": {
+ "axle": "./bin/axle"
+ },
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "dependencies": {
+ "coffee-script": "latest",
+ "distribute": "latest",
+ "portfinder": "latest",
+ "colors": "latest"
+ },
+ "devDependencies": {
+ },
+ "keywords": []
}
View
2 test.coffee
@@ -1,2 +0,0 @@
-console.log 'Hello'
-console.log process.argv
View
2 test.js
@@ -1,2 +0,0 @@
-console.log('Hello');
-console.log(process.argv);
View
0 test_client.coffee → test/test_client.coffee
File renamed without changes.

0 comments on commit 56cea1b

Please sign in to comment.
Something went wrong with that request. Please try again.