├── .ebextensions ├── elasticache.config ├── nodecommand.config └── options.config ├── .eslintrc ├── .gitignore ├── LICENSES.md ├── README.md ├── app.js ├── http.js ├── images ├── azure │ ├── create.png │ ├── new-deploy-credentials-v2.png │ ├── new-deploy-credentials.png │ ├── new-redis-cache.png │ ├── new-web-app.png │ ├── redis-access-keys.png │ ├── redis-access-ports.png │ ├── redis-list.png │ ├── redis-properties.png │ ├── settings.png │ ├── web-app-settings.png │ ├── web-list.png │ └── web-settings.png └── bluemix │ ├── 2016 │ ├── 1-Space.jpg │ ├── 10-Add-Alias.jpg │ ├── 11-Alias-Created.jpg │ ├── 12-Add-Repo.jpg │ ├── 13-git-success.jpg │ ├── 14-git-location.jpg │ ├── 15-build-and-deploy.jpg │ ├── 2-Create-Application.jpg │ ├── 3-Choose-Node-JS-SDK.jpg │ ├── 4-App-Details.jpg │ ├── 5-Connections.jpg │ ├── 6-Searchfor-redis.jpg │ ├── 7-Choose-plan.jpg │ ├── 8-Restage.jpg │ └── 9-overview.jpg │ ├── add-redis-cloud.png │ ├── app-add-services.png │ ├── app-dashboard.png │ ├── app-env-vars.png │ ├── app-is-staging.png │ ├── app-overview.png │ ├── app-redis-dashboard.png │ ├── app-redis-overview.png │ ├── create-git-success.png │ ├── create-git.png │ ├── git-url-overview.png │ ├── how-get-started-continue.png │ ├── how-get-started.png │ ├── kind-of-app.png │ ├── name-for-app.png │ ├── restage-app.png │ └── start-dashboard.png ├── package.json ├── redis.js └── settings.js /.ebextensions/elasticache.config: -------------------------------------------------------------------------------- 1 | ## 2 | # This configuration adds an ElastiCache cluster to the environment. 3 | # It creates an IAM user with the permisisons required to discover the elasticache nodes. 4 | # It provides a cfn-hup responder if the cache changes to rewrite the file 5 | # It writes a file out to: /var/nodelist 6 | # containing the cache nodes on startup and when the cache changes (through a cfn/eb update) 7 | ## 8 | 9 | Resources: 10 | MyElastiCache: 11 | Type: AWS::ElastiCache::CacheCluster 12 | Properties: 13 | CacheNodeType: 14 | Fn::GetOptionSetting: 15 | OptionName : CacheNodeType 16 | DefaultValue: cache.m1.small 17 | NumCacheNodes: 18 | Fn::GetOptionSetting: 19 | OptionName : NumCacheNodes 20 | DefaultValue: 1 21 | Engine: 22 | Fn::GetOptionSetting: 23 | OptionName : Engine 24 | DefaultValue: redis 25 | CacheSecurityGroupNames: 26 | - Ref: MyCacheSecurityGroup 27 | MyCacheSecurityGroup: 28 | Type: AWS::ElastiCache::SecurityGroup 29 | Properties: 30 | Description: "Lock cache down to webserver access only" 31 | MyCacheSecurityGroupIngress: 32 | Type: AWS::ElastiCache::SecurityGroupIngress 33 | Properties: 34 | CacheSecurityGroupName: 35 | Ref: MyCacheSecurityGroup 36 | EC2SecurityGroupName: 37 | Ref: AWSEBSecurityGroup 38 | AWSEBAutoScalingGroup : 39 | Metadata : 40 | ElastiCacheConfig : 41 | CacheName : 42 | Ref : MyElastiCache 43 | CacheSize : 44 | Fn::GetOptionSetting: 45 | OptionName : NumCacheNodes 46 | DefaultValue: 1 47 | WebServerUser : 48 | Type : AWS::IAM::User 49 | Properties : 50 | Path : "/" 51 | Policies: 52 | - 53 | PolicyName: root 54 | PolicyDocument : 55 | Statement : 56 | - 57 | Effect : Allow 58 | Action : 59 | - cloudformation:DescribeStackResource 60 | - cloudformation:ListStackResources 61 | - elasticache:DescribeCacheClusters 62 | Resource : "*" 63 | WebServerKeys : 64 | Type : AWS::IAM::AccessKey 65 | Properties : 66 | UserName : 67 | Ref: WebServerUser 68 | 69 | Outputs: 70 | WebsiteURL: 71 | Description: sample output only here to show inline string function parsing 72 | Value: | 73 | http://`{ "Fn::GetAtt" : [ "AWSEBLoadBalancer", "DNSName" ] }` 74 | MyElastiCacheName: 75 | Description: Name of the elasticache 76 | Value: 77 | Ref : MyElastiCache 78 | NumCacheNodes: 79 | Description: Number of cache nodes in MyElastiCache 80 | Value: 81 | Fn::GetOptionSetting: 82 | OptionName : NumCacheNodes 83 | DefaultValue: 1 84 | 85 | files: 86 | "/etc/cfn/cfn-credentials" : 87 | content : | 88 | AWSAccessKeyId=`{ "Ref" : "WebServerKeys" }` 89 | AWSSecretKey=`{ "Fn::GetAtt" : ["WebServerKeys", "SecretAccessKey"] }` 90 | mode : "000400" 91 | owner : root 92 | group : root 93 | 94 | "/etc/cfn/get-cache-nodes" : 95 | content : | 96 | # Define environment variables for command line tools 97 | export AWS_ELASTICACHE_HOME="/home/ec2-user/elasticache/$(ls /home/ec2-user/elasticache/)" 98 | export AWS_CLOUDFORMATION_HOME=/opt/aws/apitools/cfn 99 | export PATH=$AWS_CLOUDFORMATION_HOME/bin:$AWS_ELASTICACHE_HOME/bin:$PATH 100 | export AWS_CREDENTIAL_FILE=/etc/cfn/cfn-credentials 101 | export JAVA_HOME=/usr/lib/jvm/jre 102 | 103 | # Grab the Cache node names and configure the PHP page 104 | cfn-list-stack-resources `{ "Ref" : "AWS::StackName" }` --region `{ "Ref" : "AWS::Region" }` | grep MyElastiCache | awk '{print $3}' | xargs -I {} elasticache-describe-cache-clusters {} --region `{ "Ref" : "AWS::Region" }` --show-cache-node-info | grep CACHENODE | awk '{print $4 ":" $6}' > `{ "Fn::GetOptionSetting" : { "OptionName" : "NodeListPath", "DefaultValue" : "/var/nodelist" } }` 105 | mode : "000500" 106 | owner : root 107 | group : root 108 | 109 | "/etc/cfn/hooks.d/cfn-cache-change.conf" : 110 | "content": | 111 | [cfn-cache-size-change] 112 | triggers=post.update 113 | path=Resources.AWSEBAutoScalingGroup.Metadata.ElastiCacheConfig 114 | action=/etc/cfn/get-cache-nodes 115 | runas=root 116 | 117 | sources : 118 | "/home/ec2-user/elasticache" : "https://s3.amazonaws.com/elasticache-downloads/AmazonElastiCacheCli-latest.zip" 119 | 120 | commands: 121 | make-elasticache-executable: 122 | command: chmod -R ugo+x /home/ec2-user/elasticache/*/bin/* 123 | 124 | packages : 125 | "yum" : 126 | "aws-apitools-cfn" : [] 127 | 128 | container_commands: 129 | initial_cache_nodes: 130 | command: /etc/cfn/get-cache-nodes 131 | -------------------------------------------------------------------------------- /.ebextensions/nodecommand.config: -------------------------------------------------------------------------------- 1 | option_settings: 2 | - namespace: aws:elasticbeanstalk:container:nodejs 3 | option_name: NodeCommand 4 | value: "npm start" 5 | -------------------------------------------------------------------------------- /.ebextensions/options.config: -------------------------------------------------------------------------------- 1 | option_settings: 2 | "aws:elasticbeanstalk:customoption": 3 | CacheNodeType : cache.t1.micro 4 | NumCacheNodes : 1 5 | Engine : redis 6 | CachePort : 6379 7 | NodeListPath : /tmp/nodelist 8 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | env: 2 | node: true 3 | 4 | globals: 5 | every: true 6 | after: true 7 | constantly: true 8 | 9 | rules: 10 | camelcase: [2, {properties: "always"}] 11 | comma-dangle: "always-multiline" 12 | comma-spacing: [2, {before: false, after: true}] 13 | comma-style: [2, "last"] 14 | handle-callback-err: [2, "^.*(e|E)rr" ] 15 | indent: [2, 2] 16 | key-spacing: [2, { beforeColon: false, afterColon: true }] 17 | max-depth: [1, 3] 18 | max-len: [1, 80, 4] 19 | max-nested-callbacks: [1, 3] 20 | no-cond-assign: 2 21 | no-constant-condition: 2 22 | no-dupe-args: 2 23 | no-dupe-keys: 2 24 | no-else-return: 2 25 | no-empty: 2 26 | no-lonely-if: 2 27 | no-multiple-empty-lines: 2 28 | no-nested-ternary: 2 29 | no-reserved-keys: 2 30 | no-self-compare: 2 31 | no-sync: 1 32 | no-throw-literal: 2 33 | no-underscore-dangle: 0 34 | quote-props: [2, "as-needed"] 35 | quotes: [2, "double", "avoid-escape"] 36 | radix: 2 37 | semi-spacing: [2, {before: false, after: true}] 38 | semi: [2, "always"] 39 | space-after-keywords: [2, "always"] 40 | space-before-blocks: [2, "always"] 41 | space-before-function-paren: [1, "never"] 42 | space-in-parens: [2, "never"] 43 | spaced-line-comment: [1, "always"] 44 | strict: [2, "global"] 45 | valid-jsdoc: 2 46 | yoda: [2, "never"] 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | 4 | -------------------------------------------------------------------------------- /LICENSES.md: -------------------------------------------------------------------------------- 1 | # Licenses 2 | 3 | ## Express.js 4 | License: MIT 5 | https://github.com/strongloop/express/blob/master/LICENSE 6 | 7 | ## body-parser 8 | License: MIT 9 | https://github.com/expressjs/body-parser/blob/master/LICENSE 10 | 11 | ## redis 12 | License: MIT 13 | https://github.com/NodeRedis/node_redis#license---mit-license 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Intel IoT Examples Datastore 2 | 3 | ## Introduction 4 | 5 | The Intel IoT Examples Datastore is intended to provide a simple data store for sample applications that are part of the Intel IoT Starter Kit examples. 6 | 7 | It is a Node.js* application written using Express* ([https://github.com/strongloop/express](https://github.com/strongloop/express)). It uses a Redis* ([http://redis.io](http://redis.io)) data store. 8 | 9 | Although this data storage application is developed to deploy on Microsoft* Azure\*, IBM\* Bluemix\*, or Amazon Web Services* (AWS) Elastic Beanstalk*, since it is based on commonly used open-source modules, it should be easy to deploy on many different cloud hosts. 10 | 11 | ## How it works 12 | 13 | The Intel IoT Examples Datastore application provides a simple REST API that allows authorized callers to store either log data or incremental counter data. 14 | 15 | It exposes two sets of routes, both using basic token authentication. 16 | 17 | To authorize, provide the `AUTH_TOKEN` you set in the `X-Auth-Token` HTTP header. 18 | 19 | Multiple clients can share the same Intel IoT Examples Datastore by simply using a different route key for each application. 20 | 21 | ### Counter 22 | 23 | The counter routes are used to increment an increasing counter. 24 | 25 | - `GET /counter/:key` - gets the current value of a counter 26 | - `GET /counter/:key/inc` - increments a counter by 1 27 | 28 | ### Logger 29 | 30 | The logger routes wrap Redis* lists, and are used to keep a linear backlog of values. 31 | 32 | - `GET /logger/:key` - gets the latest value of a log 33 | - `PUT /logger/:key` - adds a value to a log (`value` parameter in the POST body) 34 | - `GET /logger/:key/all` - returns all values of a log 35 | 36 | ## Local setup 37 | 38 | To install the application dependencies, use the following command: 39 | 40 | $ npm install 41 | 42 | You also need to have Redis* installed. 43 | For more information, see the Redis* download page: 44 | 45 | http://redis.io/download 46 | 47 | ## Configuration 48 | 49 | Primary configuration for the application takes the form of the following `ENV` variables: 50 | 51 | - `REDIS_URL` - the URL to use for Redis* communication 52 | - `REDIS_AUTH` - authentication token for the Redis* backend 53 | - `PORT` - port to serve the application on (defaults to `3000`) 54 | - `AUTH_TOKEN` - authentication token for clients to use 55 | 56 | ## Deployment - Microsoft* Azure* 57 | 58 | This guide covers setting up a deployment environment for the Intel IoT Examples Datastore on Microsoft* Azure*. 59 | 60 | For other platforms, refer to the platform documentation. 61 | 62 | Before we begin, ensure you have a Microsoft* Azure* account: 63 | 64 | [https://portal.azure.com/signin/index](https://portal.azure.com/signin/index) 65 | 66 | ### Create a new web application 67 | 68 | 1. Click **New** and select **Web + Mobile > Web App**.
69 | ![Settings](images/azure/new-web-app.png) 70 | 2. Enter the name for your new web application. 71 | 3. Click **Create**.
72 | Your new web application is created. 73 | 74 | ### Create a new Redis* cache 75 | 76 | 1. Click **New** and select **Data + Storage > Redis Cache**.
77 | ![Settings](images/azure/new-redis-cache.png) 78 | 2. Enter the name for your new Redis* cache. 79 | 3. Click **Create**.
80 | Your new Redis* cache is created. 81 | 82 | ### Determine settings for Redis* 83 | 84 | 1. In the left sidebar, select **Redis Caches** and click the name of the new Redis* cache you created in the previous step.
85 | ![Settings](images/azure/redis-list.png) 86 | 2. Select **All settings > Properties**.
87 | ![Settings](images/azure/redis-properties.png) 88 | 3. Write down or copy the value in the **HOST NAME** field so you can use it for the `REDIS_URL` setting for the web application. 89 | 4. Select **All settings > Access keys**.
90 | ![Settings](images/azure/redis-access-keys.png) 91 | 5. Write down or copy the value in the **PRIMARY** or **SECONDARY** field so you can use it for the `REDIS_AUTH` setting for the web application. 92 | 6. Select **All settings > Access Ports**.
93 | ![Settings](images/azure/redis-access-ports.png) 94 | 7. Set the **Allow access only via SSL** option to **No**. 95 | 8. Click **Save**. 96 | 97 | ### Configure the web application 98 | 99 | 1. In the left sidebar, select **Web Apps** and click the name of the new web application you previously created.
100 | ![Settings](images/azure/web-list.png) 101 | 2. Select **All settings > Application settings**.
102 | ![Settings](images/azure/web-app-settings.png) 103 | 3. Scroll down to the **App settings** section. 104 | 4. Specify `REDIS_URL` by typing the value from the **HOST NAME** field above. 105 | 5. Specify `REDIS_AUTH` by typing the value from the **PRIMARY** field above. 106 | 6. Specify `AUTH_TOKEN` by typing whatever shared secret key you want. 107 | 7. Click **Save**. 108 | 109 | Note: you don't need to set `PORT`, as the Microsoft* Azure* platform does that automatically. 110 | 111 | ### Deploy the web application 112 | 113 | 1. In the left sidebar, select **App Services** and click the name of the new web application you previously created.
114 | ![Settings](images/azure/new-deploy-credentials-v2.png) 115 | 2. Click **Settings**. 116 | 3. Click **Deployment Source**. 117 | 4. Click **Choose Source**. 118 | 5. Click **External Repository**. 119 | 6. Click in the **Repository URL** field, and enter the following value: [https://github.com/intel-iot-devkit/intel-iot-examples-datastore](https://github.com/intel-iot-devkit/intel-iot-examples-datastore) 120 | 7. Click **OK**. 121 | 122 | This means your application has been deployed to the Microsoft* Azure* cloud. 123 | 124 | ## Deployment - IBM* Bluemix* 125 | 126 | This guide covers setting up a deployment environment for the Intel IoT Examples Datastore on IBM* Bluemix*. 127 | 128 | For other platforms, refer to the platform documentation. 129 | 130 | Before we begin, please ensure you have an IBM* Bluemix* account: 131 | 132 | [https://console.ng.bluemix.net/](https://console.ng.bluemix.net/) 133 | 134 | ### Create a new web application 135 | 136 | 1. Choose the **Space** where you want to add a new web application, or create a new one.
137 | ![Settings](images/bluemix/2016/1-Space.jpg) 138 | 2. Click **Create Application**.
139 | ![Settings](images/bluemix/2016/2-Create-Application.jpg) 140 | 3. Scroll down to Cloud Foundry Apps and choose **Node JS SDK**.
141 | ![Settings](images/bluemix/2016/3-Choose-Node-JS-SDK.jpg) 142 | 4. Fill in your app details.
143 | ![Settings](images/bluemix/2016/4-App-Details.jpg) 144 | 145 | 146 | ### Create new Compose for Redis connection 147 | 148 | 1. You will be redirected to the **Getting Started** page and your app will start, once there, click on **Connections** and click on **Connect New**.
149 | ![Settings](images/bluemix/2016/5-Connections.jpg) 150 | 2. Search **Redis** in the filter bar at the top of the page and click on **Compose for Redis**. 151 | ![Settings](images/bluemix/2016/6-Searchfor-redis.jpg) 152 | 3. Currently there is only one plan to choose from, head to the bottom of the page and click **Create**.
153 | ![Settings](images/bluemix/2016/7-Choose-plan.jpg) 154 | 4. When you are prompted to restage the application, click on the **Restage** button.
155 | ![Settings](images/bluemix/2016/8-Restage.jpg) 156 | 5. Once restaged click on the overview tab to take you back to the overview.
157 | ![Settings](images/bluemix/2016/9-overview.jpg) 158 | 159 | ### Configure the web application 160 | 161 | 1. Scroll to the bottom of the page and click on the **Add GIT Repo and Pipeline** button. If you have not already added an alias to your IBM ID a new window opens up, add a new alias, accept the terms and click **Create**. This alias needs to be lowercase.
162 | ![Settings](images/bluemix/2016/10-Add-Alias.jpg) 163 | 2. Once your alias has been created a confirmation page will show. Click **Continue**.
164 | ![Settings](images/bluemix/2016/11-Alias-Created.jpg) 165 | 3. Click on the **Add GIT Repo and Pipeline** button again, ensure that **Populate the repo with the starter app package and enable the Build & Deploy pipeline** is checked and click **Continue**.
166 | ![Settings](images/bluemix/2016/12-Add-Repo.jpg) 167 | 4. Once your GIT repo has been created you will be shown a success page, click **Close**.
168 | ![Settings](images/bluemix/2016/13-git-success.jpg) 169 | 5. Your new GIT url is shown in the bottom right of the page.
170 | ![Settings](images/bluemix/2016/14-git-location.jpg) 171 | 172 | ### Configure Git* 173 | 174 | In the directory you're using for this repository, use the command line to run the following Git* command that adds an IBM* Bluemix* server for deployment: 175 | 176 | git remote add bluemix 177 | 178 | where `` is the value displayed at the top right of the **Overview** page. 179 | 180 | Now you are ready to deploy the application. 181 | 182 | ### Deploy the web application 183 | 184 | To deploy your web application, run the following Git* command: 185 | 186 | git push bluemix master -f 187 | 188 | You are prompted for your IBM* Bluemix* username and password. 189 | 190 | Once the operation is complete, your application has been deployed to the IBM* Bluemix* cloud. 191 | 192 | Note: it may take a moment for your web application to restart. 193 | 194 | ## Deployment - AWS* Elastic Beanstalk* 195 | 196 | This guide covers setting up a deployment environment for the Intel IoT Examples Datastore on AWS* using Elastic Beanstalk*. 197 | 198 | For other platforms, refer to the platform documentation. 199 | 200 | Before we begin, please ensure you have an AWS* account: 201 | 202 | [http://aws.amazon.com/](http://aws.amazon.com/) 203 | 204 | ### Install the AWS* Elastic Beanstalk Command Line Interface (EB CLI*) tools 205 | 206 | To deploy the Intel IoT Examples Datastore on AWS* using Elastic Beanstalk*, you must first install the EB CLI\* tools as explained below: 207 | 208 | [http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html) 209 | 210 | ### Create a new web application 211 | 212 | In the directory you're using for this repository, use the command line to run the following EB CLI* command that creates a new AWS* application: 213 | 214 | eb init 215 | 216 | When prompted for the Elastic Beanstalk* region, enter the number of the region you wish, or choose the default. 217 | 218 | When prompted for the Elastic Beanstalk* application to use, enter the number corresponding to the **Create new Application** option. 219 | 220 | When prompted, enter **y** if you want to set up an SSH connection to your instances. Enter **n** if you do not want to set up an SSH connection. 221 | 222 | ### Deploy your web application 223 | 224 | Now, you can create your running environment by entering the following command: 225 | 226 | eb create 227 | 228 | When prompted for the Elastic Beanstalk* environment name, enter the name of the environment. If you want to accept the default, press **Enter**. 229 | 230 | When prompted for a CNAME prefix, enter the CNAME prefix you want to use. If you want to accept the default, press **Enter**. 231 | 232 | ### Configure your web application 233 | 234 | Most of the configuration settings are automatically determined by the Intel IoT Examples Datastore application itself when running under AWS* Elastic Beanstalk*. To set `AUTH_TOKEN`, enter the following command: 235 | 236 | eb setenv AUTH_TOKEN= 237 | 238 | where `` is your actual authorization token. 239 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var PORT, 4 | AUTH_TOKEN, 5 | HOST, 6 | REDIS_URL, 7 | REDIS_OPTS, 8 | REDIS_PORT; 9 | 10 | var settings = require("./settings"), 11 | redis = require("./redis"), 12 | http = require("./http"); 13 | 14 | // get counter value 15 | http.get("/counter/:key", function(req, res, next) { 16 | redis.hget("counters", req.params.key, function(err, value) { 17 | if (err) { return next(err); } 18 | res.json({ value: +value }); 19 | }); 20 | }); 21 | 22 | // increment/create a new counter 23 | http.get("/counter/:key/inc", function(req, res, next) { 24 | redis.hincrby("counters", req.params.key, 1, function(err, value) { 25 | if (err) { return next(err); } 26 | res.json({ value: value }); 27 | }); 28 | }); 29 | 30 | // get last value from log 31 | http.get("/logger/:key", function(req, res, next) { 32 | redis.lindex(req.params.key, 0, function(err, value) { 33 | if (err) { return next(err); } 34 | res.json({ value: value }); 35 | }); 36 | }); 37 | 38 | // append value to log 39 | http.put("/logger/:key", function(req, res, next) { 40 | redis.lpush(req.params.key, req.body.value, function(err, length) { 41 | if (err) { return next(err); } 42 | res.json({ value: req.body.value }); 43 | }); 44 | }); 45 | 46 | // get all log values 47 | http.get("/logger/:key/all", function(req, res, next) { 48 | redis.lrange(req.params.key, 0, -1, function(err, value) { 49 | if (err) { return next(err); } 50 | res.json({ value: value }); 51 | }); 52 | }); 53 | 54 | // start HTTP server once connected to Redis 55 | redis.once("ready", http.listen); 56 | -------------------------------------------------------------------------------- /http.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | /* eslint no-unused-vars: 0 */ 4 | 5 | var app = require("express")(); 6 | var bodyParser = require("body-parser"); 7 | 8 | // parse POST bodies 9 | app.use(bodyParser.json()); 10 | app.use(bodyParser.urlencoded({ extended: true })); 11 | 12 | // content-type 13 | app.use(function(req, res, next) { 14 | res.header("Content-Type", "application/json"); 15 | next(); 16 | }); 17 | 18 | // auth 19 | app.use(function(req, res, next) { 20 | var header = req.header("X-Auth-Token"); 21 | if (header && header === AUTH_TOKEN) { return next(); } 22 | return res.status(401).json({ error: "unauthorized" }); 23 | }); 24 | 25 | function listen() { 26 | // 404 27 | app.use(function(req, res, next) { 28 | res.status(404).json({ error: "not found" }); 29 | }); 30 | 31 | // error handler 32 | app.use(function(err, req, res, next) { 33 | res.status(500).json({ error: err }); 34 | }); 35 | 36 | // start 37 | app.listen(PORT, HOST, function() { 38 | console.log("http server at", HOST, "listening on port", PORT); 39 | }); 40 | } 41 | 42 | module.exports = { 43 | get: app.get.bind(app), 44 | put: app.put.bind(app), 45 | post: app.post.bind(app), 46 | listen: listen 47 | }; 48 | -------------------------------------------------------------------------------- /images/azure/create.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/create.png -------------------------------------------------------------------------------- /images/azure/new-deploy-credentials-v2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/new-deploy-credentials-v2.png -------------------------------------------------------------------------------- /images/azure/new-deploy-credentials.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/new-deploy-credentials.png -------------------------------------------------------------------------------- /images/azure/new-redis-cache.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/new-redis-cache.png -------------------------------------------------------------------------------- /images/azure/new-web-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/new-web-app.png -------------------------------------------------------------------------------- /images/azure/redis-access-keys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/redis-access-keys.png -------------------------------------------------------------------------------- /images/azure/redis-access-ports.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/redis-access-ports.png -------------------------------------------------------------------------------- /images/azure/redis-list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/redis-list.png -------------------------------------------------------------------------------- /images/azure/redis-properties.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/redis-properties.png -------------------------------------------------------------------------------- /images/azure/settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/settings.png -------------------------------------------------------------------------------- /images/azure/web-app-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/web-app-settings.png -------------------------------------------------------------------------------- /images/azure/web-list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/web-list.png -------------------------------------------------------------------------------- /images/azure/web-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/azure/web-settings.png -------------------------------------------------------------------------------- /images/bluemix/2016/1-Space.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/1-Space.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/10-Add-Alias.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/10-Add-Alias.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/11-Alias-Created.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/11-Alias-Created.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/12-Add-Repo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/12-Add-Repo.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/13-git-success.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/13-git-success.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/14-git-location.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/14-git-location.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/15-build-and-deploy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/15-build-and-deploy.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/2-Create-Application.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/2-Create-Application.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/3-Choose-Node-JS-SDK.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/3-Choose-Node-JS-SDK.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/4-App-Details.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/4-App-Details.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/5-Connections.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/5-Connections.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/6-Searchfor-redis.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/6-Searchfor-redis.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/7-Choose-plan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/7-Choose-plan.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/8-Restage.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/8-Restage.jpg -------------------------------------------------------------------------------- /images/bluemix/2016/9-overview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/2016/9-overview.jpg -------------------------------------------------------------------------------- /images/bluemix/add-redis-cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/add-redis-cloud.png -------------------------------------------------------------------------------- /images/bluemix/app-add-services.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-add-services.png -------------------------------------------------------------------------------- /images/bluemix/app-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-dashboard.png -------------------------------------------------------------------------------- /images/bluemix/app-env-vars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-env-vars.png -------------------------------------------------------------------------------- /images/bluemix/app-is-staging.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-is-staging.png -------------------------------------------------------------------------------- /images/bluemix/app-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-overview.png -------------------------------------------------------------------------------- /images/bluemix/app-redis-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-redis-dashboard.png -------------------------------------------------------------------------------- /images/bluemix/app-redis-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/app-redis-overview.png -------------------------------------------------------------------------------- /images/bluemix/create-git-success.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/create-git-success.png -------------------------------------------------------------------------------- /images/bluemix/create-git.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/create-git.png -------------------------------------------------------------------------------- /images/bluemix/git-url-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/git-url-overview.png -------------------------------------------------------------------------------- /images/bluemix/how-get-started-continue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/how-get-started-continue.png -------------------------------------------------------------------------------- /images/bluemix/how-get-started.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/how-get-started.png -------------------------------------------------------------------------------- /images/bluemix/kind-of-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/kind-of-app.png -------------------------------------------------------------------------------- /images/bluemix/name-for-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/name-for-app.png -------------------------------------------------------------------------------- /images/bluemix/restage-app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/restage-app.png -------------------------------------------------------------------------------- /images/bluemix/start-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel-iot-devkit/intel-iot-examples-datastore/3fa179d0616517d254681657859c443a421f560d/images/bluemix/start-dashboard.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "intel-examples-datastore", 3 | "main": "app.js", 4 | "version": "0.2.0", 5 | "private": true, 6 | "description": "A Node.js-based back-end and Redis datastore for Intel IoT Examples", 7 | "scripts": { 8 | "start": "node app.js" 9 | }, 10 | "devDependencies": {}, 11 | "dependencies": { 12 | "body-parser": "^1.13.3", 13 | "express": "^4.13.3", 14 | "redis": "^0.12.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /redis.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | /* eslint camelcase: 0 */ 4 | 5 | var redis = require("redis"); 6 | var client = redis.createClient(REDIS_PORT, REDIS_URL, REDIS_OPTS); 7 | 8 | client.once("ready", function() { console.log("connected to redis"); }); 9 | client.on("error", function(err) { console.error("err:", err); }); 10 | 11 | module.exports = client; 12 | -------------------------------------------------------------------------------- /settings.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | 3 | PORT = (process.env.PORT || 3000); 4 | AUTH_TOKEN = (process.env.AUTH_TOKEN || "DEFAULT_AUTH_TOKEN"); 5 | HOST = (process.env.HOST || "localhost"); 6 | 7 | function isAzure() { 8 | return process.env.VCAP_SERVICES; 9 | } 10 | 11 | function isAWS() { 12 | try { 13 | var stat = fs.statSync('/tmp/nodelist'); 14 | if (stat.isFile()) { 15 | return true; 16 | } 17 | return false; 18 | } catch(err) { 19 | if (err.code == "ENOENT") { 20 | return false 21 | } else throw err; 22 | } 23 | } 24 | 25 | if (isAzure()) { 26 | // application is being run in MS Azure environment 27 | console.log("Running in MS Azure"); 28 | PORT = process.env.VCAP_APP_PORT; 29 | HOST = process.env.VCAP_APP_HOST; 30 | 31 | var services = JSON.parse(process.env.VCAP_SERVICES); 32 | 33 | // look for a service starting with 'redis' 34 | for (var svcName in services) { 35 | if (svcName.match(/^redis/)) { 36 | var redisCreds = services[svcName][0]['credentials']; 37 | 38 | REDIS_URL = redisCreds.hostname; 39 | REDIS_OPTS = { auth_pass: redisCreds.password }, 40 | REDIS_PORT = redisCreds.port; 41 | } 42 | } 43 | } else if (isAWS()) { 44 | // application is being run in AWS Elastic Beanstalk environment 45 | console.log("Running in AWS Elastic Beanstalk"); 46 | var data = fs.readFileSync('/tmp/nodelist', 'UTF8'); 47 | if (data) { 48 | var lines = data.split("\n"); 49 | if (lines && lines[0] && (lines[0].length > 0)) { 50 | var line = lines[0].split(":"); 51 | REDIS_URL = line[0]; 52 | REDIS_OPTS = {} 53 | REDIS_PORT = line[1]; 54 | } 55 | } 56 | } else { 57 | // application is being run in some other environment 58 | console.log("Running in other environment"); 59 | REDIS_URL = process.env.REDIS_URL; 60 | REDIS_OPTS = { auth_pass: process.env.REDIS_AUTH }; 61 | REDIS_PORT = process.env.REDIS_PORT || 6379; 62 | } 63 | --------------------------------------------------------------------------------