You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import base64
  13. import copy
  14. import io
  15. import socket
  16. from pkg_resources import resource_filename
  17. import flask
  18. from flask_restless import APIManager
  19. from flask_sqlalchemy import SQLAlchemy
  20. from flask_bootstrap import Bootstrap
  21. from kombu import Connection
  22. from kombu.pools import producers
  23. from oslo_config import cfg
  24. from oslo_log import log
  25. from PIL import Image
  26. from sqlalchemy.dialects import mysql
  27. from faafo import queues
  28. from faafo import version
  29. LOG = log.getLogger('faafo.api')
  30. CONF = cfg.CONF
  31. api_opts = [
  32. cfg.StrOpt('listen-address',
  33. default='0.0.0.0',
  34. help='Listen address.'),
  35. cfg.IntOpt('bind-port',
  36. default='80',
  37. help='Bind port.'),
  38. cfg.StrOpt('database-url',
  39. default='sqlite:////tmp/sqlite.db',
  40. help='Database connection URL.')
  41. ]
  42. CONF.register_opts(api_opts)
  43. log.register_options(CONF)
  44. log.set_defaults()
  45. CONF(project='api', prog='faafo-api',
  46. default_config_files=['/etc/faafo/faafo.conf'],
  47. version=version.version_info.version_string())
  48. log.setup(CONF, 'api',
  49. version=version.version_info.version_string())
  50. template_path = resource_filename(__name__, "templates")
  51. app = flask.Flask('faafo.api', template_folder=template_path)
  52. app.config['DEBUG'] = CONF.debug
  53. app.config['SQLALCHEMY_DATABASE_URI'] = CONF.database_url
  54. with app.app_context():
  55. db = SQLAlchemy(app)
  56. Bootstrap(app)
  57. def list_opts():
  58. """Entry point for oslo-config-generator."""
  59. return [(None, copy.deepcopy(api_opts))]
  60. class Fractal(db.Model):
  61. uuid = db.Column(db.String(36), primary_key=True)
  62. checksum = db.Column(db.String(256), unique=True)
  63. url = db.Column(db.String(256), nullable=True)
  64. duration = db.Column(db.Float)
  65. size = db.Column(db.Integer, nullable=True)
  66. width = db.Column(db.Integer, nullable=False)
  67. height = db.Column(db.Integer, nullable=False)
  68. iterations = db.Column(db.Integer, nullable=False)
  69. xa = db.Column(db.Float, nullable=False)
  70. xb = db.Column(db.Float, nullable=False)
  71. ya = db.Column(db.Float, nullable=False)
  72. yb = db.Column(db.Float, nullable=False)
  73. if CONF.database_url.startswith('mysql'):
  74. LOG.debug('Using MySQL database backend')
  75. image = db.Column(mysql.MEDIUMBLOB, nullable=True)
  76. else:
  77. image = db.Column(db.LargeBinary, nullable=True)
  78. generated_by = db.Column(db.String(256), nullable=True)
  79. def __repr__(self):
  80. return '<Fractal %s>' % self.uuid
  81. with app.app_context():
  82. db.create_all()
  83. manager = APIManager(app=app, session=db.session)
  84. connection = Connection(CONF.transport_url)
  85. @app.route('/', methods=['GET'])
  86. @app.route('/index', methods=['GET'])
  87. @app.route('/index/<int:page>', methods=['GET'])
  88. def index(page=1):
  89. hostname = socket.gethostname()
  90. fractals = Fractal.query.filter(
  91. (Fractal.checksum is not None) & (Fractal.size is not None)).paginate(
  92. page=page, per_page=5)
  93. return flask.render_template('index.html', fractals=fractals, hostname=hostname)
  94. @app.route('/fractal/<string:fractalid>', methods=['GET'])
  95. def get_fractal(fractalid):
  96. fractal = Fractal.query.filter_by(uuid=fractalid).first()
  97. if not fractal:
  98. response = flask.jsonify({'code': 404,
  99. 'message': 'Fracal not found'})
  100. response.status_code = 404
  101. else:
  102. image_data = base64.b64decode(fractal.image)
  103. image = Image.open(io.BytesIO(image_data))
  104. output = io.BytesIO()
  105. image.save(output, "PNG")
  106. image.seek(0)
  107. response = flask.make_response(output.getvalue())
  108. response.content_type = "image/png"
  109. return response
  110. def generate_fractal(**kwargs):
  111. LOG.debug("Postprocessor called!" + str(kwargs))
  112. with producers[connection].acquire(block=True) as producer:
  113. producer.publish(kwargs['result'],
  114. serializer='json',
  115. exchange=queues.task_exchange,
  116. declare=[queues.task_exchange],
  117. routing_key='normal')
  118. def convert_image_to_binary(**kwargs):
  119. LOG.debug("Preprocessor call: " + str(kwargs))
  120. if 'image' in kwargs['data']['data']['attributes']:
  121. LOG.debug("Converting image to binary...")
  122. kwargs['data']['data']['attributes']['image'] = \
  123. str(kwargs['data']['data']['attributes']['image']).encode("ascii")
  124. def main():
  125. print("Starting API server - new...")
  126. with app.app_context():
  127. manager.create_api(Fractal, methods=['GET', 'POST', 'DELETE', 'PATCH'],
  128. postprocessors={'POST_RESOURCE': [generate_fractal]},
  129. preprocessors={'PATCH_RESOURCE': [convert_image_to_binary]},
  130. exclude=['image'],
  131. url_prefix='/v1',
  132. allow_client_generated_ids=True)
  133. app.run(host=CONF.listen_address, port=CONF.bind_port, debug=True)