Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled caching for /pebble #23

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"appcache-node": "^0.2.0",
"bower": "~1.3.2",
"dateformat": "^1.0.8-1.2.3",
"express": "^4.4.4",
"mongodb": "1.3.20",
"socket.io": "~0.9.16"
Expand Down
111 changes: 55 additions & 56 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,92 +20,45 @@
var patientData = [];
var now = new Date().getTime();
var fs = require('fs');
var dateformat = require('dateformat');
var express = require('express');
var appcache = require("appcache-node");
var mongoClient = require('mongodb').MongoClient;
var pebble = require('./lib/pebble');
var cgmData = [];
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// define helper functions
////////////////////////////////////////////////////////////////////////////////////////////////////
function errorHandler(err, req, res, next) {
if (err) {
// Log the error
var msg = "Error serving " + request.url + " - " + err.message;
sys.error(msg);
console.log(msg);

// Respond to the client
res.status(err.status);
res.render('error', { error: err });
}
}

function getAppCache(req) {
// Define the files you want the browser to cache
var hostname = req.headers.host;
return appcache.newCache([
'http://'+hostname+'/audio/alarm.mp3',
'http://'+hostname+'/audio/alarm2.mp3',
'http://'+hostname+'/css/dropdown.css',
'http://'+hostname+'/css/main.css',
'http://'+hostname+'/js/client.js',
'http://'+hostname+'/js/dropdown.js',
'http://'+hostname+'/favicon.ico',
'http://'+hostname+'/bower_components/d3/d3.min.js',
'http://'+hostname+'/bower_components/jquery/dist/jquery.min.js',
'http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,300,400,600,700,800',
'http://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700,300italic,400italic,500italic,700italic',
'',
'NETWORK:',
'*'
]);
}

function writePebbleJSON(req, res) {
req.with_collection = with_collection;
pebble.pebble(req, res);
return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// setup http server
////////////////////////////////////////////////////////////////////////////////////////////////////
var PORT = process.env.PORT || 1337;
var THIRTY_DAYS = 2592000;
var now = new Date();
var expires = new Date(now.getTime() + (1000 * THIRTY_DAYS));
expires = dateformat(expires, "GMT:ddd, d mmm yyyy HH:MM:ss Z");

var app = express();
app.set('title', 'Nightscout');

// serve special URLs
app.get("/pebble", servePebble); // Pebble API
app.get("/nightscout.appcache", serveAppcache); // HTML5 Application Cache

// define static server
var staticDir = __dirname + '/static/';
var server = express.static(staticDir);
app.use(function(req, res, next) {
res.set({
"Cache-Control": "public, max-age=" + THIRTY_DAYS,
"Cache-Control": "max-age=" + THIRTY_DAYS,
"Expires": expires,
"Vary": "Accept-Encoding",
"Accept-Ranges": "none",
"Arr-Disable-Session-Affinity": "True"
});

next();
});

// serve the pebble JSON
app.get("/pebble", function(req, res) {
writePebbleJSON(req, res);
});

// send the HTML5 app cache file
app.use("/nightscout.appcache", function(req, res) {
res.set('Content-Type', 'text/cache-manifest');
res.end(getAppCache(req));
});

// serve the static content
app.use(server);

Expand All @@ -115,6 +68,52 @@ app.use(errorHandler);
var server = app.listen(PORT);
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// server helper functions
////////////////////////////////////////////////////////////////////////////////////////////////////
function errorHandler(err, req, res, next) {
if (err) {
// Log the error
var msg = "Error serving " + request.url + " - " + err.message;
require("sys").error(msg);
console.log(msg);

// Respond to the client
res.status(err.status);
res.render('error', { error: err });
}
}

function getAppCache() {
// Define the files you want the browser to cache
return appcache.newCache([
'audio/alarm.mp3',
'audio/alarm2.mp3',
/* 'css/dropdown.css',
'css/main.css',
'js/client.js',
'js/dropdown.js', */
'/favicon.ico',
'//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,300,400,600,700,800',
'//fonts.googleapis.com/css?family=Ubuntu:300,400,500,700,300italic,400italic,500italic,700italic',
'',
'NETWORK:',
'*'
]);
}

function serveAppcache(req, res) {
res.set('Content-Type', 'text/cache-manifest');
res.end(getAppCache(req));
}

function servePebble(req, res) {
req.with_collection = with_collection;
pebble.pebble(req, res);
return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// setup socket io for data and message transmission
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>NightScout</title>
<link rel='stylesheet' type='text/css' href='css/main.css'>
<link rel='stylesheet' type='text/css' href='css/dropdown.css'>
<link rel='stylesheet' type='text/css' href='/css/main.css'>
<link rel='stylesheet' type='text/css' href='/css/dropdown.css'>
</head>
<body>
<div class='container'>
Expand Down Expand Up @@ -35,11 +35,11 @@
<div id='chartContainer'></div>
</div>
</div>
<audio id='audio' loop src='audio/alarm.mp3' type='audio/mp3'></audio>
<audio src='/audio/alarm.mp3' id='audio' type='audio/mp3'></audio>
<script src='/socket.io/socket.io.js'></script>
<script src='/bower_components/d3/d3.min.js'></script>
<script src='/bower_components/jquery/dist/jquery.min.js'></script>
<script src='js/dropdown.js'></script>
<script src='/js/dropdown.js'></script>
<script src='/js/client.js'></script>
</body>
</html>