Added an API endpoint in the server on /api

This commit is contained in:
Bram van der Veen 2015-06-27 15:03:50 +02:00
parent cf319a8f99
commit d7c2301879
5 changed files with 84 additions and 15 deletions

50
api.js Normal file
View file

@ -0,0 +1,50 @@
//api.js
var lookup = require('./lookup');
var schedule = require('./schedule');
function parse(req, res, next, api) {
req.api = true;
if (api == 'search') {
if (!req.query.name) error('You didn\'t send the needed queries: name', res);
else {
lookup.api(req, function (lookup) {
if (lookup.error) error(lookup.error, res);
else response(lookup.data, res);
});
}
}
else if (api == 'schedule') {
if (!req.query.name) error('You didn\'t send the needed queries : name', res);
else {
lookup.api(req, function (lookup) {
if (lookup.error) error(lookup.error, res);
else {
if (lookup.data.length > 1 || lookup.data.length == 0) error('The request that you did had multiple responses or none, make sure that your query returns one.', res, lookup.data)
else {
console.log(lookup.data);
schedule.api(lookup, function (schedule_data) {
response(schedule_data, res, true);
});
}
}
})
}
}
}
function error(str, res, data) {
res.set('Content-Type', 'application/json');
var error = JSON.stringify({'error' : str, 'data' : data}, null, 2);
res.status(400).end(error);
return;
}
function response(data, res, disable_pretty) {
res.set('Content-Type', 'application/json');
var response = JSON.stringify({'data': data}, null, disable_pretty ? 0 : 2);
res.status(200).end(response);
return;
}
module.exports = parse;

View file

@ -6,7 +6,7 @@ var database = require('mongoskin').db('mongodb://' + config().database);
var school_id = config().school_id;
//Function for looking through the database and finding entries related to the searchterm.
function lookup(req, res, next, search) {
function get(req, res, next, search) {
var index = database.collection('index');
easter(search) ? req.easter = easter(search) : null;
easter(search) ? search = easter(search).name : null; //Check if there are any eastereggs matching the search query.
@ -31,6 +31,19 @@ function lookup(req, res, next, search) {
});
}
function api(req, callback) {
var index = database.collection('index');
var query = RegExp(req.query.name, 'i');
index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}]}).toArray(function (err, database_entry) {
if (err) callback({'error': err});
else {
for (entry of database_entry) {entry.url = make_url(req, entry)}
callback({'data': database_entry});
}
});
}
//Function for making a link out of the given database_entry.
function make_url(req, database_entry) {
var url = 'http://roosters5.gepro-osi.nl/roosters/rooster.php?school=' + school_id + '&type=' + database_entry.type.charAt(0).toUpperCase() + database_entry.type.slice(1) + 'rooster';
@ -70,7 +83,7 @@ function easter(search) {
return null;
}
module.exports = lookup;
module.exports = {'get': get, 'api': api};
//Testing function, if test is passed in the command line will execute a test.
if (process.argv[2] == "test") {

@ -1 +1 @@
Subproject commit 7d2c86d60be054d4db7e1a36dccd3bba83ee4633
Subproject commit a3bb72a3418a3a857e03e9cd73b9e9533a98a492

View file

@ -5,16 +5,19 @@ var config = require('./configuration');
var url = require('url');
//Wrapper function that is being called by express.
function schedule(req, res, next) {
get(req.match.url, function (json) {
function get(req, res, next) {
get_schedule(req.match.url, function (json) {
req.match.json = json;
next();
});
}
//Function for getting the page via http.
function get(get_url, callback) {
function api(lookup, callback) {
get_schedule(lookup.data[0].url, callback)
}
//Function for getting the page via http.
function get_schedule(get_url, callback) {
var options = url.parse(get_url);
options.socksPort = config().tor_port;
options.socksHost = config().tor_host;
@ -89,4 +92,4 @@ function to_json(page) {
}
//Exporting the schedule function.
module.exports = schedule;
module.exports = {'get': get, 'api': api};

15
web.js
View file

@ -4,6 +4,7 @@ var less = require('express-less');
var body_parser = require('body-parser');
var fs = require('fs');
var api = require('./api');
var config = require('./configuration');
var lookup = require('./lookup');
var schedule = require('./schedule');
@ -25,16 +26,18 @@ app.get('/', function (req, res) {
res.render('homepage', req);
});
app.get('/api/:api', function (req, res) {
});
app.param('api', api);
app.get('/rooster/:search', function (req, res) {
next();
});
app.get('/over', function (req, res) {
res.send('Hier kun je lezen over werkmanrooster.');
});
app.param('search', lookup);
app.param('search', schedule);
app.param('search', lookup.get);
app.param('search', schedule.get);
app.param('search', function (req, res) {
req.links = config().links;