gpt4 book ai didi

python - 多个子域,通用路由模式

转载 作者:行者123 更新时间:2023-11-28 19:23:24 25 4
gpt4 key购买 nike

所以我想要的是拥有一个具有以下内容的 Pyramid 应用程序:

  • 如果用户访问 control.blah.com/ 我希望他们获得页面 A
  • 如果用户访问 www.blah.com/ 我希望他们访问页面 B

我尝试过的:

# Pregen for "Control" Subdomain
def pregen(request, elements, kw):
kw['_app_url'] = 'http://control.blah.com'
return elements, kw

# Custom predicate for "Control" Subdomain
def req_sub(info, request):
return request.host.startswith('control')

def main(global_config, **settings):
"""
This function returns a Pyramid WSGI application.
"""

engine = engine_from_config(settings, 'sqlalchemy.')
Session.configure(bind=engine)

config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.add_jinja2_extension('jinja2.ext.do')
config.include('pyramid_tm')
config.add_static_view('static', 'static', cache_max_age=3600)

# Control Subdomain
config.add_route('control_index', '/', custom_predicates=(req_sub, ),
pregenerator=pregen)

# Main Subdomain
config.add_route('index', '/')

config.scan()

app = config.make_wsgi_app()

return app

现在,转到 control.blah.com/ 会导致 control_index View 被调用,但是当我转到 www.blah.com/ 我收到 404 Not Found。

同样,如果我将 config.add_route('index', '/') 移到子域行之前,则会遇到相反的问题。

是否有可能得到我想要的东西,或者路线是否需要有不同的模式(也就是。不能有 2 strip 有 / 模式的路线)

最佳答案

您对自定义谓词的使用看起来是正确的,但对于与相同模式冲突的路由,您可能是正确的。这是我会尝试的:

class DomainOverrideMiddleware(object):

def __init__(self, app):
self.application = app

def __call__(self, environ, start_response):
if "HTTP_HOST" in environ and "control.blah.com" in environ['HTTP_HOST']:
environ['PATH_INFO'] = "/control" + environ['PATH_INFO']
return self.application(environ, start_response)



def main(global_config, **settings):
"""
This function returns a Pyramid WSGI application.
"""

engine = engine_from_config(settings, 'sqlalchemy.')
Session.configure(bind=engine)

config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.add_jinja2_extension('jinja2.ext.do')
config.include('pyramid_tm')
config.add_static_view('static', 'static', cache_max_age=3600)

# Control Subdomain
#Change this subdomain to handle the modified path
config.add_route('control_index', '/control/', custom_predicates=(req_sub, ),
pregenerator=pregen)

# Main Subdomain
config.add_route('index', '/')

config.scan()

#Change this to call the override
app = DomainOverrideMiddleware(config.make_wsgi_app())

因此,这会在应用程序内部使用“/control”前缀修改路径以处理不同的路由(避免冲突),但到用户的路径应该保持不变。未经测试,但理论上可行。

关于python - 多个子域,通用路由模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19146359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com