【问题标题】:Google-App-Engine urls based on handlers and methods基于处理程序和方法的 Google-App-Engine 网址
【发布时间】:2013-03-14 18:02:53
【问题描述】:

有没有人想出一种在 Google App Engine 中进行自动路由的优雅方法?我最终得到了很长的路线列表,即

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

我希望 example.com/blog 会自动路由到 blog 处理程序,example.com/blog/method 会自动路由到 blog.method 方法。

【问题讨论】:

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


    【解决方案1】:

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

    我基本上只是使用 os 来获取我的处理程序目录中的文件并遍历文件:

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

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

    我也在应该路由的方法上使用装饰器(即 url 处理程序/方法将转到处理程序文件,处理程序类和 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 上,也许人们可以添加/控制。对它进行改进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2011-05-24
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      相关资源
      最近更新 更多