2013-03-14 99 views
0

有没有人想出了一种在Google App Engine中自动路由的优雅方式?我已经结束了一个很长的路线名单,即基于处理程序和方法的Google-App-Engine网址

urls = routes.HandlerPrefixRoute(h+'index_handler.',[RedirectRoute(r'/',handler='IndexHandler')]),... 

我想它这么example.com/blog会自动路由到博客处理程序,并自动example.com/blog/method路线blog.method方法。

回答

0

我已经想出了一个非常基本的解决方案,用webapp2和app引擎进行路由。 我不打算在这里发布整个解决方案,但让我知道是否有人想看到它,我会在github上发布它并向您发送链接。

我基本上只使用操作系统通过文件来获得我的处理程序目录和循环我的文件:

for file in os.listdir(directory): 
    if file.endswith(".py") and file != '__init__.py': 

我用一个非常简单的命名约定(即FILE_NAME =文件名),所以基于文件处理程序目录,我可以为每个动态地创建路由。

我在也应该路由的方法上使用装饰器(即URL处理器/方法将转到处理器文件,将调用Handler类和Handler.Method)。所以我得到每个类的所有方法,如果类方法具有由装饰器创建的属性,请将其路由!

所以对于一个像这样的循环:

methods = inspect.getmembers(handlercls, predicate=inspect.ismethod) 
methods = [x[1] for x in methods if hasattr(x[1], 'route')] 

for method in methods: 
    # Set some kwargs that I can then pass to a route (i.e. handler path, method to call, etc...) 

就像我说的,可能为什么没有人回答了这一问题,整个解决方案已经很长了,所以如果有人想,我把它挂在GitHub上和也许人们可以添加/控制。以改善它。

相关问题