Getting Started with Google App Engine

Getting Started with Google App Engine

A jsonengine instance is best hosted on Google App Engine ( http://appengine.google.com/), which deserves a short introduction. Google App Engine is Google’s cloud-computing technology. It virtualizes web applications across multiple servers and data centers. In simple terms, that means that your application is not running in one single physical place (like a data center in Silicon Valley), but is replicated all around the world using Google’s impressive data center coverage.

Google App Engine is a Platform-as-a-Service (PaaS) solution, and differs from the other cloud-computing services (such as Amazon Web Services). The main difference is that Google App Engine provides a complete set of services and tools based on conventions rather than configuration, which allows applications to be deployed very quickly. Another advantage of Google App Engine is its pricing model—it’s free up to a certain level of used resources.

NOTE: PaaS is like any other type of hosting service, except that, instead of providing you with a blank, “wildcard” server, it offers a predefined set of services and hardware solutions. It encapsulates a lot of complexity and configuration tweaks for you. The catch is that you’ll have less freedom, and you’ll have to follow the solution provider’s guidelines.

So, before you can use jsonengine, you have to set up Google App Engine. The first thing you have to do is download the Google App Engine framework, so you can test and deploy your application locally before putting it into the cloud. Google App Engine proposes two different kinds of framework:

A Python-based SDK

A Java-based SDK As jsonengine is based on Java, we are going to use the Java-based version. Don’t

worry if you don’t have any knowledge about Java—it won’t be necessary. We simply need to deploy jsonengine, so you won’t have to do anything to configure or implement it.

Let’s start by installing Google App Engine. First, download the binaries, which you can find here:

http://code.google.com/appengine/downloads.html Then follow the installation instructions here:

http://code.google.com/appengine/docs/java/gettingstarted/installing.html An additional useful guide for installing the App Engine SDK can be found on the

jsonengine site: http://code.google.com/p/jsonengine/wiki/HowToInstall

CHAPTER 5: Synchronizing with the Cloud

Deploying jsonengine Locally

Deploying a jsonengine instance is very easy: just download the provided WAR archive and deploy it using the following Google App Engine command:

dev_appserver location/to/you/war/folder This will start the Google App Engine server locally, open your favorite browser, and go

to http://localhost:8080/samples/bbs.html. You should now be able to see the jsonengine’s sample application.

That’s all! Now that you have an up-and-running, ultra-scalable online storage solution, we can go back to our to-do application to start implementing the synchronization process.

Since jsonengine is a web application, not just a storage solution, it can serve web content, making it ideal for the mobile web application we’re building. All the code we’ve implemented (HTML, JavaScript, and CSS) can be hosted inside jsonengine’s instance.

Take a look at the jsonengine’s distribution. It has a war folder containing a classic web application structure (CSS, JS, etc.). If you have all your resources inside the right folders, with a single command from the App Engine SDK, you can actually deploy your application—which is pretty neat. When you deploy your application, the war folder is packaged in a WAR archive. (A WAR archive is simply the enterprise version of a JAR (Java archive format); it functions just like a ZIP file.)

Let’s perform a quick test to ensure that jsonengine does indeed serve web assets in addition to JSON data. Take the sample code from Chapter 4 and paste it in your war folder. This should result in the following folder structure:

JSONEngine— |- war—

|- css— |- proui.css |- js |- jquery-1.4.2.min.js |- jquery.validate.js |- json2.js |- prowebapps.js

Download from Wow! eBook <www.wowebook.com>

|- snippets— |- 04—

|- todolist.html |- todolist.css |- todolist.js

Browse to http://localhost:8080/snippets/04/todolist.html, and you will see your application running.

Since we’re building a mobile web application, you should also try it on the Android emulator. As discussed in Chapter 1, the Android emulator is unable to communicate with a webserver on your machine via the localhost address. As such, we will need to instruct the AppEngine development server to bind to other network addresses on our machine (by default, the Google App Engine local server binds to the localhost address).

CHAPTER 5: Synchronizing with the Cloud

Fortunately, though, the dev_appserver command comes with an option parameter to make the server listen on all the IPs (0.0.0.0) of your local machine.

Stop your server and restart it by adding the following parameter –a 0.0.0.0 right after your command and before the path to your war folder. Once you have done this, you will

be able to access the application from the emulator or mobile device by using the IP of your local machine.

NOTE: If you have Mongoose running in the background, the AppEngine development server will not be able to bind to the alternative address. In this case, simply close down the Mongoose server and then start the AppEngine dev server again.

At the end of this chapter, you’ll see how to deploy the application on the cloud; it’s not very complicated and it can be done in one single command. The deployed online application will have the exact same behavior as on your local server—there is nothing to configure and it’s definitively a huge time-saver.