-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
54 lines (47 loc) · 2.24 KB
/
Copy pathmain.js
File metadata and controls
54 lines (47 loc) · 2.24 KB
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
47
48
49
50
51
52
53
54
var HorariusHelper = require("./HorariusHelper");
var http = require('http');
var url = require("url");
var moment = require("moment");
var PORT= process.env.OPENSHIFT_NODEJS_PORT || 8080;
var host= process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
// Parameters
// TODO : Prendre groupe de tutorat en parametre (Si pas de groupe de tutorat, ne pas masquer tutorat)
// TODO : prendre en parametre si on enleve les multi-day events (Enlevé par defaut)
// TODO : HTTPS support
// TODO : Parse the calendars options so we can add a description or modify the name of the calendar
// TODO : Enleve tutorat inutile
// TODO : S'assurer de bien catcher les exceptions, le service ne doit pas crasher
// Create the HTTP server and use the function handleRequest has handler
var server = http.createServer(handleRequest);
// Start the server on PORT
server.listen(PORT, host, function(){
console.log("HorariusCleaner listening on Port : %s", PORT);
});
function handleRequest(req, res){
var parsedUrl = url.parse(req.url, true);
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(moment().format("MMMM Do YYYY, HH:mm:ss") + " : " + ip + " requested "+parsedUrl.href);
if(parsedUrl.pathname == "/") {
if (parsedUrl && parsedUrl.query && parsedUrl.query.cip) {
HorariusHelper.getCalendar(parsedUrl.query.cip, function (error, calendar) {
if (error) {
res.statusCode = 400;
console.log("Error Occured At "+new Date().toString() + ": ");
console.log(error);
res.end(error); // CIP is invalid or response received is invalid
} else {
// Set headers to force download of file
res.setHeader("Content-Type", "text/x-download;charset=UTF-8");
res.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+parsedUrl.query.cip+"_UdeS.ics");
res.end(calendar);
}
});
} else {
res.statusCode = 400;
res.end("Please provide a CIP.");
}
}else{
res.statusCode = 400;
res.end("This url is not allowed.");
}
}