Skip to content

Commit 4285b30

Browse files
committed
Initial Commit
0 parents  commit 4285b30

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# nginx-cache-purge
3+
4+
* Gets a list of urls from one or more sitemaps.
5+
* Send a PURGE request to list url's found
6+
7+
# Install / Running
8+
9+
## From npm
10+
11+
npm install -g nginx-cache-purge
12+
13+
## Run from git clone
14+
15+
git clone https://guthub.com/mistralol/nginx-cache-purge
16+
cd nginx-cache-purge && npm install
17+
node index.js http://www.example.com/sitemap.xml
18+
19+
## Server configuration
20+
21+
In order for this to work properly nginx must have the proxy_cache_purge configured correctly. If it does not then this program will not be able to purge the cache. Typical system like debian / ubuntu have this by default when using the nginx-extras package. It has not currently been tested with any other type of server.
22+
23+
# Additional ideas / roadmap / todo list
24+
25+
* Needs support for more input formats
26+
1. stdin
27+
2. json format
28+
3. other xml formats
29+
4. Read a flat text format
30+
* Needs limited pipelined request support aka run multiple concurrent requests
31+
* Optional send a GET (multiple?) to each url to get nginx to cache it again
32+
* Stats etc...
33+
* Better error reporting
34+
35+
For bugs / ideas please use the issue list on github
36+
37+
# Credits
38+
39+
Original Author: James Stevenson
40+
E-Mail: james@stev.org
41+
BLOG: https://www.stev.org
42+
43+

index.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'use strict;'
2+
var debug = false;
3+
4+
var fs = require('fs');
5+
var request = require('request');
6+
var xmlparse = require('xml-parser');
7+
var inspect = require('util').inspect;
8+
9+
var opt = require('node-getopt').create([
10+
['h', 'help', 'Print this help'],
11+
['d', 'debug', 'Debug mode. Print more messages'],
12+
['n', 'dry-run', 'Simulated - Does not actually send purge command'],
13+
['w', 'wait=ARG', 'Add a delay between requests in ms'],
14+
]).bindHelp();
15+
16+
var args = opt.parse(process.argv.slice(2));
17+
18+
if (args["options"]["debug"])
19+
debug = true;
20+
21+
if (args["options"]["wait"] == undefined)
22+
args["options"]["wait"] = 0;
23+
24+
if (debug)
25+
console.log(args);
26+
27+
//Do the purge
28+
function DoPurge(origin, urls) {
29+
console.log('Remaining:', urls.length);
30+
if (urls.length == 0) {
31+
console.log('Completed Purge', origin);
32+
return;
33+
}
34+
35+
if (args["options"]["dry-run"]) {
36+
var url = urls.pop();
37+
setTimeout(function() {
38+
console.log('Purge Completed URL:', url, 'PURGED');
39+
DoPurge(origin, urls);
40+
}, args["options"]["wait"]);
41+
return;
42+
}
43+
44+
var url = urls.pop();
45+
request({
46+
uri : url,
47+
method: "PURGE",
48+
timeout: 10000
49+
}, function(err, res, body) {
50+
if (err) {
51+
console.log('Purge Failed URL:', url, 'Error:', err);
52+
} else {
53+
console.log('Purge Completed URL:', url, 'PURGED:', res.statusCode);
54+
}
55+
if (args["options"]["wait"] > 0) {
56+
setTimeout(function() { DoPurge(origin, urls); }, args["options"]["wait"]);
57+
} else {
58+
DoPurge(origin, urls);
59+
}
60+
});
61+
}
62+
63+
64+
//Convert xml to list of url's
65+
function ParseSiteMap(origin, data) {
66+
var obj = xmlparse(data);
67+
var urls = [];
68+
var root = obj['root'];
69+
var urlset = root['children'];
70+
for(var i=0;i<urlset.length;i++) {
71+
var info = urlset[i];
72+
urls.push(info['children'][0]['content']);
73+
}
74+
return urls;
75+
}
76+
77+
for(var i=0;i<args['argv'].length;i++) {
78+
var url = args['argv'][i];
79+
console.log("Trying:", url);
80+
81+
//Covers both http and https
82+
if (url.startsWith('http')) {
83+
console.log('Fetching:', url);
84+
request(url, function (error, res, body) {
85+
if (error) {
86+
console.log('Failed to get:', url);
87+
} else {
88+
var urls = ParseSiteMap(url, body);
89+
DoPurge(url, urls);
90+
}
91+
});
92+
} else {
93+
console.log('Reading:', url);
94+
try {
95+
var body = fs.readFileSync(url, 'utf8');
96+
ParseSiteMap(url, body);
97+
} catch(err) {
98+
console.log('Cannot open file:', url, ' Error:', err);
99+
}
100+
}
101+
}
102+
103+

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "nginx-cache-purge",
3+
"version": "1.0.0",
4+
"description": "Sends PURGE requests to nginx",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"nginx",
11+
"purge"
12+
],
13+
"author": "James Stevenson",
14+
"license": "ISC",
15+
"repository": {
16+
"type": "git",
17+
"url": "git://github.com/mistralol/nginx-cache-purge.git"
18+
},
19+
"dependencies": {
20+
"node-getopt": "^0.2.3",
21+
"request": "^2.81.0",
22+
"xml-parser": "^1.2.1"
23+
}
24+
}

0 commit comments

Comments
 (0)