summaryrefslogtreecommitdiffstats
path: root/api/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'api/server.py')
-rw-r--r--api/server.py23
1 files changed, 20 insertions, 3 deletions
diff --git a/api/server.py b/api/server.py
index fac821b00..8cce4de87 100644
--- a/api/server.py
+++ b/api/server.py
@@ -7,13 +7,17 @@
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import logging
+from itertools import ifilter
+import inspect
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 engine
from api.database import db_session
+from api.database import models
from api.urls import urlpatterns
from yardstick import _init_logging
@@ -21,8 +25,6 @@ logger = logging.getLogger(__name__)
app = Flask(__name__)
-init_db()
-
Swagger(app)
api = Api(app)
@@ -33,6 +35,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 = ifilter(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)