Building a skeleton web application

To get underway, we need a shell of a web application where we can start filling in the details. We previously used CherryPy (http://cherrypy.org) to build a simple web app, so let's use that to kick things off.

  1. First of all, let's code a main application that will bootstrap CherryPy as well as our application context.
import cherrypy
import os
import ctx
from springpython.context import ApplicationContext
if __name__ == '__main__':
cherrypy.config.update({'server.socket_port': 8009})
applicationContext = ApplicationContext(ctx.SpringBankAppContext())
cherrypy.tree.mount(
applicationContext.get_object("view"),
'/',
config=None)
cherrypy.engine.start()
cherrypy.engine.block()

You may notice that, it is looking inside package ctx for SpringBankAppContext to find the view object.

  1. Let's write a simple, pure Python IoC container that will create the CherryPy view object we need.
from springpython.config import PythonConfig, Object
from app import *
class SpringBankAppContext(PythonConfig):
def __init__(self):
PythonConfig.__init__(self)
@Object
def view(self):
return SpringBankView()

This is the simplest container possible. We basically have one object, view, which returns an instance of SpringBankView. Later on, if we need other injected objects or features, we can easily update this confi guration.

  1. Now let's write a simple view layer with a Hello, world style opening page.
import cherrypy
class SpringBankView(object):
@cherrypy.expose
def index(self, args=None):
return """
Welcome to SpringBank!
"""
  1. Let's start our application and see what it looks like.
Building a skeleton web application
  1. Now let's visit the advertised location of http://127.0.0.1:8009.
Building a skeleton web application

This is our starting position of a web application. From here, we will spend the rest of the chapter tackling the features we just described.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.32.67