From b1aa61e03dc1329ea7fdfd7c660eb94c1bf767c6 Mon Sep 17 00:00:00 2001 From: joeduffy Date: Sat, 15 Oct 2016 09:00:28 -0700 Subject: [PATCH] Create a sample app that exposes a voting API for all 50 states --- examples/vote50/.gitignore | 2 ++ examples/vote50/app.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 examples/vote50/.gitignore create mode 100644 examples/vote50/app.js diff --git a/examples/vote50/.gitignore b/examples/vote50/.gitignore new file mode 100644 index 000000000..d5700888a --- /dev/null +++ b/examples/vote50/.gitignore @@ -0,0 +1,2 @@ +node_modules/ + diff --git a/examples/vote50/app.js b/examples/vote50/app.js new file mode 100644 index 000000000..e9abbeb9f --- /dev/null +++ b/examples/vote50/app.js @@ -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); +} +