Permalink
Browse files

[rewrite] move support to node 0.7 cluster

This add support for the node 0.7 core cluster module.
Unfortunaltly there is no backward compatibility with node 0.6 or
node 0.4.

The changes from a user perspective are primally that cluster-vhost
is a module and not a plugin, since the core module have no plugin
interface.
The config.json is almost the same with some few changes, the most
important is that transparant-proxy isn't support, but it will be
in the future.

The roadmap is that a test suite should be added to the submodules
there don't have one, then this module will get one too. After that
transparant-proxy will be added followed by an CLI interface.

Please report any bugs there might be :)
  • Loading branch information...
2 parents f596bdf + f605531 commit b8ab58235fb41fb1a936e2f22ec95c44b0f13de9 @AndreasMadsen committed Mar 14, 2012
Showing with 1,040 additions and 1,326 deletions.
  1. +2 −6 .gitignore
  2. +48 −109 README.md
  3. +3 −0 bin/vhost
  4. 0 lib/cli.js
  5. +46 −0 lib/common.js
  6. +99 −0 lib/core/config.js
  7. +94 −0 lib/core/create.js
  8. +80 −0 lib/core/inform.js
  9. +99 −0 lib/helpers/save.js
  10. +127 −0 lib/helpers/state.js
  11. +73 −0 lib/helpers/tracker.js
  12. +0 −91 lib/http-proxy.js
  13. +0 −311 lib/internal.js
  14. +103 −0 lib/module.js
  15. +0 −741 lib/plugin.js
  16. +125 −0 lib/proxy/master.js
  17. +123 −0 lib/proxy/worker.js
  18. +0 −55 lib/server.js
  19. +18 −13 package.json
View
@@ -3,12 +3,8 @@
*.swp
*.tmp
-# Log files
-npm-debug.log
-test/*.log
+# ignore intercom files
+intercom/
# All node modules
node_modules/
-
-# All tcp sockets
-sock/
View
157 README.md
@@ -1,76 +1,51 @@
#cluster-vhost
-> cluster-vhost is a plugin to [cluster](http://learnboost.github.com/cluster/) there allow you to use as many virtual host domains as you which.
-
-### Support for node 0.6.x
-> This module is currently only made for node 0.4.x, since there in 0.6.x was created a native cluster module.
->
-> However the native cluster module isn't sophisticated enought to support easy vhost setup.
-> I have made a [pull request](https://github.com/joyent/node/pull/2038) to node.js there make the native cluster module
-> a real module in my opinion. But since it contains API changes it won't be pulled in node.js version 0.6.x.
-> So as it seams now cluster-vhost won't support the latest version of node.js until 0.8.x.
+> cluster-vhost is a module there allow you to use as many virtual host domains
+ as you which. It is often used in combination with the
+ [cluster](http://nodejs.org/docs/latest/api/cluster.html) module but it is
+ not required.
>
-> **However if you have a geat thought about how to support vhost now, please tell me**.<br>
+> This module require node 0.7.0-pre or higher since the 0.6.0 cluster module isn't sophisticated enough.
## Features
- Ridiculously easy to use
- Support http to http proxy
- Support https to http proxy
- - transparent proxy remoteAddress
##How to use
-**Install:**
+####Install:
```shell
-npm install cluster
npm install cluster-vhost
```
-**Using:**
+####Use:
```javascript
-var cluster = require('cluster');
- cluster.vhost = require('cluster-vhost');
-
-cluster('./app')
- .use(cluster.vhost('example.org'))
- .on("vhost configured", function () {
- console.log("You can now access your app, by opening http://example.org:8001 in your browser");
- })
- .listen(3000);
+var cluster = require('cluster'),
+ vhost = require('cluster-vhost');
+
+vhost.use('example.org');
+vhost.on('done', function () {
+ console.log("You can now access your app, by opening http://example.org:8001 in your browser");
+});
+vhost.on('error', function (err) {
+ console.error('not good');
+ throw err;
+});
+
+//Start cluster
+cluster.fork();
```
-## Detailed instructions
-
-**First: install**<br>
-You will need to install `cluster` if you haven't already done so
+####Done:
-```shell
-npm install cluster
-```
+You can now access your site on `http://example.org:8001`. Note that if you
+started your application as root you can access using `http://example.org`.
-**Second: install cluster-vhost**<br>
-Now you are ready to install `cluster-vhost`
-
-```shell
-npm install cluster-vhost
-```
+##Configure host file
-**Third: use plugin**<br>
-Create a server.js file where, in this file you require both `cluster` and `cluster-vhost`.
-To setup vhost use the `cluster.vhost` function which takes the hostname as it's single argument.
-
-```javascript
-var cluster = require('cluster');
- cluster.vhost = require('cluster-vhost');
-
-cluster('./app')
- .use(cluster.vhost('example.org'))
- .listen(3000);
-```
-
-**Fourth: edit your hosts file**<br>
*You will need to edit you [hosts](http://en.wikipedia.org/wiki/Hosts_file) file to redirect example.org to you own computer.*
First open the file in you text editor.
@@ -86,27 +61,6 @@ localhost 127.0.0.1
example.org 127.0.0.1
```
-**Fifth: open your browser**
-
-You can now access your site on `http://example.org:8001`.<br>
-In order to access your site on `http://example.org`, you will need
-to set your firewall up to redirect from port `8001` to port `80`.
-
-##When is the proxy-server reaady
-When using `cluster-vhost`, `cluster` will emit a `vhost configured` event when everything is running and ready.
-
-```javascript
-var cluster = require('cluster');
- cluster.vhost = require('cluster-vhost');
-
-cluster('./app')
- .use(cluster.vhost('example.org'))
- .on("vhost configured", function () {
- console.log("cluster-vhost is ready");
- })
- .listen(3000);
-```
-
##Configure the proxy-server
###Creating the file<br>
@@ -117,9 +71,9 @@ step is to create it the in the right directory. This module will search
for `config.json` in its own directory and then go up in the folder tree
until it finds a `config.json` file.
-Example: if you have your websites in `~/Sites/` with `cluster` and `cluster-vhost`
-with in `~/Sites/node_modules`. `cluster-vhost` will search for `config.json` in
-the following directories:
+Example: if you have your websites in `~/Sites/` and typed `npm install cluster-vhost`
+here, you will have it in a `node_modules` folder. `cluster-vhost` will then search for
+a `config.json` file in the following directories:
```text
~/Sites/node_modules/cluster-vhost/config.json
@@ -128,26 +82,25 @@ the following directories:
~/config.json
```
-If you're using OSX, then in most cases you can assume search path will be `~/Sites/`.
-
####Alternative configuration method
-Instead of searching for `config.json` you can also configure the `proxy-server` by using the `module.config()`
+Instead of searching for `config.json` you can also configure the `proxy-server` by using the `vhost.config()`
method.
-`module.config()` method accepts a filepath or an object.
+`vhost.config()` method accepts a filepath or an object.
+
* If a filepath is given it must contain a valid json string.
* If a object is given it must contain a valid json object.
-Because this function is blocking you should only place it along with the `require` method with is also blocking.
-
```javascript
-var cluster = require("cluster");
- cluster.vhost = require("cluster-vhost").config("./vhost.config");
+var cluster = require("cluster"),
+ vhost = require("cluster-vhost");
-cluster('./app')
- .use(cluster.vhost('example.org'))
- .listen(3000);
+vhost.config('./config.json');
+vhost.use('example.org');
+
+//Start cluster
+cluster.autoFork();
```
###Writing the file
@@ -161,54 +114,40 @@ default value if not set.
{
//Because other modules may use a config.json file place you options insite a vhost obejct.
"vhost" : {
-
+
//You can specify on what the virtual host router should listen on.
//You can also set it to false if you don't want it to listen for http requests.
"http" : {
-
+
//By default it will listen for http requests on 127.0.0.1:8001
"port" : 8001,
"host" : "127.0.0.1",
-
+
//You can also use a unix path to listen on, but this will only work if no port property is set.
"path" : "./custom/unix/http.sock"
-
},
-
+
//The ssl property is a path to a directory containing a key.pem and a cert.pem file.
//By default the ssl property is set to undefined.
"ssl" : "./Sites/ssl/",
-
+
//If and only if a ssl property is set you can use https, else it will fallback to false.
- //You can in this object specify what the virtual host router should listen, just like the http object.
+ //You can in this object specify what the virtual host router should listen on, just like the http object.
"https" : {
-
+
//By default if the ssl property is set it will listen for https requests on 127.0.0.1:8002
"port" : 8002,
"host" : "127.0.0.1",
-
+
//Again you can also use a unix path
"path" : "./custom/unix/https.sock"
- },
-
- //In order to send new information to the route-table a unix socket is required.
- //This property is a path to a directory where a http.sock and https.sock will be created.
- //By default this folder will be made insite the custer-vhost module folder.
- "sock" : "./custom/sock/",
-
- //Because of the way the proxy-server route requests the remoteAdress property will not point
- //to the client but to the proxy-server (by default 127.0.0.1).
- //When transparantProxy is set to true it will modify the native http module so its remoteAdress
- //property point to the client. By default this it set to true, but if you don't which this
- //you can set it to false.
- "transparantProxy" : true
+ }
}
}
-```
+```
##Thanks to
-* cluster (LearnBoost) https://github.com/learnboost/cluster
* http-proxy-server (nodejitsu) https://github.com/nodejitsu/node-http-proxy
##License
View
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require("./../lib/cli.js");
View
No changes.
View
@@ -0,0 +1,46 @@
+/**
+ * Copyright (c) 2011 Andreas Madsen
+ * GPL License Version 3
+ */
+
+(function () {
+ "use strict";
+
+ //get modules
+ var path = require('path');
+
+ // is root detection
+ exports.isRoot = process.getuid() === 0;
+
+ //get the module root directory
+ exports.moduleRoot = path.join(path.dirname(module.filename), '/../');
+
+ //shortcut to library
+ exports.libraryDir = path.join(exports.moduleRoot, '/lib/');
+
+ //define library directorys
+ exports.helperDir = path.join(exports.libraryDir, '/helpers/');
+ exports.coreDir = path.join(exports.libraryDir, '/core/');
+ exports.proxyDir = path.join(exports.libraryDir, '/proxy/');
+
+ //define socket folder path
+ var filename = 'intercom-' + (exports.isRoot ? 'root' : 'user') + '.tcp';
+ exports.socketPath = path.join(exports.moduleRoot, '/intercom/' + filename);
+ exports.statePath = path.join(exports.moduleRoot, '/intercom/state.json');
+
+ //lazy load helpers
+ exports.helper = function (name) {
+ return require(path.join(exports.helperDir, name + '.js'));
+ };
+
+ //lazy load proxy
+ exports.proxy = function (name) {
+ return path.join(exports.proxyDir, name + '.js');
+ };
+
+ //lazy load core
+ exports.core = function (name) {
+ return require(path.join(exports.coreDir, name + '.js'));
+ };
+
+})();
Oops, something went wrong.

0 comments on commit b8ab582

Please sign in to comment.