#!/usr/bin/env python import web, config, view from web import form from view import render from uristore import uristore from exists import head_url urls = ( '/', 'shrink', '/(\S+)', 'forward' ) def uricheck(uri): try: head_url(uri) return True except: import sys #print "Unexpected error:", sys.exc_info()[0], uri return False myform = form.Form( form.Textbox("uri", form.notnull, form.Validator('Must be a valid URI', lambda x:uricheck(x))) ) class forward: def GET(self, uri): a = uristore() to = a[uri] if to: web.redirect(to) else: return web.notfound() class shrink: def GET(self): i = web.input(uri="") form = myform() if not i.uri or not form.validates(): print render.base(render.index(form)) else: a = uristore() surink = a.store(i.uri) print render.base(render.input(i.uri, surink, form)) def runfcgi_apache(func): web.wsgi.runfcgi(func, None) if __name__ == "__main__": import os web.run(urls, globals(), *config.middleware)