BANGALORE, INDIA: Cloud computing has become the talk of the town. Developers had been using Platform-as-a-Service or on-demand platforms to build applications that would run on the Cloud. This means, you can develop your applications and then host them on somebody else's infrastructure.
Google also has ventured into the cloud computing arena with their App Engine, which was first released as a Beta version in April 2008. Google App Engine not only leverages their massive infrastructure to the developers but also help them in reducing the time to write the code and deploying it on a Web server. Now, developers just have to build their apps and simply host them on App Engine. They do not have to bother about the complex setting up of the servers and their maintenance.
Google App Engine is still in its preview release, and costs nothing to get started. With a free account, one can get up to 500MB of persistent storage and enough CPU and bandwidth for about 5 million page views a month. The App Engine's virtualised computing environment caters to all the components of a modern web application: dynamic runtime, persistent storage, static file serving, emailing, monitoring and log analysis. Like any other cloud infrastructure, App Engine also provides scalability of the applications depending upon the traffic and data storage requirements.
App Engine SDK
The SDK provided by the Google to develop applications for the App Engine includes a Web server application that simulates the App Engine environment. Presently, App Engine supports only python programming language, but Google has said that later other language support will also be offered. In this article, we will see how to use App Engine SDK to build Web application and then host them online on Google's App Engine.
The App Engine SDK boasts of a strong API framework, which included Python scripting runtime, Google user accounts and mail service APIs, and datastore APIs that provides access to powerful persistence engine with querying and transaction support. The SDK also provides a server that emulates the Google App Engine platform. This helps developers to develop applications locally and check how it will perform on the Google's cloud upon deployment. Once you have created the application you can deploy it on Google's server using the command line.
Getting started
With this month's PCQ Professional DVD, we have distributed the Google App Engine version 1.1.5 and also the setup of Python 2.5.2 that is needed to run the App Engine platform. For this article, we will be using Windows as the operating platform, and so the installers provided in DVD are for Windows OS. The installers for Linux and Mac can be downloaded from the App Engine site. Since python is the pre-requisite for App Engine SDK, we need to install python version 2.5 first. Then you can install the App Engine SDK. Now you are ready to develop applications that will run on Google's cloud infrastructure.
| The command dev_appserver.py starts the App Engine web server and listens for requests on port 8080. |
To test that your setup is running; go to the command line and run the dev_appserver.py command. If an error message comes of invalid arguments, the App Engine server has been setup correctly.
Demo application
The web application that we will be making for this demo will be a simple guestbook sort of application whereby any visitor can leave a comment on the webpage. In the application, we will have just two requests: one to render the page and other to record what the visitor comments on the page. To start creating the application, first create a directory ?guestbook? and all the files for the application will reside in this directory. The App Engine application requires a configuration file called app.yaml. This yaml file describes which URLs will be used by which handler scripts. The following code snippet shows the yaml file for our application. The application identifier ?guestbook? is the name of the application and we specify all URLs to be handled by the main.py script.
application: pcqdemoapp
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
script: main.py
To upload the web application online on App Server appcfg.py command is used. And when prompted for email and password, pass your email id. |
Webapp and datastore
Since the application has to render web pages we have to use webapp framework which will handle the requests for such tasks. Therefor we will have to import WSGI handlers to handle the requests and the same will also be used to call Django template. While the datastore API lets you store persistent data and then retrieve it throughout your application. The code of the application looks like following and name the code file as ?main.py?.
#!/usr/bin/env python
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class GuestBook(db.Model):
message = db.StringProperty(required=True)
time = db.DateTimeProperty(auto_now_add=True)
sender = db.StringProperty()
class MyHandler(webapp.RequestHandler):
def get(self):
guestbooks = db.GqlQuery('SELECT * FROM GuestBook ORDER BY time DESC') values = {
'guestbooks': guestbooks
}
self.response.out.write(template.render('main.html',values))
def post(self):
guestbook = GuestBook(
message=self.request.get('message'),
sender=self.request.get('sender'))
guestbook.put()
self.redirect('/')
def main():
app = webapp.WSGIApplication(<(r'.*', MyHandler)>, debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
The class GuestBook defines the three attributes for the database structure. And using GqlQuery on database, we can retrieve the contents of the database. The class MyHandler inherits from webapp.RequestHandler and therefore in the GET method we will render the page request. With response.out we will pass the values retrieved from the datastore but also a template designed that will be used to display the output in a specified format, in our case it will be ?main.html? file. Similarly, for the POST method we will capture the content from the user's request, i.e. from the textboxes named message and sender, and pass the value to variable which is of type GuestBook.
In the above code snippet, we have to redirect the user to same page, so we will use self.redirect('/') in the POST method, which will return the request onto the same page webpage after rendering the result from the GET method also.
The demo application can be accessed at http://pcqdemoapp.appspot.com. All applications for App Engine are hosted on appspot.com |
The following code snippet shows the main.html file which will be used as template for our web application.
PCQ Demo Guest Book App
{% for guestbook in guestbooks %}
from
{% ifequal guestbook.sender "" %}
Anonymous
{% else %}
{{guestbook.sender}}
{% endifequal %}
{% endfor %}
The main.html template will use Django template engine to render the Web page. Using the script we first loop through the guestbooks list that we have passed along as a parameter in the GET method when we called the template.
Deploying on App Engine
When we are done with the writing the application, we can test the Web app on the web server provided with the SDK. To start the web server use the dev_ appserver. py command by giving it the path to the guestbook directory. The command will be like this:
dev_appserver.py c:/guestbook
The web server would be up, and ready to listen requests on port 8080. To test the webapp, type the URL http://localhost:8080 in your browser. Even if you do changes in the web application code, you will not have to restart the Web server, the changes will reflect as soon as you refresh the browser page. Since, we are using a Google's datastore to save the comments on the guestbook. So each time when you use the guestbook application, the comments from the previous session won't be lost. But if we want to clear the comment history from the server we have to pass the option when we start the server as follows:
dev_appserver.py ?clear_datastore c:/guestbook
Now you can upload and deploy this application on Google App Engine. For that first we need to register at appengine.google.com website. Sign in into App Engine using your Google account or create a new one. Now through the App Engine Administration Console, click the ?Create an Application? button and register a unique application name for your Webapp. This application ID will become the URL for your webapp if you elect to host your application on appspot.com. For our guestbook application we registered the application with ID ?pcqdemoapp?. Therefore the URL for that is:
pcqdemoapp.appspot.com
You can access the demo app at this address. Now to upload the guestbook application, first edit the app.yaml file. Change the value of the application: setting from guestbook to your registered application ID. Through the command line, run the following command:
appcfg.py update c:/guestbook
Enter your Google ID and password when prompted and when the update has been made, you can access your application by prefixing your application ID before appspot.com and enter that as your URL.
Thus, we can create apps to be hosted on the Google cloud using App Engine SDK.
/ciol/media/agency_attachments/c0E28gS06GM3VmrXNw5G.png)
Follow Us