Skip to content

Commit 756e736

Browse files
committed
new example
1 parent 58d7ae0 commit 756e736

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var isGenerating = false;
2+
var isGenerated = false;
3+
4+
exports.install = function(framework) {
5+
framework.route('/', view_homepage);
6+
framework.file('sitemap.xml', file_xml);
7+
}
8+
9+
function view_homepage() {
10+
var self = this;
11+
self.plain(self.hostname('sitemap.xml'));
12+
}
13+
14+
function file_xml(req, res, validation) {
15+
16+
if (validation)
17+
return req.url === '/sitemap.xml';
18+
19+
var options = { hostname: req.hostname(), path: framework.path.public('sitemap.xml') };
20+
21+
// Is processed sitemap.xml?
22+
if (isGenerated) {
23+
console.log('sitemap.xml -> cache');
24+
framework.responseStatic(req, res);
25+
return;
26+
}
27+
28+
// Handle multiple requests
29+
// [isGenerating === true] the request must wait
30+
if (isGenerating) {
31+
setTimeout(function() {
32+
file_xml(req, res, validation);
33+
}, 1000);
34+
return;
35+
}
36+
37+
isGenerating = true;
38+
39+
console.log('sitemap.xml -> creating');
40+
var worker = framework.worker('sitemap', 'sitemap', 5000);
41+
42+
// Send settings
43+
worker.send(options);
44+
45+
// Handle exit
46+
worker.once('exit', function() {
47+
48+
console.log('sitemap.xml -> created');
49+
50+
isGenerating = false;
51+
isGenerated = true;
52+
53+
framework.responseStatic(req, res);
54+
});
55+
56+
}

‎xml-sitemap-workers/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var framework = require('total.js');
2+
var http = require('http');
3+
var debug = false;
4+
5+
framework.run(http, debug);

‎xml-sitemap-workers/public/empty‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE THIS FILE
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Load dependencies
2+
var fs = require('fs');
3+
var utils = require('total.js/utils');
4+
5+
// Worker settings
6+
var options = { path: '', hostname: '' };
7+
8+
process.on('message', function(data) {
9+
options = data;
10+
createXML();
11+
});
12+
13+
function createXML() {
14+
15+
var writer = fs.createWriteStream(options.path);
16+
17+
var write = function(url, lastmod, priority, changefreq) {
18+
var str = '<url><loc>' + url.encode() + '</loc><lastmod>' + lastmod + '</lastmod><changefreq>' + changefreq + '</changefreq><priority>' + priority + '</priority></url>';
19+
writer.write(str);
20+
};
21+
22+
var hostname = options.hostname;
23+
var lastmod = new Date().format('yyyy-MM-dd');
24+
25+
writer.write('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.google.com/schemas/sitemap/0.84">', 'utf8');
26+
27+
// Many items
28+
write(hostname + '/', lastmod, '1.0000', 'weekly');
29+
30+
for (var i = 0; i < 1000; i++)
31+
write(hostname + '/products/' + i, lastmod, '0.3', 'monthly');
32+
33+
// Complete
34+
writer.end('</urlset>');
35+
36+
writer.on('finish', function() {
37+
// Done
38+
process.exit();
39+
});
40+
41+
}

0 commit comments

Comments
 (0)