Plug our Flask app into Nailgun's web.py
This commit is contained in:
parent
4f40d2e6ba
commit
33c7c4b768
|
@ -12,11 +12,66 @@
|
|||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import itertools
|
||||
import os
|
||||
import threading
|
||||
|
||||
from nailgun import db as nailgun_db
|
||||
from nailgun import extensions
|
||||
import web
|
||||
|
||||
import tuning_box
|
||||
from tuning_box import app as tb_app
|
||||
from tuning_box import db as tb_db
|
||||
|
||||
|
||||
class App2WebPy(web.application):
|
||||
def __init__(self):
|
||||
web.application.__init__(self)
|
||||
self.__name__ = self
|
||||
self.app = None
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def create_app(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_app(self):
|
||||
with self.lock:
|
||||
if not self.app:
|
||||
self.app = self.create_app()
|
||||
return self.app
|
||||
|
||||
def handle(self):
|
||||
written_data = []
|
||||
|
||||
def write(data):
|
||||
assert start_response.called
|
||||
written_data.append(data)
|
||||
|
||||
def start_response(status, headers, exc_info=None):
|
||||
assert not start_response.called
|
||||
assert not exc_info
|
||||
start_response.called = True
|
||||
web.ctx.status = status
|
||||
web.ctx.headers.extend(headers)
|
||||
return write
|
||||
|
||||
start_response.called = False
|
||||
|
||||
app = self.get_app()
|
||||
environ = dict(web.ctx.environ)
|
||||
environ["PATH_INFO"] = environ["REQUEST_URI"] = web.ctx.path
|
||||
result = app(environ, start_response)
|
||||
return itertools.chain(written_data, result)
|
||||
|
||||
|
||||
class TB2WebPy(App2WebPy):
|
||||
def create_app(self):
|
||||
app = tb_app.build_app()
|
||||
tb_db.prefix_tables(Extension.table_prefix())
|
||||
tb_db.db.session = nailgun_db.db
|
||||
app.config["PROPAGATE_EXCEPTIONS"] = True
|
||||
return app
|
||||
|
||||
|
||||
class Extension(extensions.BaseExtension):
|
||||
|
@ -24,6 +79,8 @@ class Extension(extensions.BaseExtension):
|
|||
version = tuning_box.__version__
|
||||
description = 'Plug tuning_box endpoints into Nailgun itself'
|
||||
|
||||
urls = [{'uri': '/config', 'handler': TB2WebPy()}]
|
||||
|
||||
@classmethod
|
||||
def alembic_migrations_path(cls):
|
||||
return os.path.join(os.path.dirname(__file__), 'migrations')
|
||||
|
|
Loading…
Reference in New Issue