Skip to content
Snippets Groups Projects
Commit 15014b07 authored by Steve Battle's avatar Steve Battle
Browse files

refactoring

parent da3e86bf
Branches
No related tags found
No related merge requests found
{
"options": {
"host": "bristol.api.urbanthings.io",
"headers": { "X-Api-Key": "0r6beKZn90a2c9EBs1IqA" },
"accept": "application/json"
},
"port": 8080
}
{
"mysql" : {
"host" : "localhost",
"user" : "root",
"password" : "",
"database" : "ATCO_010_BUS"
},
"bristolapi" : {
"rate": 2,
"options": {
"host": "bristol.api.urbanthings.io",
"headers": { "X-Api-Key": "0r6beKZn90a2c9EBs1IqA" },
"accept": "application/json"
}
}
}
\ No newline at end of file
// server.js
// load the things we need
// see openshift blog
// https://blog.openshift.com/run-your-nodejs-projects-on-openshift-in-two-simple-steps/
// use PORT environment variable or default 8080
//var port = process.env.PORT || 8080;
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080
// var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
// load the things we need
var express = require('express');
var ws = require('./ws');
var ws = require('./wslib');
exports.ws = ws;
var app = express();
var conf = require('./config/dev.json');
var options = conf.options;
exports.options = options;
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
exports.app = app;
var errorFun = function (error) {
console.error("ERROR:", error);
};
// embed path p for ws invocation
function path(p) {
options.path = p;
return options;
}
exports.path = path;
// use res.render to load up an ejs view file
//app.get('/', function(req, res) {
// res.render('pages/index');
//});
app.get('/', function(request, response) {
response.render('index');
});
// https://bristol.api.urbanthings.io/api/2.0/static/importsources
app.get("/", function (ignore, res) {
ws.invoke("/api/2.0/static/importsources")
.then(function (results) {
// res.send(JSON.stringify(results));
res.render('pages/page1', results);
}, errorFun);
app.get("/page1", function (request, response) {
ws.invoke(path("/api/2.0/static/importsources"),
(err,data) => {
if (err) response.status(404).send("Oops!");
else response.render('page1', data);
}) ;
});
// https://bristol.api.urbanthings.io/api/2.0/static/agencies?importsource=TNDS
app.get("/page2", function (req, res) {
"use strict";
var importsource = req.query.importsource;
ws.invoke("/api/2.0/static/agencies?importsource=" + importsource)
.then(function (results) {
res.render('pages/page2', results);
}, errorFun);
app.get("/page2", function (request, response) {
var importsource = request.query.importsource;
ws.invoke(path("/api/2.0/static/agencies?importsource=" + importsource),
(err,data) => {
if (err) response.status(404).send("Oops!");
else response.render('page2', data);
}) ;
});
// https://bristol.api.urbanthings.io/api/2.0/static/routes/info/Source?agencyID=UK_TNDS_NOC_FBRI
app.get("/page3", function (req, res) {
"use strict";
var agencyID = req.query.agencyID;
ws.invoke("/api/2.0/static/routes/info/Source?agencyID=" + agencyID)
.then(function (results) {
res.render('pages/page3', results);
}, errorFun);
app.get("/page3", function (request, response) {
var agencyID = request.query.agencyID;
ws.invoke(path("/api/2.0/static/routes/info/Source?agencyID=" + agencyID),
(err,data) => {
if (err) response.status(404).send("Oops!");
else response.render('page3', data);
}) ;
});
if (process.env.NODE_ENV!="test") {
app.listen(port);
console.log('Listening on port '+port);
}
\ No newline at end of file
File moved
File moved
File moved
File moved
wslib.js 0 → 100644
/*
Author: steve.battle@uwe.ac.uk
Description: Web-Service client library
*/
var https = require('https');
// rate limiter to avoid "API calls quota exceeded! maximum admitted per Second."
var RateLimiter = require('limiter').RateLimiter;
var limiter = new RateLimiter(1, 'second');
function invoke(options, errback) {
limiter.removeTokens(1, () => {
try {
// asynchronous GET
https.get(options, response => {
// initialise streamed response
var body = "";
// add event listeners to response
response.on('data', d => body += d);
response.on('end', () => {
errback(null, JSON.parse(body));
});
});
}
catch (error) { errback(error); }
})
}
exports.invoke = invoke;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment