Skip to content

Instantly share code, notes, and snippets.

@jmervine jmervine/app.js
Last active Aug 29, 2015

Embed
What would you like to do?
'use strict';
var express = require('express');
var MaxCDN = require('maxcdn');
// Initialize maxcdn from environment.
var maxcdn = new MaxCDN(process.env.ALIAS,
process.env.KEY,
process.env.SECRET);
// For this example, I'll restrict to a single zone,
// however, it wouldn't be hard to adapt this for
// multiple zones.
var zoneid = 141691;
var app = express();
// A little logging gose a long way.
app.use(express.logger('dev'));
// Add Jade
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// Load static files. here, I have none, but you should
// put your javascript and css in /public and include this.
//
app.use(express.static(__dirname + '/public'));
// main route
app.get('/?', function(req, res) {
maxcdn.get('reports/popularfiles.json', function(err, data) {
if (err) {
console.trace(err);
res.send(500, err);
return;
}
res.render('index', { popularfiles: data.data.popularfiles });
});
});
// purge zone cache route
app.get('/purge/?', function(req, res) {
maxcdn.delete('zones/pull.json/'+zoneid+'/cache', function(err, data) {
if (err) {
console.trace(err);
res.send(500, err);
return;
}
res.send(data.code);
});
});
// purge file cache route
app.get('/purge/*', function(req, res) {
var files = { files: [ '/'+req.params ] };
maxcdn.delete('zones/pull.json/'+zoneid+'/cache', files, function(err, data) {
if (err) {
console.trace(err);
res.send(500, err);
return;
}
res.send(data.code);
});
});
app.listen(3333, function(err) {
if (err) {
console.trace(err);
} else {
console.log('Started on port 3333!');
}
});
doctype html5
html
head
title MaxCDN Popularfiles
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1')
link(href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/united/bootstrap.min.css", rel="stylesheet")
script(src='http://code.jquery.com/jquery-2.1.0.min.js')
script.
$(window).load(function() {
$('button').each(function() {
//- Add click event to all buttons.
$(this).click(function() {
//- Build purge url string from button value.
var url = '/purge'+$(this).attr('value');
var that = $(this);
//- Add a simple text spinner while pruging.
var spinner = [ ' | ', ' / ', ' --- ', ' \\ ' ];
var spinIdx = 0;
function ticker() {
if (spinIdx == spinner.length) {
spinIdx = 0;
}
that.html(spinner[spinIdx++]);
}
ticker();
var interval = setInterval(function() {
ticker();
}, 250);
// Update button style.
that.removeClass('btn-primary');
that.addClass('btn-warning');
that.addClass('disabled');
// Sumbit Ajax purge request.
$.ajax(url)
.always(function() {
// On pass or fail, cleanup.
clearInterval(interval);
that.removeClass('disabled');
that.removeClass('btn-warning');
})
.done(function() {
// On success, notify.
that.html('Complete');
that.addClass('btn-success');
})
.fail(function() {
// On error, notify.
that.html('ERROR!');
that.addClass('btn-danger');
});
});
});
});
body
.navbar.navbar-default.navbar-static-top
.navbar-header
a.navbar-brand(href="/") MaxCDN Popularfiles
.container
.table-responsive
table.table.table-striped
thead
tr
th File
th Hits
th(width=100)
button.btn.btn-primary(value='') Purge All
tbody
- for entry in popularfiles
tr
td= entry.uri
td= entry.hit
td
button.btn.btn-primary(value=entry.uri) Purge
footer
center
p Powered by 
a(href="http://maxcdn.com") MaxCDN
{
"dependencies": {
"maxcdn": "^0.1.3",
"express": "^3.4.8",
"jade": "^1.1.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.