app.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging
  2. import os
  3. from importlib import import_module
  4. from logging.config import dictConfig
  5. from flask import Flask
  6. from lakesuperior.endpoints.ldp import ldp
  7. from lakesuperior.endpoints.main import main
  8. from lakesuperior.endpoints.query import query
  9. from lakesuperior.messaging.messenger import Messenger
  10. from lakesuperior.toolbox import Toolbox
  11. # App factory.
  12. def create_app(app_conf, logging_conf):
  13. '''
  14. App factory.
  15. Create a Flask app with a given configuration and initialize persistent
  16. connections.
  17. @param app_conf (dict) Configuration parsed from `application.yml` file.
  18. @param logging_conf (dict) Logging configuration from `logging.yml` file.
  19. '''
  20. app = Flask(__name__)
  21. app.config.update(app_conf)
  22. dictConfig(logging_conf)
  23. logger = logging.getLogger(__name__)
  24. logger.info('Starting LAKEsuperior HTTP server.')
  25. ## Configure endpoint blueprints here. ##
  26. app.register_blueprint(main)
  27. app.register_blueprint(ldp, url_prefix='/ldp', url_defaults={
  28. 'url_prefix': 'ldp'
  29. })
  30. # Legacy endpoint. @TODO Deprecate.
  31. app.register_blueprint(ldp, url_prefix='/rest', url_defaults={
  32. 'url_prefix': 'rest'
  33. })
  34. app.register_blueprint(query, url_prefix='/query')
  35. # Initialize RDF store connector.
  36. conn_mod_name = app_conf['store']['ldp_rs']['connector']['module']
  37. conn_mod = import_module('lakesuperior.store_layouts.ldp_rs.{}'.format(
  38. conn_mod_name))
  39. conn_cls = getattr(conn_mod, camelcase(conn_mod_name))
  40. rdf_store_conn = conn_cls(
  41. **app_conf['store']['ldp_rs']['connector']['options'])
  42. logger.info('RDF store: {}'.format(conn_mod_name))
  43. # Initialize RDF layout.
  44. rdfly_mod_name = app_conf['store']['ldp_rs']['layout']
  45. rdfly_mod = import_module('lakesuperior.store_layouts.ldp_rs.{}'.format(
  46. rdfly_mod_name))
  47. rdfly_cls = getattr(rdfly_mod, camelcase(rdfly_mod_name))
  48. app.rdfly = rdfly_cls(rdf_store_conn, app_conf['store']['ldp_rs'])
  49. logger.info('RDF layout: {}'.format(rdfly_mod_name))
  50. # Initialize file layout.
  51. nonrdfly_mod_name = app_conf['store']['ldp_nr']['layout']
  52. nonrdfly_mod = import_module('lakesuperior.store_layouts.ldp_nr.{}'.format(
  53. nonrdfly_mod_name))
  54. nonrdfly_cls = getattr(nonrdfly_mod, camelcase(nonrdfly_mod_name))
  55. app.nonrdfly = nonrdfly_cls(app_conf['store']['ldp_nr'])
  56. logger.info('Non-RDF layout: {}'.format(nonrdfly_mod_name))
  57. # Set up messaging.
  58. app.messenger = Messenger(app_conf['messaging'])
  59. return app
  60. def camelcase(word):
  61. '''
  62. Convert a string with underscores with a camel-cased one.
  63. Ripped from https://stackoverflow.com/a/6425628
  64. '''
  65. return ''.join(x.capitalize() or '_' for x in word.split('_'))