Permalink
Browse files

Changed to include upstream and locations in nginx config

  • Loading branch information...
1 parent 04d7050 commit 3ab4f99d99b00cf92bb67e7290dacc10408c8f95 Liam Oehlman committed Feb 15, 2012
Showing with 139 additions and 36 deletions.
  1. +4 −2 .gitignore
  2. +37 −14 lib/proxy.js
  3. +5 −0 templates/location.tpl
  4. +6 −0 templates/upstream.tpl
  5. +4 −0 test/files/location
  6. +7 −0 test/files/upstream
  7. +50 −0 test/nginx/conf/nginx.conf
  8. +26 −20 test/routes.js
View
@@ -1,5 +1,7 @@
node_modules
test/nginx/logs/*.log
-test/nginx/conf/*.conf
+test/nginx/conf/upstream
+test/nginx/conf/location
+test/demo.js
+test/nginx/rules.json
-## keep these
View
@@ -5,7 +5,14 @@ var handlebars = require('handlebars')
var Nginx = module.exports = exports = function(dir) {
this.dir = dir;
- this.rules = []
+ this.rules = [];
+ this.conf = path.join(dir, 'rules.json');
+
+ if (!path.existsSync(this.conf)){
+ fs.writeFileSync(this.conf, '');
+ } else {
+ this.rules = JSON.parse(fs.readFileSync(this.conf));
+ }
}
function include(arr, item) {
@@ -17,6 +24,10 @@ Nginx.prototype.add = function(hosts, source, callback) {
var add = this
, exists = false;
+ if (typeof callback == 'undefined') {
+ callback = function(){}
+ };
+
for (var ii = 0; ii < add.rules.length; ii++) {
// flag if there is already an entry fro the new route
if (add.rules[ii].source === source) {
@@ -40,7 +51,7 @@ Nginx.prototype.add = function(hosts, source, callback) {
});
} // if
} // for
-
+ fs.writeFileSync(this.conf, JSON.stringify(this.rules));
callback();
} else {
for (ii = 0; ii < add.rules.length; ii++) {
@@ -54,6 +65,7 @@ Nginx.prototype.add = function(hosts, source, callback) {
});
} // if
} // for
+ fs.writeFileSync(this.conf, JSON.stringify(this.rules));
callback();
} // if...else
} // add
@@ -62,6 +74,10 @@ Nginx.prototype.add = function(hosts, source, callback) {
Nginx.prototype.del = function(hosts, source, callback) {
var del = this
, exists = false;
+
+ if (typeof callback == 'undefined') {
+ callback = function(){}
+ };
for (var ii = 0; ii < del.rules.length; ii++) {
// flag if there is already an entry fro the new route
@@ -88,33 +104,40 @@ Nginx.prototype.del = function(hosts, source, callback) {
} // if
} // if
} // for
-
- callback(null);
+ fs.writeFileSync(this.conf, JSON.stringify(this.rules));
+ callback();
} // if...else
} // del
// update the config on disk but don't restart it yet
Nginx.prototype.update = function(callback) {
- var source = fs.readFileSync(path.resolve(__dirname, '../', 'templates', 'nginx.tpl'), 'utf8')
- , template = handlebars.compile(source)
+ 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(){}
+ }
+
var file = template(routes);
- fs.writeFile(path.join(this.dir, 'conf', 'nginx.conf'), file, function(err) {
- if (err) {
- callback(err);
- } else {
- callback(null);
- }
- })
+ fs.writeFileSync(path.join(this.dir, 'conf', 'upstream'), upstreamTemplate(routes));
+ fs.writeFileSync(path.join(this.dir, 'conf', 'location'), locationTemplate(routes));
+
+ callback();
} // update
Nginx.prototype.reload = function(callback) {
var pid = fs.readFileSync(path.join(this.dir, 'logs', 'nginx.pid'));
+ if (typeof callback == 'undefined') {
+ callback = function(){}
+ };
+
if (!pid) {
callback(new Error('Nginx not running or pid file not found'));
} else {
@@ -123,7 +146,7 @@ Nginx.prototype.reload = function(callback) {
if (err) {
callback(err);
} else {
- callback(null);
+ callback();
} // if...else
});
} // if...else
View
@@ -0,0 +1,5 @@
+{{#each route}}
+location /{{this.source}} {
+ proxy_pass http://{{this.source}}/{{this.source}};
+}
+{{/each}}
View
@@ -0,0 +1,6 @@
+{{#each route}}
+upstream {{this.source}} {
+ {{#each this.hosts}}server {{this}};
+ {{/each}}
+}
+{{/each}}
View
@@ -0,0 +1,4 @@
+
+location /test {
+ proxy_pass http://test/test;
+}
View
@@ -0,0 +1,7 @@
+
+upstream test {
+ server localhost:8001;
+ server localhost:8002;
+ server localhost:8003;
+
+}
View
@@ -0,0 +1,50 @@
+
+#user nobody;
+worker_processes 1;
+
+#error_log logs/error.log;
+#error_log logs/error.log notice;
+#error_log logs/error.log info;
+
+#pid logs/nginx.pid;
+
+
+
+
+events {
+ worker_connections 8080;
+}
+
+
+http {
+ include mime.types;
+ default_type application/octet-stream;
+
+ #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
+ # '$status $body_bytes_sent "$http_referer" '
+ # '"$http_user_agent" "$http_x_forwarded_for"';
+
+ #access_log logs/access.log main;
+
+ sendfile on;
+ #tcp_nopush on;
+
+ #keepalive_timeout 0;
+ keepalive_timeout 65;
+
+ #gzip on;
+
+include upstream;
+
+ server {
+ listen 80;
+ server_name _;
+
+ #charset koi8-r;
+
+ #access_log logs/host.access.log main;
+include location;
+
+ }
+
+}
View
@@ -14,22 +14,22 @@ function include(arr, item) {
before(function() {
// replace the nginx config with a basic one
- var nginxConf = path.join(__dirname, 'nginx', 'conf', 'nginx.conf');
- var nginxStartup = fs.readFileSync(path.join(__dirname, 'files', 'nginx.startup'), 'utf8');
+ var upstream = path.join(__dirname, 'nginx', 'conf', 'upstream')
+ , location = path.join(__dirname, 'nginx', 'conf', 'location')
- fs.unlink(nginxConf, function(err) {
- if (err) {}
+ fs.unlinkSync(location);
+ fs.writeFileSync(location, '');
+ fs.unlinkSync(upstream);
+ fs.writeFileSync(upstream, '');
- fs.writeFileSync(nginxConf, nginxStartup, 'utf8');
- // start nginx
- exec('nginx -p ' + path.join(__dirname, '/nginx/'), function(err, stdout, stderr) {
- if (err) {
- console.log(err);
- }
- });
- // start the test server
- server = spawn('node', [path.join(__dirname, 'server', 'server.js')]);
+ exec('nginx -p ' + path.join(__dirname, '/nginx/'), function(err, stdout, stderr) {
+ if (err) {
+ console.log(err);
+ }
});
+ server = spawn('node', [path.join(__dirname, 'server', 'server.js')]);
+ nginx.rules = [];
+
});
after(function() {
@@ -81,14 +81,21 @@ describe('Methods', function() {
// TODO -- Use diff to determine if the generated config is diffenent to the one on file
nginx.update(function(err) {
if (err) {
+ console.log('err');
done();
} else {
- exec('diff -q ' + path.join(__dirname, '/nginx', 'conf', 'nginx.conf') + ' ' + path.join(__dirname, 'diff', 'nginx.conf') , function(err, stdout, stderr) {
- if (stdout) {
- done(new Error('Difference in expected and actual configs'));
+ exec('diff -q ' + path.join(__dirname, '/nginx', 'conf', 'upstream') + ' ' + path.join(__dirname, 'files', 'upstream') , function(err, stdout, stderr) {
+ if (err) {
+ done(err);
} else {
- done();
- }
+ exec('diff -q ' + path.join(__dirname, '/nginx', 'conf', 'location') + ' ' + path.join(__dirname, 'files', 'location') , function(err, stdout, stderr) {
+ if (err) {
+ done(err);
+ } else {
+ done();
+ }
+ });
+ } // if...else
});
}
});
@@ -97,7 +104,7 @@ describe('Methods', function() {
it('Should reload the nginx config to have the additional routes', function(done) {
var responses = ['server1', 'server2', 'server3'];
-
+
this.timeout(6000);
request('http://localhost', function(err, resp, body) {
if (err) {
@@ -127,6 +134,5 @@ describe('Methods', function() {
});
}
});
-
});
});

0 comments on commit 3ab4f99

Please sign in to comment.