Skip to content

Commit

Permalink
Merge pull request #9 from nightscout/feature/base-api
Browse files Browse the repository at this point in the history
Feature/base api
  • Loading branch information
brianhanifin committed Jul 5, 2014
2 parents cc2d85d + fec9665 commit 7dd24cc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
97 changes: 97 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

var HTTP_INTERNAL_ERROR = 500;
var ENTRIES_DEFAULT_COUNT = 10;

var ObjectID = require('mongodb').ObjectID;

var with_collection = null;

module.exports = function (coll_fn) {
with_collection = coll_fn;

var express = require('express'),
api = express(),
bodyParser = require('body-parser');

api.use(bodyParser());

api.get('/settings', function (req, res) {
return res.json({
'units': 'mg/dl',
'theme': 'battleship'
});
});

api.get('/entries', function(req, res) {
var count = parseInt(req.query.count, 0) || ENTRIES_DEFAULT_COUNT;
getEntries(function(err, entries) {
if (err)
sendJSONError(res, HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
return res.json(entries);
}, count);
});

api.get('/entries/current', function(req, res) {
getEntries(function(err, entries) {
if (err)
sendJSONError(res, HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
return res.json(entries);
}, 1);
});

api.get('/entries/:id', function(req, res) {
getEntry(function(err, entry) {
if (err)
sendJSONError(res, HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
return res.json(entry);
}, req.params.id);
});

return api;
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Define functions to CRUD data for the API
////////////////////////////////////////////////////////////////////////////////////////////////////

function getEntry(fn, id) {
console.info("trying to find entry for id: " + id);
with_collection(function(err, collection) {
if (err)
fn(err);
else
collection.findOne({"_id": ObjectID(id)}, function (err, entry) {
if (err)
fn(err);
else
fn(null, entry);
});
});
}

function getEntries(fn, count) {
with_collection(function(err, collection) {
if (err)
fn(err);
else
collection.find({ }).sort({"date": -1}).limit(count).toArray(function (err, entries) {
if (err)
fn(err);
else
fn(null, entries);
});
});
}

// Craft a JSON friendly error message.
function sendJSONError(res, status, title, description) {
res.status(status).json({
"status" : status,
"message" : title,
"description" : description
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"postinstall": "node node_modules/bower/bin/bower install"
},
"dependencies": {
"body-parser": "^1.4.3",
"bower": "~1.3.2",
"express": "^4.4.4",
"mongodb": "1.3.20",
Expand Down
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var fs = require('fs');
var express = require('express');
var mongoClient = require('mongodb').MongoClient;
var pebble = require('./lib/pebble');
var api = require('./lib/api')(with_collection);
var cgmData = [];
////////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -37,7 +38,9 @@ var STATIC_DIR = __dirname + '/static/';
var app = express();
app.set('title', 'Nightscout');

// serve special URLs
// BASIC API
app.use("/api/v1", api);

// Pebble API
app.get("/pebble", servePebble);

Expand Down

0 comments on commit 7dd24cc

Please sign in to comment.