Programming with Google App Engine

Google app engine uses Python. I am still learning Python at the same time programming with GAE! In this post I will be noting down steps to try my first program with Google App Engine.

Prerequisite

You must download the Google App Engine development kit located at at
http://code.google.com/appengine/downloads.html.
The SDK is available for Windows, Mac OS X, and Linux. It also has python 2.5

What is included in the kit?

The kit is pretty comprehensive and includes,

  • A web server application to simulate App engine environment
  • Local copy of Datastore
  • Local copy of Google accounts and ability to get URLs, send email from your computer using APIs

CLIs from SDK

Look for following 2 CLIs once you download the SDK,

  • dev_appserver.py The development web server
  • appcfg.py Used to upload your app to App Engine

The app I am going to create is known “Hello World” but I promise (to myself) that I will create better app next time to try out some advanced APIs. (may be app for Bulb and Tube ;)

So here it goes,

Create hello.py and put following code,

print ‘Content-Type: text/plain’ print ”
print ‘Hello World!’
Next, you need to have a configuration file called app.yaml.

Create a file in the directory called app.yaml and write it to read as follows:

application: san_hello_world version: 1.0 runtime: python api_version: 1 handlers: – url: /.* script: hello.py

Because the handler script and configuration file are mapping every URL to the handler, the application is done. Trust me! That’s it.

Now you can test the app with the web server included with the App Engine SDK.

Start the web server with the following command:

google_appengine/dev_appserver.py hello/ (where hello is a folder having above 2 files)

Go to browser and run http:/localhost:8080

You can continue to modify files and web server gets notification and updates what is displayed in the browser (ofcouse once your refresh the browser)

Once you develop and test your app, its time to register it with Google. After all main
intention of this app is to run on cloud and not local on my computer. :)

If this is first time for you, you need to authenticate yourself by providing cell phone number where Google can text you auth code.

The next step is to register the application ID for your application using,

https://appengine.google.com/start/createapp

Once the registration is completed, you access the application by going to

http://application-id.appspot.com.

You are almost done!

To upload your finished application to Google App Engine, run the following command:

appcfg.py update hello

Enter your Google user name and password at the prompts. Now you can see your application on App Engine and all you need to do is open up a web browser and enter

http://application-id.appspot.com.

Note: You can create 10 applications per google account

Love to hear your comments. I will explore some more APIs from GAE and also going to try my hands on Amazon Web Services.

Here is something important about Google App Engine. When you start learning new technology, it is good idea to go through its architecture and understand how it works.

Google App Engine

Zest Infotech - Architecture of Google App Engine

App engine is meant for applications that reacts quickly to requests. It is expected to respond within hundreds of milliseconds to respond to a web request. A request can be as simple as get chunk of data from data store or contact a remote server. Let’s walk through inside of Google App engine and see what exactly happens when an request comes,

  • As soon as you/user interacts with an app hosted on Google app engine, the request is sent from browser to app engine. The first stop is “front end”. It is a load balancer for distributing incoming requests efficiently across multiple nodes. It has mapping so figures out for which app the request is and then consults corresponding configuration file.
  • The configuration tells front end how to treat a request based on URL. (This is similar or close to what we call as DOCROOT)
  • If request does not belong to any entry in configuration 404 is returned
  • If it matches to a static file, control is transferred to staic file server. This server is dedicated for serving static files. It is optimized for fast delivery
  • If it matches a pattern mapped to one of the request handlers, the frontend sends request to app server. The server invokes the app by calling the request handler that corresponds with the URL path of the request, according to the app configurations
  • Finally It waits for the response

You can configure front end optionally to authenticate using Google account

Every app in Google app engine runs in a capsule called as “run time environment”. It is basically abstraction above the actual nodes. (Operating system and hardware).
This run time environment is also called as “sandbox”. Sandbox allows fenced environment to every app which can be executed as if it is exclusive access to underneath hardware and resources. Due to this sandbox, apps developed on google app engine do not have access to file system or network ports of hardware it is running.

Something every app developer should remmeber all the time! :)

so what an app can do if it can not access file system or network ports? There are some services provided by app engine to perform various tasks. For example to fetch URLs, app can make http requests to remote machines. This facility is provided by service which is used by Google’s great apps like gmail, picassa etc.

There is no streaming implemented so request handler prepares the response then returns it and then terminates. And then app engine sends that response to client. Once all data is sent, request terminates.

The controlling authority to control the front ends, app servers and static file server is called as “app master”. One of the task of app master is for deploying new versions of application software and configuration.