2015-09-30 60 views
0

是否有一種(簡單)的方式來構建webapp2的url的方式,將允許像下面的結構?我看不到從父級傳遞PathPrefixRoute的方法,但類似PathPrefixRoute('(/v1)', [PathPrefixRoute('$1/app1',會很酷。python webapp2嵌套PathPrefixRoute

urls = [ 
    PathPrefixRoute('/v1', [ 
     PathPrefixRoute('/app1', [ 
      # /v1/app1/index 
      Route('/index', 'v1.app1.index.Main') 
     ]), 
     PathPrefixRoute('/app2', [ 
      # /v1/app2/index 
      Route('/index', 'v1.app2.index.Main') 
     ]) 
    ]), 
    PathPrefixRoute('/v2', [ 
     PathPrefixRoute('/app1', [ 
      # /v2/app1/index 
      Route('/index', 'v2.app1.index.Main') 
     ]) 
    ]) 
] 

application = webapp2.WSGIApplication(routes=urls) 

回答

0

我最終寫了我自己的自定義服務器關閉uwsgi。我使用的路由機制我已經粘貼在下面,但完整的應用程序是318行的善良,並且可以在基本的1核心服務器上處理100個RPS(這是我測試過的所有內容,但感覺它可以處理1000個RPS)。

隨時評論好,壞和醜陋的想法。

class Static: 
    def __init__(self, s): self._s = s 
    def __str__(self): return self._s 

SITES = { 
    '(127.0.0.1|localhost)': { 
     '/': 'default.index.Home' 
    }, 
    '(example.com)': { 
     '/': 'example.index.Home', 
     '/static': Static('example'), 
     '/console': { 
      '/': 'example.console.Home', 
      '/login': 'example.console.Login' 
     } 
    } 
} 

def get_location(self): 
    http_host = self.env.get('HTTP_HOST', '').split(':')[0] # host:port 
    if not http_host: raise HttpException(404, "Empty Site") 
    path_info = self.env.get('PATH_INFO') 
    if not path_info: raise HttpException(404, "Empty Path") 
    for site in SITES: 
     if re.match(site, http_host, re.I): 
      paths = SITES[site] 
      parts = path_info.split('/')[1:] # first is always blank 
      for part in parts: 
       p = paths.copy() 
       for path in p: 
        if re.match(path + '$', '/' + part, re.I): 
         if isinstance(p[path], basestring): 
          return p[path] 
         paths = p[path] 
         break 
        paths = None 
       if not paths: break 
      raise HttpException(404, "Unknown Path") 
    raise HttpException(404, "Unknown Site")