Add the "demo1" example app

This just adds a simple voting app for the demo script.  It leverages
a Node.js Express app for the frontend and a MongoDB database for the backend.
It conditionally provisions and attaches to an AWS Elastic Block Store volume
in the event that it's targeting an AWS cluster, and ephemeral storage otherwise.
This commit is contained in:
joeduffy 2016-11-04 12:17:46 -07:00
parent 9836600e73
commit 7e26ab050d
6 changed files with 119 additions and 0 deletions

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

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

16
examples/demo1/Dockerfile Normal file
View file

@ -0,0 +1,16 @@
FROM node:argon
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]

18
examples/demo1/Mu.yaml Normal file
View file

@ -0,0 +1,18 @@
name: demo1-example
description: A simple Express voting app with a MongoDB backend, ready for production use.
services:
private:
{{if aws .target}}
- aws/ebs/volume
{{done}}
- mongodb/mongodb
{{if aws .target}}
volume: @volume
{{done}}
public:
- app:
type: mu/container
build: .
arguments:
db: @mongodb

9
examples/demo1/README.md Normal file
View file

@ -0,0 +1,9 @@
# examples/demo1
A simple Express voting app with a MongoDB backend, ready for production use.
It supports two web APIs:
1. GET /, prints out the current vote counts.
2. POST /vote, casts your vote for a particular candidate.

63
examples/demo1/app.js Normal file
View file

@ -0,0 +1,63 @@
"use strict";
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
// First read in the arguments.
if (process.argv.length < 3) {
console.log("Missing required database argument");
process.exit(-1);
}
var db = process.argv[2];
// Connect to the database and then fire up an Express app.
mongodb.MongoClient.connect(`mongodb://${db}`, (err, conn) => {
if (err) {
console.log(`Problem connecting to database ${db}:`);
console.log(err);
process.exit(-1);
}
var votes = conn.collection("votes");
var app = express();
app.use(bodyParser.json());
app.get("/", (req, res) => {
votes.aggregate([ { "$group" : { _id: "$candidate", count: { $sum: 1 } } } ]).toArray(
(err, candidates) => {
// TODO: sort?
if (err) {
res.status(500).send(`An error occurred: ${err}`);
} else {
res.send(JSON.stringify(candidates, null, 4));
}
}
);
});
app.post("/vote", (req, res) => {
var vote = req.body;
if (!vote || !vote.candidate) {
res.status(500).send("Missing candidate in POST body");
} else {
votes.insertOne(
{ candidate: vote.candidate, time: Date.now() },
(err) => {
if (err) {
res.status(500).send(`An error occurred: ${err}`);
} else {
res.status(200);
}
}
);
}
});
app.listen(8080, () => {
console.log("App listening on port 8080");
});
});

View file

@ -0,0 +1,11 @@
{
"name": "demo1",
"version": "0.0.1",
"description": "A simple Express voting app with a MongoDB backend, ready for production use.",
"main": "app.js",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"mongodb": "^2.2.11"
}
}