Google App Engine / Python — First Steps

So I’ve been playing with Google App Engine for a while, and though I’d track my steps on my most recent project. Everything I’m doing here mirrors the tutorials they provide, so there’s really no new information.

The first step is to get the application running. I set up eclipse with PyDev and the Google Plug-in for Eclipse, and set up my project like so. All of this will probably change.

lg
\- languagegenerator
  \- models
  \- views
      app.py
      main.py
\- static
    index.html
    favico.ico

Then I created an app.yaml in the lg (top) folder:

application: languagegenerator
version: 1
runtime: python
api_version: 1
threadsafe: true

handlers:
- url: /favicon.ico
  static_files: static/favicon.ico
  upload: static/favicon.ico

- url: /static
  static_dir: static

- url: /.*
  script: languagegenerator/views/main.py

This captures requests for favio, static files, and dynamic content, in that order.

main.py is sort of the router for the app. It takes the incoming URL and decides what to run next.

from languagegenerator.views.app import MainPage, Version

import webapp2
import wsgiref.handlers


if __name__ == '__main__':

    application = webapp2.WSGIApplication([
        ('/', MainPage),
        ('/version', Version), ],
        debug=True)
    wsgiref.handlers.CGIHandler().run(application)

And app.py is where I’m putting my application logic. Right now I have main and app under “views”, but I may change that as things evolve. It might be better to rename “views” to “representations” so I can be a cool REST guy.

from google.appengine.api import users
from datetime import datetime

import webapp2


class Version(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('ver=1.0 now=%s' % datetime.isoformat(datetime.now()))


class MainPage(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if not user:
            self.redirect(users.create_login_url(self.request.uri))

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('OK %s' % datetime.isoformat(datetime.now()))

apps.py captures a few things:

  1. a “version” URL I can use as a “hello world” link, or as a service heartbeat
  2. usage of the Google Users API, forcing a login before they can go
  3. a main page, which doesn’t really do anything and will probably go away when I’m done.

For my hello world project prior to this, setting up the run configuration in Eclipse was trivial. But this time around, I really had to fight for it. I’m not sure what I did differently.

The run configuration has these settings:


Main/Main Module: ${GOOGLE_APP_ENGINE}/dev_appserver.py
Arguments/Program Arguments: "${workspace_loc:lg}"
Arguments/Working directory: ${workspace_loc:}

This time around, I had to manually configure the GOOGLE_APP_ENGINE value (!?), but I’ve never had to that before. Not sure what I did wrong.

And that’s it. It runs — it’s all up from there!

Advertisements

Leave a comment

Filed under cloud computing, google app engine, python, utility

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s