Skip to content

Commit

Permalink
feat: aws lambda build.
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney committed Dec 28, 2023
2 parents 34e1312 + b027458 commit 3bd8ecd
Show file tree
Hide file tree
Showing 16 changed files with 3,353 additions and 669 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
ci:
name: CI
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup Node
uses: actions/[email protected]
with:
node-version: '20.10.0'
- name: Install Dependencies
run: npm ci
- name: Save error log
uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: npm-debug-log-${{ hashFiles('package-lock.json') }}
path: npm-debug.log
- name: Build
run: npm run build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.log
node_modules
dist
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[restbus][0]
============
# [restbus][0]

Converts the [NextBus][1] [Public XML Feed][2], which is used to build real-time transit applications, into a RESTful JSON API.

Expand Down
7 changes: 7 additions & 0 deletions lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Used by restbus.info domain for hosting documentation about restbus.
*/
const serverlessExpress = require('@codegenie/serverless-express');
const restbus = require('./lib/restbus');

exports.handler = serverlessExpress({ app: restbus.app() });
178 changes: 100 additions & 78 deletions lib/api/agencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ var agencies = {};

function getJson($) {
var agency = {
id: $.tag,
title: $.title,
region: $.regionTitle
};
id: $.tag,
title: $.title,
region: $.regionTitle
};

return agency;
}

function getLinks(hrefs, agency, req) {
var params = req.params,
s = {
href: hrefs.self,
type: utils.c.MTJSON,
rel: 'self',
rt: utils.c.AGY,
title: "Transit agency '" + agency.id + "'."
},
r = {
href: hrefs.route,
type: utils.c.MTJSON,
rel: 'describedby',
rt: utils.c.RTE,
title: "A collection of routes for transit agency '" + agency.id + "'."
},
v = {
href: hrefs.vehicle,
type: utils.c.MTJSON,
rel: 'describedby',
rt: utils.c.VEH,
title: "A collection of vehicles for transit agency '" + agency.id + "'."
},
a = {
href: hrefs.agency,
type: utils.c.MTJSON,
rel: 'bookmark',
rt: utils.c.AGY,
title: 'A collection of transit agencies. This is the API root!'
},
to = [],
from = [];

if(params.agency) {
s = {
href: hrefs.self,
type: utils.c.MTJSON,
rel: 'self',
rt: utils.c.AGY,
title: "Transit agency '" + agency.id + "'."
},
r = {
href: hrefs.route,
type: utils.c.MTJSON,
rel: 'describedby',
rt: utils.c.RTE,
title: "A collection of routes for transit agency '" + agency.id + "'."
},
v = {
href: hrefs.vehicle,
type: utils.c.MTJSON,
rel: 'describedby',
rt: utils.c.VEH,
title: "A collection of vehicles for transit agency '" + agency.id + "'."
},
a = {
href: hrefs.agency,
type: utils.c.MTJSON,
rel: 'bookmark',
rt: utils.c.AGY,
title: 'A collection of transit agencies. This is the API root!'
},
to = [],
from = [];

if (params.agency) {
to = to.concat([r, v]);
from.push(a);
} else {
Expand All @@ -57,63 +57,85 @@ function getLinks(hrefs, agency, req) {
return { self: s, to: to, from: from };
}

agencies.list = function(req, res) {
https.get(utils.getOptionsWithPath(NBXML_FEED + '?command=agencyList'), function(nbres) {
utils.getJsFromXml(nbres, function(err, js) {
var json = [],
agencies.list = function (req, res) {
https
.get(utils.getOptionsWithPath(NBXML_FEED + '?command=agencyList'), function (nbres) {
utils.getJsFromXml(nbres, function (err, js) {
var json = [],
uri;

if(!err) {
uri = utils.getOpenPath(req);
if (!err) {
uri = utils.getOpenPath(req);

js.body.agency.forEach(function(agency) {
var $ = agency.$,
js.body.agency.forEach(function (agency) {
var $ = agency.$,
a = getJson($),
hrefself = [uri, '/', a.id].join(''),
vuri = [hrefself, '/', 'vehicles'].join(''),
ruri = [hrefself, '/', 'routes'].join('');

if (req.query.links !== 'false') {
a._links = getLinks({self: hrefself, route: ruri, vehicle: vuri, agency: uri}, a, req);
}
if (req.query.links !== 'false') {
a._links = getLinks(
{ self: hrefself, route: ruri, vehicle: vuri, agency: uri },
a,
req
);
}

json.push(a);
});
res.status(200).json(json);
} else utils.streamOrParseError(err, js, res);
json.push(a);
});
res.status(200).json(json);
} else utils.streamOrParseError(err, js, res);
});
})
.on('error', function (e) {
utils.nbRequestError(e, res);
});
}).on('error', function(e) { utils.nbRequestError(e, res); });
};

agencies.get = function(req, res) {
agencies.get = function (req, res) {
var agency_id = req.params.agency;

https.get(utils.getOptionsWithPath(NBXML_FEED + '?command=agencyList'), function(nbres) {
utils.getJsFromXml(nbres, function(err, js) {
var json = false,
uri, ruri, vuri, auri;

if(!err) {
uri = utils.getOpenPath(req);
js.body.agency.every(function(agency) {
var $ = agency.$;

if($.tag == agency_id) {
json = getJson($);
ruri = uri + '/routes';
vuri = uri + '/vehicles';
auri = uri.replace('/' + json.id, '');

if (req.query.links !== 'false') {
json._links = getLinks({self: uri, route: ruri, vehicle: vuri, agency: auri}, json, req);
}
} else return true; // Keep looping.
});
if(json) res.status(200).json(json);
else res.status(404).json(utils.errors.get(404, 'Agency id not found: ' + agency_id));
} else utils.streamOrParseError(err, js, res);
https
.get(utils.getOptionsWithPath(NBXML_FEED + '?command=agencyList'), function (nbres) {
utils.getJsFromXml(nbres, function (err, js) {
var json = false,
uri,
ruri,
vuri,
auri;

if (!err) {
uri = utils.getOpenPath(req);
js.body.agency.every(function (agency) {
var $ = agency.$;

if ($.tag == agency_id) {
json = getJson($);
ruri = uri + '/routes';
vuri = uri + '/vehicles';
auri = uri.replace('/' + json.id, '');

if (req.query.links !== 'false') {
json._links = getLinks(
{ self: uri, route: ruri, vehicle: vuri, agency: auri },
json,
req
);
}
} else return true; // Keep looping.
});
if (json) res.status(200).json(json);
else
res
.status(404)
.json(utils.errors.get(404, 'Agency id not found: ' + agency_id));
} else utils.streamOrParseError(err, js, res);
});
})
.on('error', function (e) {
utils.nbRequestError(e, res);
});
}).on('error', function(e) { utils.nbRequestError(e, res); });
};

module.exports = agencies;
2 changes: 1 addition & 1 deletion lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module.exports = {
routes: require('./routes'),
vehicles: require('./vehicles'),
predictions: require('./predictions')
};
};
Loading

0 comments on commit 3bd8ecd

Please sign in to comment.