mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
If-Modified-Since header support for the /entries and /treatments API
This commit is contained in:
@@ -71,6 +71,23 @@ function configure (app, wares, ctx) {
|
||||
return es.map(sync);
|
||||
}
|
||||
|
||||
function ifModifiedSince (req, res, next) {
|
||||
|
||||
var lastEntryDate = new Date(_.last(res.entries).dateString);
|
||||
res.setHeader('Last-Modified', lastEntryDate.toUTCString());
|
||||
|
||||
var ifModifiedSince = req.get('If-Modified-Since');
|
||||
|
||||
if (!ifModifiedSince) { return next(); }
|
||||
|
||||
if (lastEntryDate <= new Date(ifModifiedSince)) {
|
||||
res.status(304).send({status:304, message: 'Not modified', type:'internal'});
|
||||
return;
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
/**
|
||||
* @method format_entries
|
||||
* A final middleware to send payloads assembled by previous middlewares
|
||||
@@ -248,7 +265,7 @@ function configure (app, wares, ctx) {
|
||||
res.entries_err = err;
|
||||
return next( );
|
||||
});
|
||||
}, format_entries);
|
||||
}, ifModifiedSince, format_entries);
|
||||
|
||||
/**
|
||||
* @module get#/entries/:spec
|
||||
@@ -280,7 +297,7 @@ function configure (app, wares, ctx) {
|
||||
prepReqModel(req, req.params.model);
|
||||
query_models(req, res, next);
|
||||
}
|
||||
}, format_entries);
|
||||
}, ifModifiedSince, format_entries);
|
||||
|
||||
|
||||
/**
|
||||
@@ -292,7 +309,7 @@ function configure (app, wares, ctx) {
|
||||
* `find[date]`.
|
||||
*
|
||||
*/
|
||||
api.get('/entries', query_models, format_entries);
|
||||
api.get('/entries', query_models, [ifModifiedSince, format_entries]);
|
||||
|
||||
/**
|
||||
* @function echo_query
|
||||
|
||||
Reference in New Issue
Block a user