app.py 2.8 KB

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