A small blogging platform in Google App Engine

If you have never made a web application it may seem daunting. There are hundreds of alternative technologies and frameworks out there. And web apps development is quite different from client applications, which is what most developers are used to.

Here is an example of a web application. Wikipedia!

Screenshot of Wikipedia

Most web applications share a few common elements:

  • A persistence layer, for authored content or user created content
  • A system to connect each user request with a part of the application
  • A method to render and display that content to users

Traditionally the persistence layer is a SQL database, the requests are directed for example to Java servlets in an app server like Tomcat, and there is a more or less refined templating engine, in which the content is added to create the whole page returned to the user.

Another alternative is Google App Engine. Google App Engine is a platform and a set of libraries to develop web applications based in Google's own infrastructure. It is available in Java and Python, but here I will concentrate on the Python version.

The persistence layer of Google App Engine is the Datastore, a highly parallel but simple to use storage solution. The Datastore doesn't support queries as complex SQL does, however it can scale up to a level which is beyond what a normal database can do. And it can do so in a way that is trivial for the developer.

Google App Engine uses yaml and the webapp framework to answer user queries. One can set a configuration file which assigns certain url paths (via a regular expression) to instances of webapp.RequestHandler.

handlers:
- url: /.*
  script: app.py

The instance can implement a get() method which generates the response returned to the user.

class App(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, World!')

Finally, in order to generate HTML, Google App Engine incorporates the templating engine from Django. A template is essentially a document with variables instead of content. When the application needs to answer a user request it can load the template and replace the variables with real content, for example information from the data storage.

This is basically the way that this blog is made. It is a very simple Google App Engine application. It uses the Datastore for the blog posts, which are retrieved when it receives a request for the /blog url path. Then it replaces the blog post content into the blog template and returns that content. Here is a sample of code. It is not the actual code, but it gives a complete example:

class Blog(webapp.RequestHandler):
  def get(self):
    # Retrieve the posts from the database.
    query = 'SELECT * FROM Post WHERE public = True ORDER BY date DESC '
         'LIMIT %d ' % NUM_POSTS_IN_MAIN_PAGE
    posts = db.GqlQuery(query).fetch(NUM_POSTS_IN_MAIN_PAGE)

    # Render them into html.
    if len(posts) > 0:
      template_values = {'posts': posts }
      template_path = os.path.join(os.path.dirname(__file__), 'templates/blog')
      content = template.render(template_path, template_values)
    else:
      content = ''

    # And return a full page with the blog content.
    s = pages.FullPageGenerator()
    self.response.out.write(s.generate(content))

To sum up, Google App Engine is a great option for developing web applications. Expecially if you will require it to scale seamlessly, or to integrate with Google services. Check out the Google App Engine tutorial.

Powered by Google App Engine

Written on December 27, 2009