Mobilize Your MongoDB - Developing iPhone and Android Apps in the Cloud - Part 3

During Part 2 of the developing iPhone and Android apps in the cloud 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 MongoDB database.

During Part 2 of the developing iPhone and Android apps in the cloud 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 MongoDB 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/beershift.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.

not being a git expert I received the following error when doing a push to the remote repository:

! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'ssh://df07bb82bceb4c4e995db14f54e57e9b@beershift-MYDOMAINHERE.rhcloud.com/~/git/beershift.git/' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.

After trying a few things I was able to push by using " git push -all --force openshift"

Just curious to know what was the cause of the original error.

I got the same error and just did a -f on the git push. I assume that is OK, I just saw this after. I do have the openshif displaying a welcome message at my URL. I am just walking through this after the sesion Grand did the other day for with 10gen.

Thanks for posting @bruce!

I created a new thread here to discuss the issue. Let us know if you run into any other issues.

Thanks;
~Nam

See new forum thread:
https://www.openshift.com/forums/openshift/git-push-error-non-fast-forward-error-failed-to-push-some-refs-to

I'm not sure if it's my git, OS X, or github, but I get a fatal remote error 'Repository not found' when I use

git://github.com/beershift/BeerShiftWeb.git

instead of the camel-cased beershift:

git://github.com/BeerShift/BeerShiftWeb.git

in step 2.

Yes, you are correct. The url for the git repository is case sensitive. I have updated the post above with the correct URL.

git://github.com/BeerShift/BeerShiftWeb.git

Greate pieces. Keep writing such kind of info on your site. I am really impressed by your site. Hey there, You have performed an incredible job.

Android Apps

You are guys are really doing well for solving our problems of apps. Keep the good work guide.

Regards,

The smart phones like android, iphones etc are used mostly in some important fields of life now a days. The importance of this media is just because of developed apps for androids which are facilitating well to their users. To get more, You just need to click here.

All,

I am trying to follow this blog series to create the BeerShift mobile app. After I make the change to the API key in my config.php file and try to access the application via my browser (Safari), I receive the following error:

An Error Was Encountered The Host must be set to connect to MongoDB

I had to "force" the git push to openshift to have the backend source code properly uploaded. Does anyone have any suggestions?

Thanks in advance. --craig