1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
14 from six.moves import filter
16 from flasgger import Swagger
17 from flask import Flask
18 from flask_restful import Api
20 from api.database import Base
21 from api.database import db_session
22 from api.database import engine
23 from api.database.v1 import models
24 from api.urls import urlpatterns
25 from api import ApiResource
26 from yardstick import _init_logging
27 from yardstick.common import utils
28 from yardstick.common import constants as consts
31 from urlparse import urljoin
33 from urllib.parse import urljoin
35 LOG = logging.getLogger(__name__)
38 app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 * 1024
46 def shutdown_session(exception=None):
50 def get_resource(resource_name):
51 name = ''.join(resource_name.split('_'))
52 return next((r for r in utils.itersubclasses(ApiResource)
53 if r.__name__.lower() == name))
59 if issubclass(a[1], Base):
65 subclses = filter(func, inspect.getmembers(models, inspect.isclass))
66 LOG.debug('Import models: %s', [a[1] for a in subclses])
67 Base.metadata.create_all(bind=engine)
70 def app_wrapper(*args, **kwargs):
72 return app(*args, **kwargs)
75 def get_endpoint(url):
76 ip = socket.gethostbyname(socket.gethostname())
77 return urljoin('http://{}:{}'.format(ip, consts.API_PORT), url)
82 api.add_resource(get_resource(u.target), u.url, endpoint=get_endpoint(u.url))
84 LOG.error('url resource not found: %s', u.url)
87 if __name__ == '__main__':
89 LOG.setLevel(logging.DEBUG)
90 LOG.info('Starting server')
92 app.run(host='0.0.0.0')