Create a sample app that exposes a voting API for all 50 states

This commit is contained in:
joeduffy 2016-10-15 09:00:28 -07:00
parent e0af9a4d6a
commit b1aa61e03d
2 changed files with 28 additions and 0 deletions

2
examples/vote50/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules/

26
examples/vote50/app.js Normal file
View file

@ -0,0 +1,26 @@
var mu = require("mu");
// Define a Service that creates a Stack, wires up Functions to Triggers, and exposes an API:
class VotingService {
constructor() {
this.votes = new mu.Table();
this.voteCounts = new mu.Table();
this.votes.forEach(vote => {
// Keep our aggregated counts up-to-date:
this.voteCounts.updateIncrement(vote.color, vote.count);
});
}
vote(info) {
this.votes.push({ color: info.color, count: 1 });
}
}
// Create a HTTP endpoint Service that receives votes from an API:
var voteAPI = new mu.HTTPGateway();
for (var state of [ "AL", "AK", ... "WI", "WY" ]) {
var votingService = new VotingService();
voteAPI.register(`/${state}`, votingService);
}