forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nightscout#8 from nightscout/dev
Refresh dev from nightscout
- Loading branch information
Showing
41 changed files
with
5,252 additions
and
1,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var consts = require('../../constants'); | ||
var moment = require('moment'); | ||
|
||
function configure(app, wares, ctx) { | ||
var express = require('express') | ||
, api = express.Router(); | ||
|
||
api.use(wares.compression()); | ||
api.use(wares.bodyParser({ | ||
limit: 1048576 * 50 | ||
})); | ||
// text body types get handled as raw buffer stream | ||
api.use(wares.bodyParser.raw({ | ||
limit: 1048576 | ||
})); | ||
// json body types get handled as parsed json | ||
api.use(wares.bodyParser.json({ | ||
limit: 1048576 | ||
})); | ||
// also support url-encoded content-type | ||
api.use(wares.bodyParser.urlencoded({ | ||
limit: 1048576 | ||
, extended: true | ||
})); | ||
// invoke common middleware | ||
api.use(wares.sendJSONStatus); | ||
|
||
api.use(ctx.authorization.isPermitted('api:activity:read')); | ||
|
||
// List activity data available | ||
api.get('/activity', function(req, res) { | ||
var ifModifiedSince = req.get('If-Modified-Since'); | ||
ctx.activity.list(req.query, function(err, results) { | ||
var d1 = null; | ||
|
||
_.forEach(results, function clean(t) { | ||
|
||
var d2 = null; | ||
|
||
if (t.hasOwnProperty('created_at')) { | ||
d2 = new Date(t.created_at); | ||
} else { | ||
if (t.hasOwnProperty('timestamp')) { | ||
d2 = new Date(t.timestamp); | ||
} | ||
} | ||
|
||
if (d2 == null) { return; } | ||
|
||
if (d1 == null || d2.getTime() > d1.getTime()) { | ||
d1 = d2; | ||
} | ||
}); | ||
|
||
if (!_.isNil(d1)) res.setHeader('Last-Modified', d1.toUTCString()); | ||
|
||
if (ifModifiedSince && d1.getTime() <= moment(ifModifiedSince).valueOf()) { | ||
res.status(304).send({ | ||
status: 304 | ||
, message: 'Not modified' | ||
, type: 'internal' | ||
}); | ||
return; | ||
} else { | ||
return res.json(results); | ||
} | ||
}); | ||
}); | ||
|
||
function config_authed(app, api, wares, ctx) { | ||
|
||
function post_response(req, res) { | ||
var activity = req.body; | ||
|
||
if (!_.isArray(activity)) { | ||
activity = [activity]; | ||
}; | ||
|
||
ctx.activity.create(activity, function(err, created) { | ||
if (err) { | ||
console.log('Error adding activity data', err); | ||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); | ||
} else { | ||
console.log('Activity measure created'); | ||
res.json(created); | ||
} | ||
}); | ||
} | ||
|
||
api.post('/activity/', wares.bodyParser({ | ||
limit: 1048576 * 50 | ||
}), ctx.authorization.isPermitted('api:activity:create'), post_response); | ||
|
||
api.delete('/activity/:_id', ctx.authorization.isPermitted('api:activity:delete'), function(req, res) { | ||
ctx.activity.remove(req.params._id, function() { | ||
res.json({}); | ||
}); | ||
}); | ||
|
||
// update record | ||
api.put('/activity/', ctx.authorization.isPermitted('api:activity:update'), function(req, res) { | ||
var data = req.body; | ||
ctx.activity.save(data, function(err, created) { | ||
if (err) { | ||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); | ||
console.log('Error saving activity'); | ||
console.log(err); | ||
} else { | ||
res.json(created); | ||
console.log('Activity measure saved', data); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
if (app.enabled('api') && app.enabled('careportal')) { | ||
config_authed(app, api, wares, ctx); | ||
} | ||
|
||
return api; | ||
} | ||
|
||
module.exports = configure; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.