Move wsgi app class to api directory
This commit moves the wsgi application class from cmd/api.py to api/app.py. This enables admins to deploy blazar-api via WSGI. It is good to place the class outside of the cmd directory. Partially Implements: blueprint deploy-api-in-wsgi Change-Id: I4305ead55995ca04aeae34c4c489e4865391f8a1
This commit is contained in:
parent
2e026f8b3e
commit
774b519342
|
@ -0,0 +1,59 @@
|
||||||
|
# Copyright (c) 2017 NTT Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from blazar.api.v1 import app as v1_app
|
||||||
|
from blazar.api.v2 import app as v2_app
|
||||||
|
|
||||||
|
|
||||||
|
class VersionSelectorApplication(object):
|
||||||
|
"""Maps WSGI versioned apps and defines default WSGI app."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._status = ''
|
||||||
|
self._response_headers = []
|
||||||
|
self.v1 = v1_app.make_app()
|
||||||
|
self.v2 = v2_app.make_app()
|
||||||
|
|
||||||
|
def _append_versions_from_app(self, versions, app, environ):
|
||||||
|
tmp_versions = app(environ, self.internal_start_response)
|
||||||
|
if self._status.startswith("300"):
|
||||||
|
tmp_versions = json.loads("".join(tmp_versions))
|
||||||
|
versions['versions'].extend(tmp_versions['versions'])
|
||||||
|
|
||||||
|
def internal_start_response(self, status, response_headers, exc_info=None):
|
||||||
|
self._status = status
|
||||||
|
self._response_headers = response_headers
|
||||||
|
|
||||||
|
def __call__(self, environ, start_response):
|
||||||
|
self._status = ''
|
||||||
|
self._response_headers = []
|
||||||
|
|
||||||
|
if environ['PATH_INFO'] == '/' or environ['PATH_INFO'] == '/versions':
|
||||||
|
versions = {'versions': []}
|
||||||
|
self._append_versions_from_app(versions, self.v1, environ)
|
||||||
|
self._append_versions_from_app(versions, self.v2, environ)
|
||||||
|
if len(versions['versions']):
|
||||||
|
start_response("300 Multiple Choices",
|
||||||
|
[("Content-Type", "application/json")])
|
||||||
|
return [json.dumps(versions)]
|
||||||
|
else:
|
||||||
|
start_response("204 No Content", [])
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
if environ['PATH_INFO'].startswith('/v1'):
|
||||||
|
return self.v1(environ, start_response)
|
||||||
|
return self.v2(environ, start_response)
|
|
@ -18,7 +18,6 @@ eventlet.monkey_patch(
|
||||||
os=True, select=True, socket=True, thread=True, time=True)
|
os=True, select=True, socket=True, thread=True, time=True)
|
||||||
|
|
||||||
import gettext
|
import gettext
|
||||||
import json
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from eventlet import wsgi
|
from eventlet import wsgi
|
||||||
|
@ -27,7 +26,7 @@ from oslo_log import log as logging
|
||||||
|
|
||||||
gettext.install('blazar')
|
gettext.install('blazar')
|
||||||
|
|
||||||
from blazar.api.v1 import app as v1_app
|
from blazar.api import app as wsgi_app
|
||||||
from blazar.api.v2 import app as v2_app
|
from blazar.api.v2 import app as v2_app
|
||||||
from blazar.utils import service as service_utils
|
from blazar.utils import service as service_utils
|
||||||
|
|
||||||
|
@ -52,46 +51,6 @@ CONF.register_opts(api_opts)
|
||||||
CONF.import_opt('host', 'blazar.config')
|
CONF.import_opt('host', 'blazar.config')
|
||||||
|
|
||||||
|
|
||||||
class VersionSelectorApplication(object):
|
|
||||||
"""Maps WSGI versioned apps and defines default WSGI app."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self._status = ''
|
|
||||||
self._response_headers = []
|
|
||||||
self.v1 = v1_app.make_app()
|
|
||||||
self.v2 = v2_app.make_app()
|
|
||||||
|
|
||||||
def _append_versions_from_app(self, versions, app, environ):
|
|
||||||
tmp_versions = app(environ, self.internal_start_response)
|
|
||||||
if self._status.startswith("300"):
|
|
||||||
tmp_versions = json.loads("".join(tmp_versions))
|
|
||||||
versions['versions'].extend(tmp_versions['versions'])
|
|
||||||
|
|
||||||
def internal_start_response(self, status, response_headers, exc_info=None):
|
|
||||||
self._status = status
|
|
||||||
self._response_headers = response_headers
|
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
|
||||||
self._status = ''
|
|
||||||
self._response_headers = []
|
|
||||||
|
|
||||||
if environ['PATH_INFO'] == '/' or environ['PATH_INFO'] == '/versions':
|
|
||||||
versions = {'versions': []}
|
|
||||||
self._append_versions_from_app(versions, self.v1, environ)
|
|
||||||
self._append_versions_from_app(versions, self.v2, environ)
|
|
||||||
if len(versions['versions']):
|
|
||||||
start_response("300 Multiple Choices",
|
|
||||||
[("Content-Type", "application/json")])
|
|
||||||
return [json.dumps(versions)]
|
|
||||||
else:
|
|
||||||
start_response("204 No Content", [])
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
if environ['PATH_INFO'].startswith('/v1'):
|
|
||||||
return self.v1(environ, start_response)
|
|
||||||
return self.v2(environ, start_response)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Entry point to start Blazar API wsgi server."""
|
"""Entry point to start Blazar API wsgi server."""
|
||||||
cfg.CONF(sys.argv[1:], project='blazar', prog='blazar-api')
|
cfg.CONF(sys.argv[1:], project='blazar', prog='blazar-api')
|
||||||
|
@ -99,7 +58,7 @@ def main():
|
||||||
if not CONF.enable_v1_api:
|
if not CONF.enable_v1_api:
|
||||||
app = v2_app.make_app()
|
app = v2_app.make_app()
|
||||||
else:
|
else:
|
||||||
app = VersionSelectorApplication()
|
app = wsgi_app.VersionSelectorApplication()
|
||||||
|
|
||||||
wsgi.server(eventlet.listen((CONF.host, CONF.port), backlog=500), app)
|
wsgi.server(eventlet.listen((CONF.host, CONF.port), backlog=500), app)
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,9 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from blazar.api import app as api
|
||||||
from blazar.api.v1 import app as v1_app
|
from blazar.api.v1 import app as v1_app
|
||||||
from blazar.api.v2 import app as v2_app
|
from blazar.api.v2 import app as v2_app
|
||||||
from blazar.cmd import api
|
|
||||||
from blazar import tests
|
from blazar import tests
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue