Permalink
Browse files

Initial readme commit

  • Loading branch information...
1 parent d33941c commit fb899434914947a47c5c66fe0ddf15b8413c6646 Liam Oehlman committed Aug 13, 2012
Showing with 65 additions and 16 deletions.
  1. +49 −0 README.md
  2. +16 −16 lib/proxy.js
View
@@ -0,0 +1,49 @@
+## Premise
+This library allows for the generation of a nginx config file from a set of templates. For each endpoint e.g /test there can be many servers attached to it. There can also be many endpoints defined.
+
+Nginx should be build from source for use with this library as the "-p" flag does not seem to work in the nginx ubuntu package.
+
+## Usage
+```
+var proxy = require('nginx-proxy'),
+ nginx = new proxy('/path/to/nginx/dir');
+```
+
+## Methods
+Will forward requests for localhost/test to localhost:8000/test
+```
+nginx.add(['localhost:8000'], 'test', function(err) {
+ console.log(err);
+});
+```
+Will forward request for localhost/test to localhost:8000/test and localhost:8001 via round-robin
+```
+nginx.add(['localhost:8000', 'localhost:8001'], 'test'), function(err) {
+ console.log(err);
+});
+----------------------------------------
+
+This will remove localhost:8000 from the /test endpoint
+```
+nginx.del(['localhost:8000'], 'test'), function(err) {
+ console.log(err);
+});
+```
+
+----------------------------------------
+
+After using nginx.add() or nginx.del() changes will be written to disk via rules.json
+
+----------------------------------------
+Generates the nginx config from the rules
+```
+nginx.update(function(err) {
+ console.log(err);
+});
+```
+Reloads nginx's config and thus applys all rules written from nginx.update()
+```
+nginx.reload(function(err) {
+ console.log(err);
+});
+```
View
@@ -1,7 +1,7 @@
-var handlebars = require('handlebars')
- , fs = require ('fs')
- , path = require('path')
- , exec = require('child_process').exec
+var handlebars = require('handlebars'),
+ fs = require ('fs'),
+ path = require('path'),
+ exec = require('child_process').exec;
var Nginx = module.exports = exports = function(dir) {
this.dir = dir;
@@ -21,8 +21,8 @@ function include(arr, item) {
// add the rule to the routes object
Nginx.prototype.add = function(hosts, source, callback) {
- var add = this
- , exists = false;
+ var add = this,
+ exists = false;
if (typeof callback == 'undefined') {
callback = function(){}
@@ -72,8 +72,8 @@ Nginx.prototype.add = function(hosts, source, callback) {
// remove the route form the rules object
Nginx.prototype.del = function(hosts, source, callback) {
- var del = this
- , exists = false;
+ var del = this,
+ exists = false;
if (typeof callback == 'undefined') {
callback = function(){}
@@ -111,18 +111,18 @@ Nginx.prototype.del = function(hosts, source, callback) {
// update the config on disk but don't restart it yet
Nginx.prototype.update = function(callback) {
- var upstream = fs.readFileSync(path.resolve(__dirname, '../', 'templates', 'upstream.tpl'), 'utf8')
- , location = fs.readFileSync(path.resolve(__dirname, '../', 'templates', 'location.tpl'), 'utf8')
- , upstreamTemplate = handlebars.compile(upstream)
- , locationTemplate = handlebars.compile(location)
- , routes = {
- 'route' : this.rules
- };
+ var upstream = fs.readFileSync(path.resolve(__dirname, '../', 'templates', 'upstream.tpl'), 'utf8'),
+ location = fs.readFileSync(path.resolve(__dirname, '../', 'templates', 'location.tpl'), 'utf8'),
+ upstreamTemplate = handlebars.compile(upstream),
+ locationTemplate = handlebars.compile(location),
+ routes = {
+ 'route' : this.rules
+ };
if (typeof callback == 'undefined') {
callback = function(){}
}
-
+ // no idea why this was here, maybe an older version of handlebars required it?
//var file = template(routes);
fs.writeFileSync(path.join(this.dir, 'conf', 'upstream'), upstreamTemplate(routes));

0 comments on commit fb89943

Please sign in to comment.