对google app engine的一瞄|A glance at google app engine


homepage:
http://code.google.com/appengine/

In general, google app engine is a free web application hoster. with following special feature:

  • PROS:
    1. free, not only a python supported virtual server, but also a build-in object database (called GQL) and a cvs system
    2. users can use google account to sign them onto your site
    3. a simple and well designed framework to start with (see the architecture section)
  • CONS:
    1. there are limitations (no directly file access, no socket)
    2. you source is open to google :D
    3. support Python only currently
    4. 有些域名.appspot.com被GFW了,我代表我个人从意念上FUCK这个愚蠢的中华人民共和国政府
  • Architecture:
  • google app engine provides a very simple MVC model for developers to build their app upon.

    • Controller: app.yaml file and webapp.WSGIApplication

    there is a handlers section in the app.yaml where you can define handlers to match & process request, like .htaccess file in apache:

    handlers:
    
    - url: /images
    
      static_dir: images
    
    - url: /user/.*
    
      script: handler.py
    
      login: required
    
    - url: /.*
    
      script: handler.py
    
    - url: /admin/(.*)
    
       script: admin/1.py
    
       login: admin

    and in the handler.py, you can bind certain url to a python class:

    application = webapp.WSGIApplication(
    
                                         [('/', MainPage),
    
                                          ('/guestbook', Guestbook)],
    
                                         debug=True)
    
    def main():
    
      run_wsgi_app(application)
    
    if __name__ == "__main__":
    
      main()
    • Model: Python class(inherit from webapp.RequestHandler)

    In the above example, when we access /guestbook, a Guestbook class instance will be created and its ‘get’ function will be called, you can override that function to process the page logic and prepare the data you want to show to user.

    • View: page template(there is a default build-in template engine Django in google app engine)

    Still about the above example, in the get function of Guestbook class, after data is prepared,

    template_values = {
    
        'messages': message_array,
    
        'title': url
    
    }
    
    path = os.path.join(os.path.dirname(__file__), 'guestbook.html')
    
    self.response.out.write(template.render(path, template_values))

    –these lines of code will call the template engine to render the page.

    in the guestbook.html, format the data and output html:

    <head><title>{{title}}</title></head>
    <body>
    {% for msg in messages %}
      {% if msg.author %}
        <b>{{ msg.author.nickname|upper|escape }}</b> wrote: 
      {% else %}
        An anonymous person wrote:
      {% endif %}
      <blockquote>{{ msg.content|escape }}</blockquote>
    {% endfor %}
    </body>