Search code examples
google-app-engineweb-applicationspython-2.7webapp2

Please help me improve my routing


My routing works towards me but it's built from a long time and since the framework was updated to webapp2 so I mix routings and don't know which way is best. Could you help me improve which and how I should do my routing? Is it better to make the framework have the routing make a default value for the handler or set the routing directly above the handler instead?

app = webapp2.WSGIApplication([
    ('/', HomeHandler),
    ('/shop', FileUploadFormHandler),
    ('/mypage', MyPageHandler),
    webapp2.Route(r'/newpassword/', handler=NewPassword, name='newpassword'),
    webapp2.Route(r'/signup/', handler=Signup, name='signup'),
    webapp2.Route(r'/myorg/', handler=Myorg, name='signup'),
    webapp2.Route(r'/register/', handler=Register, name='register'),
  webapp2.Route(r'/education/objectives/index', handler=Objectives, name='objectives'),
    webapp2.Route(r'/newdistributor/', handler=NewDistributor, name='newdistributor'),
    webapp2.Route(r'/emaillogin/', handler=Emaillogin, name='emaillogin'),
   webapp2.Route(r'/paysonreceive/', handler=PaysonReceiveHandler, name='paysonreceivehandler'),
   webapp2.Route(r'/paysonhandler/', handler=PaysonHandler, name='paysonhandler'),
  webapp2.Route('/passwdreset/<token>', handler=PasswordReset, name='passwordreset'),
    ('/shop/articles', ShopHandler),
    ('/info', InfoHandler),
    ('/update', UpdateHandler),
    ('/support', SupportHandler),
    ('/events.*', EventHandler),
    ('/academy', AcademyHandler),
    ('/marketing.*', MarketingHandler),
    ('/dropbox', FileUploadFormHandler),
    ('/upload', FileUploadHandler),
    ('/user/register', UserRegisterHandler),
    ('/tracker', TrackerHandler),
    ('/generate_upload_url', GenerateUploadUrlHandler),
    ('/file/([0-9]+)', FileInfoHandler),
    ('/file/([0-9]+)/download', FileDownloadHandler),
    ('/file/([0-9]+)/success', AjaxSuccessHandler),
    ('/home', Home),
    ('/paypal', Home),
    ('/sell', Sell),
    ('/sell/(.*)/', Sell),
    ('/buy/(.*)/return/(.*)/(.*)/', BuyReturn),
    ('/buy/(.*)/cancel/(.*)/', BuyCancel),
    ('/buy/(.*)/', Buy),
    ('/image/(.*)/', PPImage),
    webapp2.Route(r'/login/', handler=NewLoginHandler, name='login'),
    webapp2.Route(r'/logout/', handler=NewLogoutHandler, name='logout'
                  ),
    webapp2.Route(r'/secure/', handler=SecureRequestHandler,
                  name='secure'),
    webapp2.Route(r'/create/', handler=CreateUserHandler,
                  name='create-user'),
    ], config=config)

So when adding a new URI I must add both to app.yaml, the routing and a handler clas and a template which is not ideal since those are too many places to add stuff just to get a response from the WSGI. So I'd like to rework to code to make it work with more default values for instance if I add a RequestHandler class then it should have a default routing since it already has a unique name. Can the framework do that?

Thanks for any answer or comment


Solution

  • I don't think create a default routing is a good idea, but you should be able to create it. For example:

    import pages1
    import pages2
    
    def route_generator(modules):
        urls = []
        for m in modules:
            for h in dir(m):
                handler = eval("%s.%s"%(m.__name__, h))
                if issubclass(handler, webapp.RequestHandler):                
                    urls.append(('/%s/'%(handler.__name__), handler))
    
        return urls
    
    # list all module contained your handler class.
    # and use a generator to generate the url mapping.
    app = webapp2.WSGIApplication(route_generator([pages1,pages2], config=config)