pulumi/examples/demo1/app.js
joeduffy 736008168d Add two variants to the Mu demo
This adds two hypothetical variants to the Mu demo, "building" new capabilities
up from the basic Express and MongoDB app we start with:

1) The first variant is to leverage the Mu SDK for service discovery and
   configuration, eliminating the "loosely typed" approach that is common with
   Docker (e.g., having to fetch connection URLs from arguments, environment
   variables, etc).  This extends the Mu type system "into" the Docker container,
   whereas previously it stopped at the boundary.

2) The second variant really takes this to the next level, embellishing the nice
   DSL-like approach.  In this model, infrastructure is expressed in code.
2016-11-07 09:37:47 -08:00

65 lines
1.7 KiB
JavaScript

// Standard Express and MongoDB app.
"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");
});
});