基于处理程序和方法的Google-App-Engine网址

时间:2013-03-14 18:02:53

标签: google-app-engine routing url-routing

有没有人想出在Google App Engine中进行自动路由的优雅方式?我结束了很长的路线清单,即

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

我喜欢它,所以example.com/blog会自动路由到博客处理程序,example.com/blog/method会自动路由到blog.method方法。

1 个答案:

答案 0 :(得分:0)

我已经找到了使用webapp2和app引擎进行路由的非常基本的解决方案。 我不打算在这里发布整个解决方案,但如果有人想看到它,请告诉我,我会将它发布在github上并发送给你一个链接。

我基本上只是使用os将我的文件放在我的处理程序目录中并循环遍历文件:

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

我使用一个非常简单的命名约定(即file_name = FileName),因此根据处理程序目录中的文件,我可以为每个文件动态创建路径。

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

这样的for循环:

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上,也许人们可以添加/控制。为了改善它。