├── .eslintrc.js ├── .gitignore ├── .travis.yml ├── .yamllint.yml ├── CONTRIBUTING.md ├── LICENSE ├── MAINTAINERS.md ├── README.md ├── containers └── map-api │ ├── Dockerfile │ ├── app.js │ ├── models │ ├── beacon.js │ ├── booth.js │ └── event.js │ ├── package.json │ ├── public │ ├── css │ │ └── style.css │ └── index.html │ ├── routes │ ├── beacons.js │ ├── booths.js │ ├── events.js │ ├── renderSvg.js │ └── svg.js │ └── views │ └── main.ejs ├── curl-mockdata.sh ├── images ├── mongo-architecture.png ├── mongo-command.png ├── mongo-ssl.png └── mongo-url.png ├── manifests ├── map-api.yaml └── mongo.yaml ├── mockdata-index.js ├── mockdata-think.js ├── scripts └── resources.sh └── tests ├── test-csslint.sh ├── test-docker.sh ├── test-eslint.sh ├── test-html-lint.sh ├── test-shellcheck.sh └── test-yamllint.sh /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "env": { 3 | "es6": true, 4 | "node": true, 5 | "mocha": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": 2017, 10 | "sourceType": "module" 11 | }, 12 | "rules": { 13 | "indent": [ 14 | "error", 15 | 2 16 | ], 17 | "linebreak-style": [ 18 | "error", 19 | "unix" 20 | ], 21 | "semi": [ 22 | "error", 23 | "always" 24 | ], 25 | "no-console" : "off", 26 | "guard-for-in": 0, 27 | "prefer-promise-reject-errors": 2, 28 | "no-invalid-this": 0, 29 | }, 30 | "globals": { 31 | "jasmine": true 32 | }, 33 | "overrides": [ 34 | { 35 | "files": "containers/map-api/test/*.js", 36 | "rules": { 37 | "no-unused-vars": "off" 38 | } 39 | } 40 | ] 41 | }; 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: lts/* 4 | sudo: required 5 | 6 | services: 7 | - docker 8 | 9 | 10 | before_install: 11 | - sudo apt-get install shellcheck 12 | - sudo pip install yamllint 13 | - git clone https://github.com/IBM/pattern-ci 14 | 15 | before_script: 16 | - "./pattern-ci/tests/shellcheck-lint.sh" 17 | - "./pattern-ci/tests/yaml-lint.sh" 18 | 19 | jobs: 20 | include: 21 | - script: ./tests/test-docker.sh 22 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This is an open source project, and we appreciate your help! 4 | 5 | We use the GitHub issue tracker to discuss new features and non-trivial bugs. 6 | 7 | In addition to the issue tracker, [#journeys on 8 | Slack](https://dwopen.slack.com) is the best way to get into contact with the 9 | project's maintainers. 10 | 11 | To contribute code, documentation, or tests, please submit a pull request to 12 | the GitHub repository. Generally, we expect two maintainers to review your pull 13 | request before it is approved for merging. For more details, see the 14 | [MAINTAINERS](MAINTAINERS.md) page. 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/IBM/kubernetes-mongoose.svg?branch=master)](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 | ![](/images/mongo-architecture.png) 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 | ![](https://github.com/IBM/pattern-utils/blob/master/compose-dbs/mongo-url-string.png?raw=true) 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 | -------------------------------------------------------------------------------- /containers/map-api/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:8-alpine 2 | 3 | COPY app.js / 4 | COPY package.json / 5 | COPY public /public 6 | COPY models /models 7 | COPY routes /routes 8 | COPY views /views 9 | 10 | RUN npm install 11 | 12 | EXPOSE 8080 13 | 14 | CMD ["node", "app.js"] 15 | -------------------------------------------------------------------------------- /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/models/beacon.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | // eslint-disable-next-line 4 | let beaconSchema = mongoose.Schema({ 5 | beaconId: {type: String, unique: true}, 6 | x: Number, 7 | y: Number, 8 | minCount: Number, 9 | maxCount: Number, 10 | }); 11 | 12 | module.exports = mongoose.model("Beacon", beaconSchema); 13 | -------------------------------------------------------------------------------- /containers/map-api/models/booth.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | // eslint-disable-next-line 4 | let boothSchema = mongoose.Schema({ 5 | boothId: {type: String, unique: true}, 6 | unit: String, 7 | description: String, 8 | measurementUnit: String, 9 | shape: {type: Object, required: true}, 10 | contact: String, 11 | }); 12 | 13 | module.exports = mongoose.model("Booth", boothSchema); 14 | -------------------------------------------------------------------------------- /containers/map-api/models/event.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | let Booths = require("../models/booth"); 4 | let Beacons = require("../models/beacon"); 5 | 6 | // eslint-disable-next-line 7 | let eventSchema = mongoose.Schema({ 8 | eventId: {type: String, unique: true}, 9 | eventName: String, 10 | location: String, 11 | x: Number, 12 | y: Number, 13 | startDate: Date, 14 | endDate: Date, 15 | map: [Booths.schema], 16 | beacons: [Beacons.schema], 17 | }); 18 | 19 | module.exports = mongoose.model("Event", eventSchema); 20 | -------------------------------------------------------------------------------- /containers/map-api/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kubernetes-mongoose", 3 | "main": "app.js", 4 | "description": "kubernetes-mongoose", 5 | "version": "1.0.0", 6 | "private": false, 7 | "engines": { 8 | "node": "6.*" 9 | }, 10 | "scripts": { 11 | "start": "node app.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/IBM/kubernetes-mongoose" 16 | }, 17 | "dependencies": { 18 | "assert": "^1.4.x", 19 | "body-parser": "^1.17.x", 20 | "cors": "^2.8.x", 21 | "ejs": "^2.5.7", 22 | "express": "^4.15.x", 23 | "fs": "~0.0.1", 24 | "mongoose": "^5.2.x", 25 | "pdfkit": "^0.8.3", 26 | "router": "^1.3.2", 27 | "svg-to-pdfkit": "^0.1.6" 28 | }, 29 | "devDependencies": { 30 | "chai": "^3.5.0", 31 | "chai-http": "^4.2.0", 32 | "mocha": "^5.2.0" 33 | }, 34 | "author": "Anthony Amanse", 35 | "license": "Apache-2.0" 36 | } 37 | -------------------------------------------------------------------------------- /containers/map-api/public/css/style.css: -------------------------------------------------------------------------------- 1 | #header { 2 | display: flex; 3 | justify-content: center; 4 | height: 150px; 5 | background-color: #68fce6; 6 | font-family: 'Open Sans'; 7 | color: #000 ; 8 | align-items: center; 9 | flex-direction: column; 10 | } 11 | 12 | #main svg { 13 | padding-top: 100px; 14 | display: block; 15 | margin: auto; 16 | } 17 | 18 | #main svg text { 19 | font-size: 14pt; 20 | font-family: 'Open Sans'; 21 | } 22 | 23 | #main { 24 | padding-top: 50px; 25 | display: block; 26 | font-family: 'Open Sans'; 27 | text-align: center; 28 | margin: auto; 29 | } 30 | 31 | #main ul { 32 | font-size: 12pt; 33 | display: inline; 34 | align-items: center; 35 | } 36 | 37 | #main li { 38 | margin-top: 12pt; 39 | } 40 | 41 | #main h2 { 42 | margin-bottom: -25px; 43 | } -------------------------------------------------------------------------------- /containers/map-api/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Main Page 4 | 5 | 6 | 7 | 8 | 16 |
17 |

GET

18 |
    19 |
  • /beacons
  • 20 |
  • /booths
  • 21 |
  • /events
  • 22 |
  • /beacons/:beaconId
  • 23 |
  • /booths/:boothId
  • 24 |
  • /events/:eventId
  • 25 |
  • /svg/:eventId
  • 26 |
  • /svg/<:eventId>.pdf
  • 27 |
28 |

POST

29 |
    30 |
  • /beacons/add
  • 31 |
  • /booths/add
  • 32 |
  • /events/add
  • 33 |
34 |
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/booths.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | 4 | const Booths = require("../models/booth"); 5 | 6 | // endpoints for booth 7 | router.post("/add", function(req, res) { 8 | // JSON in req.body 9 | // Insert input validation 10 | let addBooth = new Booths(req.body); 11 | addBooth.save(function(err) { 12 | if (err) { 13 | res.send(err); 14 | } else { 15 | res.send("Saved booth."); 16 | } 17 | }); 18 | }); 19 | 20 | router.get("/", function(req, res) { 21 | Booths.find(function(err, booths) { 22 | if (err) { 23 | res.send(err); 24 | } else { 25 | res.send(booths); 26 | } 27 | }); 28 | }); 29 | 30 | router.get("/:boothId", function(req, res) { 31 | Booths.findOne(req.params, function(err, booth) { 32 | if (err) { 33 | res.send(err); 34 | } else if (booth) { 35 | res.send(booth); 36 | } else { 37 | res.send("Booth not found..."); 38 | } 39 | }); 40 | }); 41 | 42 | module.exports = router; 43 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 = "" + 74 | content + ""; 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 | -------------------------------------------------------------------------------- /containers/map-api/views/main.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | Map 4 | 5 | 6 | 7 | 8 | 17 |
18 | <% if (svg) { %> 19 | <%- svg %> 20 | <% } else if (list) %> 21 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /images/mongo-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/kubernetes-mongoose/158af61ed08e0396ff35e6cff3c76374ebcbf7a3/images/mongo-architecture.png -------------------------------------------------------------------------------- /images/mongo-command.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/kubernetes-mongoose/158af61ed08e0396ff35e6cff3c76374ebcbf7a3/images/mongo-command.png -------------------------------------------------------------------------------- /images/mongo-ssl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/kubernetes-mongoose/158af61ed08e0396ff35e6cff3c76374ebcbf7a3/images/mongo-ssl.png -------------------------------------------------------------------------------- /images/mongo-url.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/kubernetes-mongoose/158af61ed08e0396ff35e6cff3c76374ebcbf7a3/images/mongo-url.png -------------------------------------------------------------------------------- /manifests/map-api.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | metadata: 5 | name: map-api-test 6 | labels: 7 | app: map-api 8 | spec: 9 | ports: 10 | - port: 80 11 | protocol: TCP 12 | targetPort: 8080 13 | type: LoadBalancer 14 | selector: 15 | app: map-api-test 16 | --- 17 | apiVersion: extensions/v1beta1 18 | kind: Deployment 19 | metadata: 20 | name: map-api-deployment-test 21 | labels: 22 | app: map-api 23 | spec: 24 | strategy: 25 | type: Recreate 26 | template: 27 | metadata: 28 | labels: 29 | app: map-api-test 30 | spec: 31 | containers: 32 | - image: anthonyamanse/map-api:3.0 33 | imagePullPolicy: Always 34 | name: map-api 35 | env: 36 | - name: MONGODB_URL 37 | valueFrom: 38 | secretKeyRef: 39 | name: mongodb-url 40 | key: MONGODB_URL 41 | ports: 42 | - containerPort: 8080 43 | -------------------------------------------------------------------------------- /manifests/mongo.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | metadata: 5 | name: mongodb 6 | labels: 7 | app: mongodb 8 | spec: 9 | ports: 10 | - port: 27017 11 | targetPort: 27017 12 | type: ClusterIP 13 | selector: 14 | app: mongodb 15 | --- 16 | apiVersion: extensions/v1beta1 17 | kind: Deployment 18 | metadata: 19 | name: mongodb 20 | labels: 21 | app: mongodb 22 | spec: 23 | strategy: 24 | type: Recreate 25 | template: 26 | metadata: 27 | labels: 28 | app: mongodb 29 | spec: 30 | containers: 31 | - image: mongo 32 | name: mongodb 33 | ports: 34 | - containerPort: 27017 35 | # Note: 36 | # For testing, this Mongo instance will not 37 | # persist the data and will stay in the container. 38 | # When the container is deleted or restarted, data will be lost. 39 | # If you want a persistent Mongo instance, 40 | # mount a volume in /data/db 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /scripts/resources.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script contains functions used by many of the scripts found in scripts/ 4 | # and tests/. 5 | 6 | test_failed(){ 7 | echo -e >&2 "\033[0;31m$1 test failed!\033[0m" 8 | exit 1 9 | } 10 | 11 | test_passed(){ 12 | echo -e "\033[0;32m$1 test passed!\033[0m" 13 | } 14 | 15 | is_pull_request(){ 16 | if [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then 17 | echo -e "\033[0;33mPull Request detected; not running $1!\033[0m" 18 | exit 0 19 | fi 20 | } 21 | -------------------------------------------------------------------------------- /tests/test-csslint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | if find . -path ./node_modules -prune -o -name '*.css' -print0 | xargs -n1 -0 ./node_modules/.bin/csslint --ignore=order-alphabetical; then 7 | test_passed "$0" 8 | else 9 | test_failed "$0" 10 | fi 11 | -------------------------------------------------------------------------------- /tests/test-docker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | main(){ 7 | for DIRECTORY in ./containers/*; do 8 | pushd "$DIRECTORY" 9 | if ! docker build --quiet .; then 10 | test_failed "$0" 11 | fi 12 | popd 13 | done 14 | test_passed "$0" 15 | } 16 | 17 | main "$@" 18 | -------------------------------------------------------------------------------- /tests/test-eslint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | if find . -path ./node_modules -prune -o -name '*.js' -print0 | xargs -n1 -0 ./node_modules/.bin/eslint; then 7 | test_passed "$0" 8 | else 9 | test_failed "$0" 10 | fi 11 | -------------------------------------------------------------------------------- /tests/test-html-lint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | if find . -path ./node_modules -prune -o \( -name '*.htm' -o -name '*.html' \) -print0 | xargs -n1 -0 -I % ./node_modules/.bin/html-lint % --verbose --strict; then 7 | test_passed "$0" 8 | else 9 | test_failed "$0" 10 | fi 11 | -------------------------------------------------------------------------------- /tests/test-shellcheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | if find . -name '*.sh' -print0 | xargs -n1 -0 shellcheck -x -s bash; then 7 | test_passed "$0" 8 | else 9 | test_failed "$0" 10 | fi 11 | -------------------------------------------------------------------------------- /tests/test-yamllint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # shellcheck disable=SC1090 4 | source "$(dirname "$0")"/../scripts/resources.sh 5 | 6 | if find . -path ./node_modules -prune -o \( -name '*.yml' -o -name '*.yaml' \) -print0 | xargs -n1 -0 yamllint -c .yamllint.yml; then 7 | test_passed "$0" 8 | else 9 | test_failed "$0" 10 | fi 11 | --------------------------------------------------------------------------------