web/code-middle.py

Sat Jun 20 20:44:03 2009 +0900

author
hattori@www.hexacosa.net
date
Sat Jun 20 20:44:03 2009 +0900
changeset 6
c6da061b21c4
permissions
-rwxr-xr-x

profiling for web application

hattori@5 1 import web
hattori@5 2 import fapws._evwsgi as evwsgi
hattori@5 3 from fapws import base
hattori@5 4
hattori@5 5
hattori@5 6 DBPATH = '/var/www/app/hello/hello.sqlite'
hattori@5 7 TEMPLATE_PATH = '/var/www/templates/'
hattori@5 8
hattori@5 9 urls = (
hattori@5 10 '/', 'index',
hattori@5 11 '/(.*)', 'hello',
hattori@5 12 )
hattori@5 13
hattori@5 14 render = web.template.render(TEMPLATE_PATH)
hattori@5 15 application = web.application(urls, globals()).wsgifunc()
hattori@5 16 db = web.database(dbn='sqlite', db=DBPATH)
hattori@5 17
hattori@5 18 class hello:
hattori@5 19 def GET(self, username):
hattori@5 20 users = db.select('hellouser', where="name = '%s'" % username)
hattori@5 21 isFound = False
hattori@5 22 for u in users:
hattori@5 23 if u.name == username:
hattori@5 24 isFound = True
hattori@5 25 if not isFound:
hattori@5 26 db.query("INSERT INTO hellouser (name) VALUES ('%s')" % username)
hattori@5 27 return render.hello(username, isFound)
hattori@5 28
hattori@5 29 class index:
hattori@5 30 def GET(self):
hattori@5 31 return "Hello web.py"
hattori@5 32
hattori@5 33 class HelloDebugMiddleware:
hattori@5 34 def __init__(self, application):
hattori@5 35 self.application = application
hattori@5 36 def __call__(self, env, start_response):
hattori@5 37 web.debug("Hello Debug!")
hattori@5 38 return self.application(env, start_response)
hattori@5 39
hattori@5 40 if __name__ == '__main__':
hattori@5 41 application = web.application(urls, globals()).wsgifunc()
hattori@5 42 application = HelloDebugMiddleware(application)
hattori@5 43 evwsgi.start("0.0.0.0", 8080)
hattori@5 44 evwsgi.set_base_module(base)
hattori@5 45 evwsgi.wsgi_cb(("", application))
hattori@5 46 evwsgi.run()
hattori@5 47

mercurial