Mobilize Your MongoDB: Building MongoDB Mobile Apps with OpenShift PaaS Part III

During Part 2 of this BeerShift series, we went over all of the required dependencies required for the BeerShift application to run. We installed Titanium Studio, Xcode, the Android SDK, and the OpenShift client tools. We also signed up for OpenShift and created an application for our backend REST services and added a mongo database.

In this blog post, we will go over the backend REST services and get everything up and running on the OpenShift cloud. We will also register for a brewerydb.com API key and add it to our application so that we can make remote REST calls to look up beer and brewery information.

Step 1: Sign up for an brewerydb.com API key (it's free)

Instead of storing such a large amount of beer data that can quickly grow out of date, I decided to rely on a third party to retrieve this information. In this sample application, we will only be using the beer name, description, and brewery information. However, the API that we are using provides more information including images that could be incorporated into the application.

In order to sign up for an API key, head on over to http://www.brewerydb.com/developers and sign up for an account.

Once you sign up for an account, you will need to identify the application that you are writing with a brief description. After providing that information, you will receive an API key via email a short time later.

Step 2: Download the BeerShift backend source code

In order to start using the beershift backend source code, we will need to clone the git repository and add it to our IDE. Luckily, Titanium Studio, which we installed during the last blog post, has built in git revision control functionality that makes this process very painless. Start up Titanium Studio and click the import project button on the left hand side of the screen.

Select to import from a Git Repository as New Project and click Next.

In the URI field, enter in git://github.com/beershift/BeerShiftWeb.git

Once this source code has been downloaded, Titanium Studio automatically creates a new project for you. Click on Open Project on the left hand side of the screen and the source code for the project will be visible.

Step 3: Add your OpenShift git repo as a remote to your project

Now that you have a project that contains the source code for the backend services, we need to add your OpenShift application that we created in part 2 of this series to your git configuration. This can be done with a few commands at the terminal prompt.

The first thing we need to do is find the directory that our project was created in. This is typically under the Titanium Studio Workspace location that you set when you started the IDE. If you don't know the location, you can always right click on a file in the project and view the properties for that file.

On my system, the project source files are located at: /Users/gshipley/Documents/Titanium Studio Workspace/BeerShiftWeb

Issue the following commands at a terminal prompt:

$ cd /Users/gshipley/Documents/Titanium Studio Workspace/BeerShiftWeb

$ rhc domain show -l yourOpenShiftUserName

You should see a giturl for the beershift application that we created in part 2 of this series that looks similar to:

Copy the git URL beginning with ssh:// and type the following command:

$ git remote add openshift ssh://239ce12ccf4953b9f3637fb4cdac5e@beershift-onpaas.rhcloud.com/~/git/...

Making sure to replace the git URL above with the one from your application.

Step 4: Add your brewerydb.com API key

In step 1, we signed up for a brewerydb.com API key. In this step we need to add that key to the config.php file in our project.

Now that you have your API key added, you need to need commit your code and push your backend services to OpenShift.

$ git commit -a -m ...Adding my API Key“

$ git push openshift

Verify that your application is up and running by pointing your browser to beershift-yourDomainName.rhcloud.com

Step 5: How did this all work, right out of the box?

You are probably wondering how the database is working without any configuration? If you take a look in the mongodb.php file under the config directory, you will notice that we are using environment variable to authenticate to the database. OpenShift sets these environment variables on your host machine so that you don't have to remember them. This makes deployments and application portability across machines very easy.

Step 6: Understand the code

I created a video that will walk you through how the code and configuration works. Instead of covering it here in this blog, I suggest that the reader watch the video.

In the next part of this blog series, I will go over the mobile application code in detail and then we will put all of the parts together to make an application that runs on iPhone and Android with a MongoDB backend hosted in the cloud.

Tagged with: MongoDB, Mongo, NoSQL, Polyglot persistence, 10gen

comments powered by Disqus