-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynomiter.js
executable file
·67 lines (57 loc) · 1.82 KB
/
dynomiter.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var restify = require("restify");
var server;
var dynomiter = (function () {
var constants = {
API_KEY: "", // add your Google API key
API_URL: "https://www.googleapis.com"
},
testUrl;
function log (msg) {
var date = new Date();
console.log(msg + " " + date.toTimeString() + " " + date.toDateString());
}
function googlePageSpeedTest (req, res, next) {
var url = "http://" + req.params.url,
client,
path = "/pagespeedonline/v1/runPagespeed?url=" + encodeURIComponent(url) +
"&key=" + constants.API_KEY +
"&strategy=desktop";
testUrl = url;
log("Testing " + url);
client = restify.createJsonClient({
url: constants.API_URL
});
client.get(path, function(err, req, res, data) {
if(data && data.formattedResults.ruleResults) {
checkGzip(data.formattedResults.ruleResults.EnableGzipCompression);
} else {
log("Testing failed");
}
});
res.send("Testing complete");
}
function checkGzip (gzipData) {
log("Checking for GZIP");
if(gzipData.urlBlocks !== undefined) {
log("GZIP not detected");
sendWarning("Warning: GZIP is currently off for " + testUrl);
}
}
function sendWarning (msg) {
log("Sending warning email");
// for my purpose I sent an email to ifttt
// and setup an SMS recipe for notifications
}
return {
test: googlePageSpeedTest,
log: log
};
}());
server = restify.createServer({
name: "Dynomiter"
});
server.listen(process.env.PORT || 7357);
server.get('/test/:url', dynomiter.test);
if(server) {
dynomiter.log("PageSpeed server started");
}