Create a sample app that exposes a voting API for all 50 states
This commit is contained in:
parent
e0af9a4d6a
commit
b1aa61e03d
2
examples/vote50/.gitignore
vendored
Normal file
2
examples/vote50/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules/
|
||||
|
26
examples/vote50/app.js
Normal file
26
examples/vote50/app.js
Normal 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);
|
||||
}
|
||||
|
Loading…
Reference in a new issue