def build_app(loop):
"""Define the aiohttp web application framework and setup the routes to be used in the API"""
global current_action
app = web.Application(loop=loop)
current_action = ''
# Disable all caching for everything, disable once the Web UI gets cache
# breaking urls for it's assets (still need to not cache the REST responses, index.html though)
# TODO(cmaloney): Python 3.6 switch this to `async def` per:
# http://aiohttp.readthedocs.io/en/stable/web.html#signals
def no_caching(request, response):
response.headers['Cache-Control'] = 'no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
app.on_response_prepare.append(no_caching)
app.router.add_route('GET', '/', root)
app.router.add_route('GET', '/api/v{}'.format(VERSION), redirect_to_root)
app.router.add_route('GET', '/api/v{}/version'.format(VERSION), get_version)
app.router.add_route('GET', '/api/v{}/configure'.format(VERSION), configure)
app.router.add_route('POST', '/api/v{}/configure'.format(VERSION), configure)
app.router.add_route('GET', '/api/v{}/configure/status'.format(VERSION), configure_status)
app.router.add_route('GET', '/api/v{}/configure/type'.format(VERSION), configure_type)
app.router.add_route('GET', '/api/v{}/success'.format(VERSION), success)
# TODO(malnick) The regex handling in the variable routes blows up if we insert another variable to be
# filled in by .format. Had to hardcode the VERSION into the URL for now. Fix suggestions please!
app.router.add_route('GET', '/api/v1/action/{action_name:preflight|postflight|deploy}', action_action_name)
app.router.add_route('POST', '/api/v1/action/{action_name:preflight|postflight|deploy}', action_action_name)
app.router.add_route('GET', '/api/v{}/action/current'.format(VERSION), action_current)
app.router.add_route('GET', '/api/v{}/logs'.format(VERSION), logs_handler)
# TODO(cmaloney): These should probably actually hard fail.
try:
# Passing an absolute path because we don't trust add_static() to resolve relative paths for us.
app.router.add_static('/assets', os.path.abspath(assets_path))
app.router.add_static('/download/log', os.path.abspath(STATE_DIR))
except ValueError as err:
log.warning(err)
# Allow overriding calculators with a `gen_extra/async_server.py` if it exists
if os.path.exists('gen_extra/async_server.py'):
mod = importlib.machinery.SourceFileLoader('gen_extra.async_server', 'gen_extra/async_server.py').load_module()
mod.extend_app(app)
return app
评论列表
文章目录