diff options
Diffstat (limited to 'api/server.py')
-rw-r--r-- | api/server.py | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/api/server.py b/api/server.py index fac821b00..5bac1ba47 100644 --- a/api/server.py +++ b/api/server.py @@ -6,14 +6,21 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from __future__ import absolute_import + +import inspect import logging +from functools import reduce +from six.moves import filter +from flasgger import Swagger from flask import Flask from flask_restful import Api -from flasgger import Swagger -from api.database import init_db +from api.database import Base from api.database import db_session +from api.database import engine +from api.database import models from api.urls import urlpatterns from yardstick import _init_logging @@ -21,8 +28,6 @@ logger = logging.getLogger(__name__) app = Flask(__name__) -init_db() - Swagger(app) api = Api(app) @@ -33,6 +38,21 @@ def shutdown_session(exception=None): db_session.remove() +def init_db(): + def func(a): + try: + if issubclass(a[1], Base): + return True + except TypeError: + pass + return False + + subclses = filter(func, inspect.getmembers(models, inspect.isclass)) + logger.debug('Import models: %s', [a[1] for a in subclses]) + Base.metadata.create_all(bind=engine) + + +init_db() reduce(lambda a, b: a.add_resource(b.resource, b.url, endpoint=b.endpoint) or a, urlpatterns, api) |