24
RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May 2017

RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP MBaaS Services and RESTful APIs

Integrating with MongoDB and Amazon RDS

Hong Hua, Chad DarbySolution Architects @ GPTE2 May 2017

Page 2: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Agenda

● Create an RHMAP MBaaS Service

● Expose RESTful API for CRUDL operations

● First version: MongoDB for persistence

● Second version: Amazon RDS for persistence

Page 3: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP MBaaS Service

Page 4: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP MBaaS Service

Page 5: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RESTful API

Page 6: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RESTful API

Page 7: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RESTful API

Page 8: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP and MongoDB

Page 9: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP and MongoDB

● Every RHMAP project includes a hosted MongoDB instance

Page 10: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Create a New User - Part 1

var $fh = require('fh-mbaas-api');

users.post('/', function(req, res) { console.log(new Date(), 'In user route POST / req.body=', req.body);

// // TODO: Do your work here //

res.json({'msg': 'POST Success'}); });

Page 11: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Create a New User - Part 2

// Create a single entry/row var options = { "act": "create", "type": "myusers", "fields": req.body };

$fh.db(options, function (err, data) { if (err) { res.json({msg : err}); } else { res.json(data); } });

Page 12: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Create a New User - Testing

Page 13: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Get a List of Users - Part 1

users.get('/', function(req, res) { console.log(new Date(), 'In user route GET / req.body=', req.body);

// // TODO: Do your work here //

res.json({'msg': 'GET Success'}); });

Page 14: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Get a List of Users - Part 2

// get a list of users var options = { "act": "list", "type": "myusers" };

$fh.db(options, function (err, data) { if (err) { res.json({msg : err}); } else { res.json(data); } });

Page 15: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Get a List of Users - Testing

Page 16: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP and Amazon RDS

Page 17: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

RHMAP and Amazon RDS

● Amazon Relational Data Services (RDS)

● Example of connecting to an external data service … could be ANY data service

Page 18: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Create a New User - Set up Connection Pool

var pool = mysql.createPool({ connectionLimit : 10, host : process.env.MYSQL_HOST, database : process.env.MYSQL_DATABASE, user : process.env.MYSQL_USERNAME, password : process.env.MYSQL_PASSWORD});

Page 19: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Create a New User - Perform SQL Insert

users.post('/', function(req, res) {

var values = [req.body.firstname, req.body.lastname]; var theSql = "insert into demo_users set firstname=?, lastname=?";

pool.query(theSql, values, function(err, results) { if (err) { var info = {"status" : "fail", "error" : err}; res.json(info); }

var info = {"status" : "ok", "data" : results}; res.json(info); });

});

Page 20: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Get a List of Users - Perform Query

users.get('/', function(req, res) { var theSql = "select * from demo_users";

pool.query(theSql, function(err, rows) { if (err) { var result = {"status" : "fail","error" : err}; res.json(result); }

var result = {"status" : "ok", "data" : rows};

res.json(result); });

Page 21: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

Get List of Users - Test

Page 22: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

That’s all folks?

Page 23: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May

THANK YOUplus.google.com/+RedHat

linkedin.com/company/red-hat

youtube.com/user/RedHatVideos

facebook.com/redhatinc

twitter.com/RedHatNews

Chad Darby

Hong Hua

We love your feedback!

Page 24: RHMAP MBaaS Services and RESTful APIs - Red Hat...RHMAP MBaaS Services and RESTful APIs Integrating with MongoDB and Amazon RDS Hong Hua, Chad Darby Solution Architects @ GPTE 2 May