35 |
36 |
37 |
--------------------------------------------------------------------------------
/containers/map-api/routes/beacons.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const router = express.Router();
3 |
4 | const Beacons = require("../models/beacon");
5 |
6 | // endpoints for beacon
7 | router.post("/add", function(req, res) {
8 | // JSON in req.body
9 | // Insert input validation
10 | let addBeacon = new Beacons(req.body);
11 | addBeacon.save(function(err) {
12 | if (err) {
13 | res.send(err);
14 | } else {
15 | res.send("Saved beacon.");
16 | }
17 | });
18 | });
19 |
20 | router.get("/", function(req, res) {
21 | Beacons.find(function(err, beacons) {
22 | if (err) {
23 | res.send(err);
24 | } else {
25 | res.send(beacons);
26 | }
27 | });
28 | });
29 |
30 | router.get("/:beaconId", function(req, res) {
31 | Beacons.findOne(req.params, function(err, beacon) {
32 | if (err) {
33 | res.send(err);
34 | } else if (beacon) {
35 | res.send(beacon);
36 | } else {
37 | res.send("Beacon not found...");
38 | }
39 | });
40 | });
41 |
42 | module.exports = router;
43 |
--------------------------------------------------------------------------------
/containers/map-api/routes/renderSvg.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const router = express.Router();
3 |
4 | const svgRoute = require("./svg");
5 |
6 | const Events = require("../models/event");
7 |
8 | router.get("/:eventId", function(req, res) {
9 | Events.findOne({"eventId": req.params.eventId.split(".")[0]}, function(err, event) {
10 | if (err) {
11 | res.send(err);
12 | } else if (event) {
13 |
14 | // Get SVG Content
15 | let SVGContent = svgRoute.functions.svgContent(event.map,25);
16 | let svg = svgRoute.functions.svgTemplate(event.x,event.y,SVGContent,25);
17 |
18 | // Send SVG
19 | res.render('main', {svg: svg, name: event.eventName, list: []});
20 |
21 | } else {
22 | res.send("Event not found...");
23 | }
24 | });
25 | });
26 |
27 | router.get("/", function(req, res) {
28 | Events.find(function(err, events) {
29 | if (err) {
30 | res.send(err);
31 | } else {
32 | res.render('main', { list: events, name: "Stored Events", svg: ""});
33 | }
34 | });
35 | });
36 |
37 | module.exports = router;
38 |
--------------------------------------------------------------------------------
/.yamllint.yml:
--------------------------------------------------------------------------------
1 | ---
2 | rules:
3 | braces:
4 | min-spaces-inside: 0
5 | max-spaces-inside: 0
6 | min-spaces-inside-empty: 0
7 | max-spaces-inside-empty: 0
8 | brackets:
9 | min-spaces-inside: 0
10 | max-spaces-inside: 0
11 | min-spaces-inside-empty: 0
12 | max-spaces-inside-empty: 0
13 | colons:
14 | max-spaces-before: 0
15 | max-spaces-after: 1
16 | commas:
17 | max-spaces-before: 0
18 | min-spaces-after: 1
19 | max-spaces-after: 1
20 | comments:
21 | require-starting-space: true
22 | min-spaces-from-content: 2
23 | comments-indentation: enable
24 | document-end: disable
25 | document-start:
26 | present: true
27 | empty-lines:
28 | max: 2
29 | max-start: 0
30 | max-end: 0
31 | hyphens:
32 | max-spaces-after: 1
33 | indentation:
34 | spaces: consistent
35 | indent-sequences: true
36 | check-multi-line-strings: false
37 | key-duplicates: enable
38 | line-length:
39 | max: 80
40 | allow-non-breakable-words: true
41 | allow-non-breakable-inline-mappings: true
42 | level: warning
43 | new-line-at-end-of-file: enable
44 | new-lines:
45 | type: unix
46 | trailing-spaces: enable
47 | truthy: enable
48 |
--------------------------------------------------------------------------------
/containers/map-api/app.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const app = express();
3 | const mongoose = require("mongoose");
4 | const assert = require("assert");
5 | const cors = require("cors");
6 |
7 | const beaconRoute = require("./routes/beacons");
8 | const boothRoute = require("./routes/booths");
9 | const eventRoute = require("./routes/events");
10 | const svgRoute = require("./routes/svg");
11 | const renderRoute = require("./routes/renderSvg");
12 |
13 | const mongoDbUrl = process.env.MONGODB_URL;
14 |
15 | let mongoDbOptions = {
16 | // ssl: true,
17 | // sslValidate: true,
18 | useNewUrlParser: true
19 | };
20 |
21 | mongoose.set('useCreateIndex',true);
22 | mongoose.connection.on("error", function(err) {
23 | console.log("Mongoose default connection error: " + err);
24 | });
25 |
26 | mongoose.connection.on("open", function(err) {
27 | console.log("CONNECTED...");
28 | assert.equal(null, err);
29 | });
30 |
31 | if (process.env.UNIT_TEST == "test") {
32 | mongoose.connect("mongodb://localhost/myapp");
33 | }
34 | else {
35 | mongoose.connect(mongoDbUrl, mongoDbOptions);
36 | }
37 |
38 | app.use(require("body-parser").json());
39 | app.use(cors());
40 |
41 | app.set('view engine', 'ejs');
42 |
43 | app.use(express.static(__dirname + "/public"));
44 |
45 | app.use("/main", renderRoute);
46 | app.use("/beacons", beaconRoute);
47 | app.use("/booths", boothRoute);
48 | app.use("/events", eventRoute);
49 | app.use("/svg", svgRoute.main);
50 |
51 | let port = process.env.PORT || 8080;
52 | app.listen(port, function() {
53 | console.log("To view your app, open this link in your browser: http://localhost:" + port);
54 | });
55 |
56 | module.exports = app;
--------------------------------------------------------------------------------
/containers/map-api/routes/events.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const router = express.Router();
3 |
4 | const Beacons = require("../models/beacon");
5 | const Booths = require("../models/booth");
6 | const Events = require("../models/event");
7 |
8 | // endpoints for event
9 | router.post("/add", function(req, res) {
10 | // JSON in req.body
11 | // Insert input validation
12 | let boothIds = req.body.map;
13 | let beaconIds = req.body.beacons;
14 | console.log(boothIds);
15 | console.log(beaconIds);
16 |
17 | Booths.find({"boothId": {$in: boothIds}},
18 | function(err, booths) {
19 | if (err) {
20 | console.log("error in booth query");
21 | res.send(err);
22 | } else {
23 | req.body.map = booths;
24 | Beacons.find({"beaconId": {$in: beaconIds}},
25 | function(err, beacons) {
26 | if (err) {
27 | console.log("error in beacon query");
28 | res.send(err);
29 | } else {
30 | req.body.beacons = beacons;
31 |
32 | let addEvent = new Events(req.body);
33 | addEvent.save(function(err) {
34 | if (err) {
35 | console.log(req.body);
36 | console.log("error in add event");
37 | res.send(err);
38 | } else {
39 | res.send("Saved event.");
40 | }
41 | });
42 |
43 | }
44 | });
45 | }
46 | });
47 | });
48 |
49 | router.get("/", function(req, res) {
50 | Events.find(function(err, events) {
51 | if (err) {
52 | res.send(err);
53 | } else {
54 | res.send(events);
55 | }
56 | });
57 | });
58 |
59 | router.get("/:eventId", function(req, res) {
60 | Events.findOne(req.params, function(err, event) {
61 | if (err) {
62 | res.send(err);
63 | } else if (event) {
64 | res.send(event);
65 | } else {
66 | res.send("Event not found...");
67 | }
68 | });
69 | });
70 |
71 | module.exports = router;
72 |
--------------------------------------------------------------------------------
/mockdata-think.js:
--------------------------------------------------------------------------------
1 | /*global db:false ISODate:false */
2 | db.beacons.insert({ "beaconId" : "B11", "x" : 2, "y" : 5, "minCount" : 1, "maxCount" : 100 });
3 | db.beacons.insert({ "beaconId" : "B12", "x" : 11, "y" : 4, "minCount" : 1, "maxCount" : 100 });
4 | db.beacons.insert({ "beaconId" : "B13", "x" : 19, "y" : 20, "minCount" : 1, "maxCount" : 100 });
5 | db.beacons.insert({ "beaconId" : "B14", "x" : 10, "y" : 11, "minCount" : 1, "maxCount" : 100 });
6 | db.booths.insert({ "boothId" : "A11", "unit" : "Node", "description" : "Node description", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 10, "height" : 5, "x" : 1, "y" : 1}, "contact" : "John Doe" });
7 | db.booths.insert({ "boothId" : "A12", "unit" : "MongoDB", "description" : "MongoDB is not a relational database", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 5, "height" : 35, "x" : 40, "y" : 1}, "contact" : "Mary Jane" });
8 | db.booths.insert({ "boothId" : "A13", "unit" : "Swift", "description" : "Swift is not just for iOS", "measurementUnit" : "metre", "shape" : {"type": "circle", "radius" : 3, "cx" : 6, "cy" : 15}, "contact" : "Jane Doe" });
9 | db.booths.insert({ "boothId" : "A14", "unit" : "VR", "description" : "Virtual Reality is growing", "measurementUnit" : "metre", "shape" : {"type": "ellipse", "cx" : 18, "cy" : 32, "rx" : 13, "ry" : 3}, "contact" : "Smith John" });
10 | db.booths.insert({ "boothId" : "A15", "unit" : "Watson", "description" : "IBM Watson.", "measurementUnit" : "metre", "shape" : {"type": "polygon", "points" : "22,1 30,21 17,25 13,23"}, "contact" : "Catherine May" });
11 | db.events.insert({ "eventId" : "think", "eventName" : "Think", "location" : "Las Vegas", "x" : 46, "y" : 37, "startDate" : ISODate("2018-03-19T00:00:00Z"), "endDate" : ISODate("2018-03-22T00:00:00Z"), "beacons" : [], "map" : [] });
12 |
13 | let booths = db.booths.find({"boothId": {$in: ["A11","A12","A13","A14","A15"]}}).toArray();
14 | let beacons = db.beacons.find({"beaconId": {$in: ["B11","B12","B13","B14"]}}).toArray();
15 |
16 | db.events.update({eventId: "think"}, {$set: {"beacons": beacons, "map": booths}});
17 |
--------------------------------------------------------------------------------
/mockdata-index.js:
--------------------------------------------------------------------------------
1 | /*global db:false ISODate:false */
2 | db.beacons.insert({ "beaconId" : "B01", "x" : 2, "y" : 5, "minCount" : 1, "maxCount" : 100 });
3 | db.beacons.insert({ "beaconId" : "B02", "x" : 11, "y" : 4, "minCount" : 1, "maxCount" : 100 });
4 | db.beacons.insert({ "beaconId" : "B03", "x" : 19, "y" : 20, "minCount" : 1, "maxCount" : 100 });
5 | db.beacons.insert({ "beaconId" : "B04", "x" : 10, "y" : 11, "minCount" : 1, "maxCount" : 100 });
6 | db.booths.insert({ "boothId" : "A01", "unit" : "Node", "description" : "Node description", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 3, "height" : 3, "x" : 0, "y" : 0}, "contact" : "John Doe" });
7 | db.booths.insert({ "boothId" : "A02", "unit" : "MongoDB", "description" : "MongoDB is not a relational database", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 5, "height" : 4, "x" : 16, "y" : 1}, "contact" : "Mary Jane" });
8 | db.booths.insert({ "boothId" : "A03", "unit" : "Swift", "description" : "Swift is not just for iOS", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 6, "height" : 2, "x" : 6, "y" : 4}, "contact" : "Jane Doe" });
9 | db.booths.insert({ "boothId" : "A04", "unit" : "VR", "description" : "Virtual Reality is growing", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 9, "height" : 7, "x" : 4, "y" : 8}, "contact" : "Smith John" });
10 | db.booths.insert({ "boothId" : "A05", "unit" : "Watson", "description" : "IBM Watson.", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 3, "height" : 5, "x" : 16, "y" : 8}, "contact" : "Catherine May" });
11 | db.booths.insert({ "boothId" : "A06", "unit" : "Info", "description" : "Information Booth", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 7, "height" : 3, "x" : 11, "y" : 17}, "contact" : "Ben Jerry" });
12 | db.booths.insert({ "boothId" : "A07", "unit" : "IBM Cloud", "description" : "Used to be called Bluemix", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 4, "height" : 4, "x" : 3, "y" : 17}, "contact" : "Joe Myers" });
13 | db.events.insert({ "eventId" : "index", "eventName" : "Index", "location" : "San Francisco", "x" : 21, "y" : 21,"startDate" : ISODate("2018-02-20T00:00:00Z"), "endDate" : ISODate("2018-02-24T00:00:00Z"), "beacons" : [], "map" : [] });
14 |
15 | let booths = db.booths.find({"boothId": {$in: ["A01","A02","A03","A04","A05","A06","A07"]}}).toArray();
16 | let beacons = db.beacons.find({"beaconId": {$in: ["B01","B02","B03","B04"]}}).toArray();
17 |
18 | db.events.update({eventId: "index"}, {$set: {"beacons": beacons, "map": booths}});
19 |
--------------------------------------------------------------------------------
/MAINTAINERS.md:
--------------------------------------------------------------------------------
1 | # Maintainers Guide
2 |
3 | This guide is intended for maintainers - anybody with commit access to one or
4 | more Code Pattern repositories.
5 |
6 | ## Methodology
7 |
8 | This repository does not have a traditional release management cycle, but
9 | should instead be maintained as a useful, working, and polished reference at
10 | all times. While all work can therefore be focused on the master branch, the
11 | quality of this branch should never be compromised.
12 |
13 | The remainder of this document details how to merge pull requests to the
14 | repositories.
15 |
16 | ## Merge approval
17 |
18 | The project maintainers use LGTM (Looks Good To Me) in comments on the pull
19 | request to indicate acceptance prior to merging. A change requires LGTMs from
20 | two project maintainers. If the code is written by a maintainer, the change
21 | only requires one additional LGTM.
22 |
23 | ## Reviewing Pull Requests
24 |
25 | We recommend reviewing pull requests directly within GitHub. This allows a
26 | public commentary on changes, providing transparency for all users. When
27 | providing feedback be civil, courteous, and kind. Disagreement is fine, so long
28 | as the discourse is carried out politely. If we see a record of uncivil or
29 | abusive comments, we will revoke your commit privileges and invite you to leave
30 | the project.
31 |
32 | During your review, consider the following points:
33 |
34 | ### Does the change have positive impact?
35 |
36 | Some proposed changes may not represent a positive impact to the project. Ask
37 | whether or not the change will make understanding the code easier, or if it
38 | could simply be a personal preference on the part of the author (see
39 | [bikeshedding](https://en.wiktionary.org/wiki/bikeshedding)).
40 |
41 | Pull requests that do not have a clear positive impact should be closed without
42 | merging.
43 |
44 | ### Do the changes make sense?
45 |
46 | If you do not understand what the changes are or what they accomplish, ask the
47 | author for clarification. Ask the author to add comments and/or clarify test
48 | case names to make the intentions clear.
49 |
50 | At times, such clarification will reveal that the author may not be using the
51 | code correctly, or is unaware of features that accommodate their needs. If you
52 | feel this is the case, work up a code sample that would address the pull
53 | request for them, and feel free to close the pull request once they confirm.
54 |
55 | ### Does the change introduce a new feature?
56 |
57 | For any given pull request, ask yourself "is this a new feature?" If so, does
58 | the pull request (or associated issue) contain narrative indicating the need
59 | for the feature? If not, ask them to provide that information.
60 |
61 | Are new unit tests in place that test all new behaviors introduced? If not, do
62 | not merge the feature until they are! Is documentation in place for the new
63 | feature? (See the documentation guidelines). If not do not merge the feature
64 | until it is! Is the feature necessary for general use cases? Try and keep the
65 | scope of any given component narrow. If a proposed feature does not fit that
66 | scope, recommend to the user that they maintain the feature on their own, and
67 | close the request. You may also recommend that they see if the feature gains
68 | traction among other users, and suggest they re-submit when they can show such
69 | support.
70 |
--------------------------------------------------------------------------------
/curl-mockdata.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B01", "x" : 2, "y" : 5, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
4 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B02", "x" : 11, "y" : 4, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
5 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B03", "x" : 19, "y" : 20, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
6 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B04", "x" : 10, "y" : 11, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
7 |
8 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A01", "unit" : "Node", "description" : "Node description", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 3, "height" : 3, "x" : 0, "y" : 0}, "contact" : "John Doe" }' "$1"/booths/add
9 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A02", "unit" : "MongoDB", "description" : "MongoDB is not a relational database", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 5, "height" : 4, "x" : 16, "y" : 1}, "contact" : "Mary Jane" }' "$1"/booths/add
10 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A03", "unit" : "Swift", "description" : "Swift is not just for iOS", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 6, "height" : 2, "x" : 6, "y" : 4}, "contact" : "Jane Doe" }' "$1"/booths/add
11 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A04", "unit" : "VR", "description" : "Virtual Reality is growing", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 9, "height" : 7, "x" : 4, "y" : 8}, "contact" : "Smith John" }' "$1"/booths/add
12 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A05", "unit" : "Watson", "description" : "IBM Watson.", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 3, "height" : 5, "x" : 16, "y" : 8}, "contact" : "Catherine May" }' "$1"/booths/add
13 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A06", "unit" : "Info", "description" : "Information Booth", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 7, "height" : 3, "x" : 11, "y" : 17}, "contact" : "Ben Jerry" }' "$1"/booths/add
14 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A07", "unit" : "IBM Cloud", "description" : "Used to be called Bluemix", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 4, "height" : 4, "x" : 3, "y" : 17}, "contact" : "Joe Myers" }' "$1"/booths/add
15 | sleep 1s
16 | curl -X POST -H 'Content-type: application/json' -d '{ "eventId" : "index", "eventName" : "Index", "location" : "San Francisco", "x" : 21, "y" : 21,"startDate" : "2018-02-20T00:00:00Z", "endDate" : "2018-02-24T00:00:00Z", "beacons" : ["B01","B02","B03","B04"], "map" : ["A01","A02","A03","A04","A05","A06","A07"] }' "$1"/events/add
17 |
18 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B11", "x" : 2, "y" : 5, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
19 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B12", "x" : 11, "y" : 4, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
20 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B13", "x" : 19, "y" : 20, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
21 | curl -X POST -H 'Content-type: application/json' -d '{ "beaconId" : "B14", "x" : 10, "y" : 11, "minCount" : 1, "maxCount" : 100 }' "$1"/beacons/add
22 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A11", "unit" : "Node", "description" : "Node description", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 10, "height" : 5, "x" : 1, "y" : 1}, "contact" : "John Doe" }' "$1"/booths/add
23 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A12", "unit" : "MongoDB", "description" : "MongoDB is not a relational database", "measurementUnit" : "metre", "shape" : {"type": "rectangle", "width" : 5, "height" : 35, "x" : 40, "y" : 1}, "contact" : "Mary Jane" }' "$1"/booths/add
24 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A13", "unit" : "Swift", "description" : "Swift is not just for iOS", "measurementUnit" : "metre", "shape" : {"type": "circle", "radius" : 3, "cx" : 6, "cy" : 15}, "contact" : "Jane Doe" }' "$1"/booths/add
25 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A14", "unit" : "VR", "description" : "Virtual Reality is growing", "measurementUnit" : "metre", "shape" : {"type": "ellipse", "cx" : 18, "cy" : 32, "rx" : 13, "ry" : 3}, "contact" : "Smith John" }' "$1"/booths/add
26 | curl -X POST -H 'Content-type: application/json' -d '{ "boothId" : "A15", "unit" : "Watson", "description" : "IBM Watson.", "measurementUnit" : "metre", "shape" : {"type": "polygon", "points" : "22,1 30,21 17,25 13,23"}, "contact" : "Catherine May" }' "$1"/booths/add
27 | sleep 1s
28 | curl -X POST -H 'Content-type: application/json' -d '{ "eventId" : "think", "eventName" : "Think", "location" : "Las Vegas", "x" : 46, "y" : 37, "startDate" : "2018-03-19T00:00:00Z", "endDate" : "2018-03-22T00:00:00Z", "beacons" : ["B11","B12","B13","B14"], "map" : ["A11","A12","A13","A14","A15"] }' "$1"/events/add
--------------------------------------------------------------------------------
/containers/map-api/routes/svg.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const router = express.Router();
3 | const PDFDocument = require("pdfkit");
4 | const SVGtoPDF = require("svg-to-pdfkit");
5 | const stream = require("stream");
6 |
7 | const Events = require("../models/event");
8 |
9 |
10 | router.get("/:eventId", function(req, res) {
11 | Events.findOne({"eventId": req.params.eventId.split(".")[0]}, function(err, event) {
12 | if (err) {
13 | res.send(err);
14 | } else if (event) {
15 |
16 | // Get SVG Content
17 | let SVGContent = svgContent(event.map,25);
18 | let svg = svgTemplate(event.x,event.y,SVGContent,25);
19 |
20 | // If .pdf is present, send a pdf version of the svg
21 | if (req.params.eventId.split(".").pop() == "pdf") {
22 |
23 | const pdfPointRatio = 3/4;
24 | let pdfX = pdfPointRatio * event.x * 25;
25 | let pdfY = pdfPointRatio * event.y * 25;
26 |
27 | // Start making PDF
28 | let doc = new PDFDocument({ size: [pdfX,pdfY]}); // Fixed to 1000 for now.
29 | let echoStream = new stream.Writable();
30 | let pdfBuffer = new Buffer("");
31 |
32 | // Write to Buffer
33 | echoStream._write = function (chunk, encoding, done) {
34 | pdfBuffer = Buffer.concat([pdfBuffer, chunk]);
35 | done();
36 | };
37 |
38 | // Use svg-to-pdfkit
39 | SVGtoPDF(doc, svg, 0, 0);
40 | doc.pipe(echoStream);
41 | doc.end();
42 |
43 | // Set content type to pdf
44 | res.contentType("application/pdf");
45 |
46 | // When stream is done, send pdf
47 | echoStream.on("finish", function () {
48 | // Make Buffer readable stream
49 | let bufferStream = new stream.PassThrough();
50 | bufferStream.end(pdfBuffer);
51 | bufferStream.pipe(res);
52 | });
53 | } else {
54 |
55 | // Send SVG
56 | res.send(svg);
57 | }
58 | } else {
59 | res.send("Event not found...");
60 | }
61 | });
62 | });
63 |
64 | /**
65 | * Forms an SVG
66 | * @param {String} width is the width of SVG.
67 | * @param {String} height is the height of SVG.
68 | * @param {String} content contains the SVG elements.
69 | * @param {String} scale is used to scale the values: [width, height]
70 | * @return {String} an SVG in xml format
71 | */
72 | function svgTemplate(width, height, content, scale) {
73 | let svg = "";
75 | return svg;
76 | }
77 |
78 | function svgContent(arrayOfElements,scale) {
79 | let svg = "";
80 | for (let i = 0; i < arrayOfElements.length; i++) {
81 | let booth = arrayOfElements[i];
82 | if(booth.shape.type == "rectangle") {
83 | svg +=
84 | rectangleTemplate(booth,scale);
85 | }
86 | if(booth.shape.type == "circle") {
87 | svg +=
88 | circleTemplate(booth,scale);
89 | }
90 | if(booth.shape.type == "ellipse") {
91 | svg +=
92 | ellipseTemplate(booth,scale);
93 | }
94 | if(booth.shape.type == "polygon") {
95 | svg +=
96 | polygonTemplate(booth,scale);
97 | }
98 | }
99 | return svg;
100 | }
101 |
102 | /**
103 | * Forms an SVG element of rectangle
104 | * @param {String} x is the x location of the rectangle.
105 | * @param {String} y is the y location of the rectangle.
106 | * @param {String} width is the width of the rectangle.
107 | * @param {String} height is the height of the rectangle.
108 | * @param {String} scale is used to scale the values above.
109 | * @return {String} an SVG element of rectangle in xml format
110 | */
111 | function rectangleTemplate(booth, scale) {
112 | let elem = booth.shape;
113 | elem.x *= scale;
114 | elem.y *= scale;
115 | elem.width *= scale;
116 | elem.height *= scale;
117 | const xCentroid = (elem.width/2)+elem.x;
118 | const yCentroid = (elem.height/2)+elem.y;
119 |
120 | let svg = "";
122 | svg += "" +
125 | booth.unit + "";
126 | return svg;
127 | }
128 |
129 | /**
130 | * Forms an SVG element of circle
131 | * @param {String} cx is the x location of the center of the circle.
132 | * @param {String} cy is the y location of the center of the circle.
133 | * @param {String} radius is the radius of the circle.
134 | * @param {String} scale is used to scale the values above.
135 | * @return {String} an SVG element of circle in xml format
136 | */
137 | function circleTemplate(booth, scale) {
138 | let elem = booth.shape;
139 | elem.cx *= scale;
140 | elem.cy *= scale;
141 | elem.radius *= scale;
142 |
143 | let svg = "";
145 | svg += "" +
148 | booth.unit + "";
149 | return svg;
150 | }
151 |
152 | /**
153 | * Forms an SVG element of ellipse
154 | * @param {String} cx is the x location of the center of the ellipse.
155 | * @param {String} cy is the y location of the center of the ellipse.
156 | * @param {String} rx is the x radius of the ellipse.
157 | * @param {String} ry is the y radius of the ellipse.
158 | * @param {String} scale is used to scale the values above.
159 | * @return {String} an SVG element of rectangle in xml format
160 | */
161 | function ellipseTemplate(booth, scale) {
162 | let elem = booth.shape;
163 | elem.cx *= scale;
164 | elem.cy *= scale;
165 | elem.rx *= scale;
166 | elem.ry *= scale;
167 |
168 | let svg = "";
170 | svg += "" +
173 | booth.unit + "";
174 | return svg;
175 | }
176 |
177 | /**
178 | * Forms an SVG element of polygon
179 | * @param {String} points contains the points of the polygon.
180 | * @param {String} scale is used to scale the values above.
181 | * @return {String} an SVG element of rectangle in xml format
182 | */
183 | function polygonTemplate(booth, scale) {
184 | let elem = booth.shape;
185 | let integers = elem.points.split(/[\s,]+/);
186 | let scaledInt = "";
187 | let xPoints = [];
188 | let yPoints = [];
189 |
190 | for (let i in integers) {
191 | if (i % 2 == 0) {
192 | scaledInt += integers[i]*scale + ",";
193 | xPoints.push(integers[i]*scale);
194 | }
195 | else {
196 | scaledInt += integers[i]*scale + " ";
197 | yPoints.push(integers[i]*scale);
198 | }
199 | }
200 |
201 | const xCentroid = xPoints.reduce((a,b) => (a+b)) / xPoints.length;
202 | const yCentroid = yPoints.reduce((a,b) => (a+b)) / yPoints.length;
203 | let svg = "";
204 | svg += "" +
207 | booth.unit + "";
208 | return svg;
209 | }
210 |
211 | module.exports = {
212 | main: router,
213 | functions: { svgTemplate, svgContent, rectangleTemplate, circleTemplate, ellipseTemplate, polygonTemplate }
214 | };
215 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/IBM/kubernetes-mongoose)
2 |
3 | # Create a Map Server with MongoDB and Mongoose
4 | In this Code Pattern, we will create a map server using MongoDB and Mongoose. The map server is a Node.js app using the mongoose framework. The map server will serve the data to generate an SVG of the map for a dashboard. The dashboard will show the booths and draw a map for the user. PDF versions will also be served for the iOS app that will generate an indoor map.
5 |
6 | When the reader has completed this Code Pattern, they will understand how to:
7 |
8 | * Implement Mongoose with NodeJS
9 | * Serve the data with Express as REST APIs
10 |
11 | 
12 |
13 | ## Flow
14 | 1. A MongoDB is set up. Compose for MongoDB is used in this pattern.
15 | 2. User adds in mock data that matches the schema of the app.
16 | 3. User interacts with the deployed app via its APIs
17 |
18 | ## Included components
19 | * [Compose for MongoDB](https://console.bluemix.net/catalog/services/compose-for-mongodb): MongoDB with its powerful indexing and querying, aggregation and wide driver support, has become the go-to JSON data store for many startups and enterprises.
20 | * [Kubernetes Cluster](https://console.bluemix.net/containers-kubernetes/catalogCluster): Create and manage your own cloud infrastructure and use Kubernetes as your container orchestration engine.
21 |
22 | ## Featured technologies
23 | * [Mongoose](http://mongoosejs.com/): A JavaScript framework that serves as a MongoDB object modeling tool.
24 | * [Node.js](https://nodejs.org/): An open-source JavaScript run-time environment for executing server-side JavaScript code.
25 | * [Databases](https://en.wikipedia.org/wiki/IBM_Information_Management_System#.22Full_Function.22_databases): Repository for storing and managing collections of data.
26 |
27 | # Prerequisite
28 |
29 | Create a Kubernetes cluster with either [Minikube](https://kubernetes.io/docs/getting-started-guides/minikube) for local testing, or with [IBM Cloud Kubernetes Service](https://github.com/IBM/container-journey-template/blob/master/README.md) to deploy in cloud. The code here is regularly tested against [Kubernetes Cluster from IBM Cloud Kubernetes Service](https://console.ng.bluemix.net/docs/containers/cs_ov.html#cs_ov) using Travis.
30 |
31 | Install [Docker](https://www.docker.com) by following the instructions [here](https://www.docker.com/community-edition#/download) for your preferrerd operating system. You would need docker if you want to build and use your own images.
32 |
33 | # Steps
34 |
35 | 1. [Clone the repo](#1-clone-the-repo)
36 | 2. [Create Compose for MongoDB service with IBM Cloud or deploy one in Kubernetes](#2-create-compose-for-mongodb-service-with-ibm-cloud-or-deploy-one-in-kubernetes)
37 | 3. [Build your images](#3-build-your-images)
38 | 4. [Configure Deployment files](#4-configure-deployment-files)
39 | 5. [Deploy the application](#5-deploy-the-application)
40 | 6. [Generate Mock Data](#6-generate-mock-data)
41 | 7. [Perform API Requests](#7-perform-api-requests)
42 |
43 | ### 1. Clone the repo
44 |
45 | Clone the `kubernetes-mongoose` locally. In a terminal, run:
46 |
47 | ```
48 | $ git clone https://github.com/IBM/kubernetes-mongoose
49 | ```
50 |
51 | ### 2. Create Compose for MongoDB service with IBM Cloud _or deploy one in Kubernetes_
52 |
53 | Create the following service:
54 |
55 | * [**Compose for MongoDB**](https://console.bluemix.net/catalog/services/compose-for-mongodb)
56 |
57 | Or deploy it in your Kubernetes cluster:
58 |
59 | ```
60 | $ kubectl apply -f manifests/mongo.yaml
61 | ```
62 |
63 | ### 3. Build your images _(optional)_
64 |
65 | You can choose to build your own images, or use the default one in the `manifests/map-api.yaml`.
66 |
67 | ```
68 | $ cd containers/map-api
69 | $ docker build -t /map-api:1.0 .
70 | $ docker push /map-api:1.0
71 | ```
72 |
73 | ### 4. Configure Deployment files
74 |
75 | * Get the MongoDB URL for your Compose for MongoDB.
76 |
77 | 
78 |
79 | * Create a Secret with your own MongoDB URL. This secret will be used by the Pod in `manifests/map-api.yaml`
80 |
81 | ```
82 | kubectl create secret generic mongodb-url --from-literal=MONGODB_URL=""
83 | ```
84 |
85 | * If you have built your own images in Step 3, change the image name `anthonyamanse/map-api:3.0` in `manifests/map-api.yaml` to your own.
86 |
87 | ### 5. Deploy the application
88 |
89 | * Create the Deployment and service resource for the application.
90 | ```
91 | $ kubectl apply -f manifests/map-api.yaml
92 | ```
93 |
94 | * To check if the Pods are deployed and running.
95 | ```
96 | $ kubectl get pods
97 | NAME READY STATUS RESTARTS AGE
98 | map-api-deployment-4132200164-k5s3c 1/1 Running 0 1m
99 | ```
100 |
101 | * To access your application, you would need the Public IP of your cluster's worker and the NodePort of the Service resource of your application.
102 | ```
103 | $ bx cs workers
104 | OK
105 | ID Public IP Private IP Machine Type State Status Version
106 | kube-dal10-crbbdb1ff6a36846e9b2dfb522a07005af-w1 169.60.XX.XX 10.177.184.196 b1c.16x64 normal Ready 1.7.4_1502*
107 |
108 | $ kubectl get svc map-api
109 | NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
110 | map-api 172.21.138.200 169.46.YY.YY 80:31873/TCP 7d
111 | ```
112 |
113 | Access the application on:
114 | `169.60.XX.XX:31873`
115 |
116 | > Note: or if you have a load balancer, use the IP found in EXTERNAL-IP via `kubectl get svc`: `169.46.YY.YY`
117 |
118 | ### 6. Generate Mock Data
119 |
120 | You'll notice that if you go to the `Stored Events` link in the app (or go to `169.60.XX.XX:31873/main`), you would not find any Events. That is because we haven't added anything to our database yet.
121 |
122 | Generate mock data with the `curl-mockdata.sh` script. This script should do `curl` commands to your app. The mock data contains a set of data that you'd usually find in a map. The map setting is for a conference.
123 |
124 | ```
125 | $ ./curl-mockdata.sh http://169.60.XX.XX:31873
126 | ```
127 |
128 | You should see some output that should say `Saved beacon... etc`.
129 |
130 | * Go back to your browser and view the dashboard again. `169.60.XX.XX:31873/main`. You should now see **Think** and **Index** events.
131 | * You can click on the event to view their floorplan.
132 |
133 | ### 7. Perform API Requests
134 |
135 | APIs for this application is listed in the front page `169.60.XX.XX:31873`.
136 |
137 | You could test some of them using a terminal:
138 |
139 | ```
140 | $ curl http://169.60.XX.XX:31873/beacons
141 |
142 |
143 | $ curl http://169.60.XX.XX:31873/booths
144 |
145 |
146 | $ curl http://169.60.XX.XX:31873/events
147 |
148 | ```
149 |
150 | To get an SVG or PDF of the floor plan of an event:
151 | `http://169.60.XX.XX:31873/svg/<:eventId>` add `.pdf` if you want a PDF version.
152 | * `http://169.60.XX.XX:31873/svg/index` _SVG_
153 | * `http://169.60.XX.XX:31873/svg/think.pdf` _PDF_
154 |
155 | # Learn more
156 |
157 | * **Node.js Code Patterns**: Enjoyed this Code Pattern? Check out our other [Node.js Code Patterns](https://developer.ibm.com/code/technologies/node-js/)
158 |
159 | # License
160 | This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1 (DCO)](https://developercertificate.org/) and the [Apache Software License, Version 2](http://www.apache.org/licenses/LICENSE-2.0.txt).
161 |
162 | [Apache Software License (ASL) FAQ](http://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN)
163 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------