forked from movableink/ics-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var http = require('http');
var url = require('url');
var fs = require('fs');
var underscore = require('underscore');
var template = underscore.template(fs.readFileSync('./ics.template').toString());
var port = process.env['PORT'] || 7033;
var host = process.env['HOST'] || 'ics.movableink-dmz.com';
http.createServer(function(req, res) {
var params = url.parse(req.url, true).query;
var options = {
host: host,
timezone: params.tz,
summary: params.summary,
description: params.description,
location : params.location,
name: params.name
};
options.startDate = formatDate(new Date(params.start));
options.endDate = params.end ? formatDate(new Date(params.end)) : options.startDate;
options.uid = (new Date()).getTime() + "@" + host;
options.now = formatDate(new Date());
var output = template(options);
res.writeHead(200, {'Content-Type': 'text/calendar'});
res.end(output);
}).listen(port);
console.log("Listening on port " + port + ", host " + host);
function formatDate(d) {
return d.getFullYear() + pad2(d.getMonth() + 1) + pad2(d.getDate()) + "T" + pad2(d.getHours()) + pad2(d.getMinutes()) + pad2(d.getSeconds());
}
function pad2(i) {
if(i < 10) {
return "0" + i;
} else {
return "" + i;
}
}