我一直在研究金字塔,我正在寻找一个中间件来服务我的 api。我已经使用 DispatchMiddleware 通过 werkzeug 完成了这项工作。
我怎样才能在金字塔内实现这一点
project
| __init__.py
| api
| | __init__.py
| web
| | __init__.py
项目/api/初始化.py
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('cornice')
api_includes(config)
config.scan() // scan only api_includes/routes
app = config.make_wsgi_app()
return app
项目/网络/初始化.py
def web_includes(config):
config.include('project.posts', route_prefix='/post')
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
web_includes(config)
config.add_route('home', '/')
config.scan() // only web_includes/routes
app = config.make_wsgi_app()
return app
项目/初始化.py
from pyramid.config import Configurator
from api import main as api_main
from web import main as web_main
from pyramid.wsgi import wsgiapp, wsgiapp2
def main(global_configs, **settings):
web = wsgiapp2(web_main(global_configs, **settings))
api = wsgiapp2(api_main(global_configs, **settings))
config = Configurator()
config.add_view(web, name='/')
config.add_view(api, name='api')
return config.make_wsgi_app()
当访问我的 api 时,它可以工作,但是除非我指定一个不是 '/' 或 '' 的名称,否则它们不会工作,例如:
config.add_view(web, name='foo')
去 /foo 或 /foo/post 工作