From a74f99f6df78dc950c62c7a9abcbe399ea19fa2b Mon Sep 17 00:00:00 2001 From: arnorhs Date: Sun, 28 Apr 2013 00:25:35 +0000 Subject: [PATCH 1/2] Bumped restify version to 2.4.x --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a78de53e..ed5f64e1 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ } ], "dependencies" : { - "restify": "1.4.x", + "restify": "2.4.x", "request": "2.11.x", "jquery": "1.8.x", "scraper" : "0.0.9", From f693fc44b5c2a07be2b43588f7082217cfc09f76 Mon Sep 17 00:00:00 2001 From: arnorhs Date: Sun, 28 Apr 2013 06:35:59 +0000 Subject: [PATCH 2/2] Removing the need for globals and requiring modules the "Node way" - Started passing in the server object into each endpoint, since they have to use it to register routes. A better approach would probably be to make a special object they call on to add the routes, but this is maybe a bit better than what we're currently doing. - Only added the requires where they are needed - Didn't test any endpoint not documented on docs.apis.is --- endpoints/bus/index.js | 5 ++++- endpoints/bus/realtime.js | 4 +++- endpoints/bus/search.js | 2 +- endpoints/car/index.js | 6 +++++- endpoints/car/lookup.js | 6 +++++- endpoints/company/index.js | 6 +++++- endpoints/currency/arion.js | 6 +++++- endpoints/currency/index.js | 7 ++++++- endpoints/currency/lb.js | 4 +++- endpoints/currency/m5.js | 6 +++++- endpoints/domainlookup/index.js | 6 +++++- endpoints/flight/index.js | 6 +++++- endpoints/frontpage/index.js | 2 +- endpoints/house/index.js | 5 ++++- endpoints/static_endpoints/frontpage.js | 2 +- endpoints/static_endpoints/help_out.js | 2 +- endpoints/static_endpoints/phone.js | 4 ++-- endpoints/word/index.js | 6 +++++- lib/endpoints.js | 8 +++++--- server.js | 22 +++------------------- 20 files changed, 74 insertions(+), 41 deletions(-) diff --git a/endpoints/bus/index.js b/endpoints/bus/index.js index b453a338..4fec471f 100644 --- a/endpoints/bus/index.js +++ b/endpoints/bus/index.js @@ -1,4 +1,7 @@ -exports.setup = function(){ +var request = require('request'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/bus/search', version: '1.0.0'}, search); server.post({path: '/bus/realtime', version: '1.0.0'}, realtime); } diff --git a/endpoints/bus/realtime.js b/endpoints/bus/realtime.js index e87ebff1..bd6ce194 100644 --- a/endpoints/bus/realtime.js +++ b/endpoints/bus/realtime.js @@ -1,4 +1,6 @@ -exports.setup = function(){ +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/bus/realtime', version: '1.0.0'}, realtime); //Old server.get({path: '/bus/realtime', version: '1.0.0'}, realtime); diff --git a/endpoints/bus/search.js b/endpoints/bus/search.js index 1bf49a4a..6931d36b 100644 --- a/endpoints/bus/search.js +++ b/endpoints/bus/search.js @@ -1,4 +1,4 @@ -exports.setup = function(){ +exports.setup = function(server){ //Search function is broken at this point //server.post({path: '/bus/search', version: '1.0.0'}, search); } diff --git a/endpoints/car/index.js b/endpoints/car/index.js index 09acd6ea..4cfc737c 100644 --- a/endpoints/car/index.js +++ b/endpoints/car/index.js @@ -1,4 +1,8 @@ -exports.setup = function(){ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/car', version: '1.0.0'}, slash); } diff --git a/endpoints/car/lookup.js b/endpoints/car/lookup.js index 752ef7bc..a045f49a 100644 --- a/endpoints/car/lookup.js +++ b/endpoints/car/lookup.js @@ -1,4 +1,8 @@ -exports.setup = function(){ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/car', version: '1.0.0'}, lookup); //Old server.get({path: '/car', version: '1.0.0'}, lookup); diff --git a/endpoints/company/index.js b/endpoints/company/index.js index fb242650..c6244b2b 100644 --- a/endpoints/company/index.js +++ b/endpoints/company/index.js @@ -1,4 +1,8 @@ -exports.setup = function(){ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/company', version: '1.0.0'}, lookup); //Old server.get({path: '/company', version: '1.0.0'}, lookup); diff --git a/endpoints/currency/arion.js b/endpoints/currency/arion.js index dba6a44b..5bb2a7af 100644 --- a/endpoints/currency/arion.js +++ b/endpoints/currency/arion.js @@ -1,4 +1,8 @@ -exports.setup = function(){ +var request = require('request'); +var moment = require('moment'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.get({path: '/currency/arion', version: '1.0.0'}, slash); } diff --git a/endpoints/currency/index.js b/endpoints/currency/index.js index d7509450..b1cec778 100644 --- a/endpoints/currency/index.js +++ b/endpoints/currency/index.js @@ -1,4 +1,9 @@ -exports.setup = function(){ +var request = require('request'); +var moment = require('moment'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/currency', version: '1.0.0'}, slash); } diff --git a/endpoints/currency/lb.js b/endpoints/currency/lb.js index 87dc4cb5..6794d35f 100644 --- a/endpoints/currency/lb.js +++ b/endpoints/currency/lb.js @@ -1,4 +1,6 @@ -exports.setup = function(){ +var request = require('request'); + +exports.setup = function(server){ server.get({path: '/currency/lb', version: '1.0.0'}, getCurrencies); }; diff --git a/endpoints/currency/m5.js b/endpoints/currency/m5.js index 062b63d2..9f703f80 100644 --- a/endpoints/currency/m5.js +++ b/endpoints/currency/m5.js @@ -1,4 +1,8 @@ -exports.setup = function() { +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server) { server.get({path: '/currency/m5', version: '1.0.0'}, getCurrencies); }; diff --git a/endpoints/domainlookup/index.js b/endpoints/domainlookup/index.js index a650f4b9..7e3ee909 100644 --- a/endpoints/domainlookup/index.js +++ b/endpoints/domainlookup/index.js @@ -1,6 +1,10 @@ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + //Possible endpoint: //https://www.isnic.is/is/whois/mini.php?type=all&query=apis.is -exports.setup = function(){ +exports.setup = function(server){ server.get({path: '/whois', version: '1.0.0'}, slash); } diff --git a/endpoints/flight/index.js b/endpoints/flight/index.js index 0e4341fb..1a1a4c31 100644 --- a/endpoints/flight/index.js +++ b/endpoints/flight/index.js @@ -1,4 +1,8 @@ -exports.setup = function(){ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + +exports.setup = function(server){ server.post({path: '/flight', version: '1.0.0'}, slash); //Old server.post({path: '/flight', version: '1.0.0'}, slash); diff --git a/endpoints/frontpage/index.js b/endpoints/frontpage/index.js index 3d574f0d..b0d40e1e 100644 --- a/endpoints/frontpage/index.js +++ b/endpoints/frontpage/index.js @@ -1,4 +1,4 @@ -exports.setup = function(){ +exports.setup = function(server){ server.get({path: '/'}, getSlash); server.post({path: '/'}, postSlash); } diff --git a/endpoints/house/index.js b/endpoints/house/index.js index cffb96d5..167e2a17 100644 --- a/endpoints/house/index.js +++ b/endpoints/house/index.js @@ -1,6 +1,9 @@ //Incomplete -exports.setup = function(){ +var request = require('request'); +var $ = require('jquery'); + +exports.setup = function(server){ server.get({path: '/house', version: '1.0.0'}, slash); } diff --git a/endpoints/static_endpoints/frontpage.js b/endpoints/static_endpoints/frontpage.js index 7a28b203..5fc48c76 100644 --- a/endpoints/static_endpoints/frontpage.js +++ b/endpoints/static_endpoints/frontpage.js @@ -1,4 +1,4 @@ -exports.setup = function(){ +exports.setup = function(server){ server.get({path: '/'}, getFrontpage); server.post({path: '/'}, postFrontpage); } diff --git a/endpoints/static_endpoints/help_out.js b/endpoints/static_endpoints/help_out.js index ca9a9d24..3615a533 100644 --- a/endpoints/static_endpoints/help_out.js +++ b/endpoints/static_endpoints/help_out.js @@ -1,4 +1,4 @@ -exports.setup = function(){ +exports.setup = function(server){ server.post({path: '/help_out', version: '1.0.0'}, helpOut); server.get({path: '/help_out', version: '1.0.0'}, helpOut); server.post({path: '/help', version: '1.0.0'}, helpOut); diff --git a/endpoints/static_endpoints/phone.js b/endpoints/static_endpoints/phone.js index 59f1b82b..8d97fc54 100644 --- a/endpoints/static_endpoints/phone.js +++ b/endpoints/static_endpoints/phone.js @@ -3,9 +3,9 @@ var phone = function(req, res, next){ return next(); } -exports.setup = function(){ +exports.setup = function(server){ server.post({path: '/phone', version: '1.0.0'}, phone); server.get({path: '/phone', version: '1.0.0'}, phone); server.post({path: '/phone', version: '2.0.0'}, phone); server.get({path: '/phone', version: '2.0.0'}, phone); -} \ No newline at end of file +} diff --git a/endpoints/word/index.js b/endpoints/word/index.js index cece9455..178ca3d3 100644 --- a/endpoints/word/index.js +++ b/endpoints/word/index.js @@ -1,5 +1,9 @@ +var request = require('request'); +var $ = require('jquery'); +var h = require('../../lib/helpers.js'); + //Very incomplete -exports.setup = function(){ +exports.setup = function(server){ server.post({path: '/word', version: '1.0.0'}, slash); } diff --git a/lib/endpoints.js b/lib/endpoints.js index cb5eb53e..27a09094 100644 --- a/lib/endpoints.js +++ b/lib/endpoints.js @@ -1,17 +1,19 @@ +var fileModule = require('file'); /** * Loads upp all the required endpoints */ -exports.load = function(){ + +exports.load = function(server){ //Load all endpoints in the endpoints folder //walk is blocking on purpose because the server can't listen yet fileModule.walk('./endpoints', function(a, dirPath, dirs, files){ if(files){ files.forEach(function(file,key){ //Setup the endpoint via the setup function - require('../'+file).setup(); + require('../'+file).setup(server); }); }else{ console.log('There is no endpoint to load!'); } }); -} \ No newline at end of file +} diff --git a/server.js b/server.js index eda1d8c8..6d85460a 100644 --- a/server.js +++ b/server.js @@ -1,24 +1,8 @@ /** * Global npm modules */ -exports.restify = restify = require('restify'); -exports.server = server = restify.createServer(); - -exports.scraper = scraper = require('scraper'); -exports.request = request = require('request'); -exports.fs = fs = require('fs'); -exports.$ = $ = require('jquery'); -exports.moment = moment = require('moment'); -exports.file = fileModule = require('file'); -exports.parseString = parseString = require('xml2js').parseString; -exports.cheerio = cheerio = require("cheerio"); -exports.isn2wgs = isn2wgs = require('isn2wgs'); - -/** - * Global helpers - */ -exports.h = h = require('./lib/helpers.js'); -exports.endpoints = endpoints = require('./lib/endpoints.js'); +var restify = require('restify'), + server = restify.createServer(); //Enables the use of posted params server.use(restify.bodyParser({ mapParams: true })); @@ -27,7 +11,7 @@ server.use(restify.queryParser()); /** * Endpoints setup */ -endpoints.load(); +require('./lib/endpoints.js').load(server); /** * Start the server