__init__.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Entry point for external WSGI servers (like uWSGI or Gunicorn).
  21. Configuration files can be specified in the environment variable
  22. ``RADICALE_CONFIG``.
  23. """
  24. import os
  25. import threading
  26. from typing import Iterable, Optional, cast
  27. import pkg_resources
  28. from radicale import config, log, types
  29. from radicale.app import Application
  30. from radicale.log import logger
  31. VERSION: str = pkg_resources.get_distribution("radicale").version
  32. _application_instance: Optional[Application] = None
  33. _application_config_path: Optional[str] = None
  34. _application_lock = threading.Lock()
  35. def _get_application_instance(config_path: str, wsgi_errors: types.ErrorStream
  36. ) -> Application:
  37. global _application_instance, _application_config_path
  38. with _application_lock:
  39. if _application_instance is None:
  40. log.setup()
  41. with log.register_stream(wsgi_errors):
  42. _application_config_path = config_path
  43. configuration = config.load(config.parse_compound_paths(
  44. config.DEFAULT_CONFIG_PATH,
  45. config_path))
  46. log.set_level(cast(str, configuration.get("logging", "level")))
  47. # Log configuration after logger is configured
  48. for source, miss in configuration.sources():
  49. logger.info("%s %s", "Skipped missing" if miss
  50. else "Loaded", source)
  51. _application_instance = Application(configuration)
  52. if _application_config_path != config_path:
  53. raise ValueError("RADICALE_CONFIG must not change: %r != %r" %
  54. (config_path, _application_config_path))
  55. return _application_instance
  56. def application(environ: types.WSGIEnviron,
  57. start_response: types.WSGIStartResponse) -> Iterable[bytes]:
  58. """Entry point for external WSGI servers."""
  59. config_path = environ.get("RADICALE_CONFIG",
  60. os.environ.get("RADICALE_CONFIG"))
  61. app = _get_application_instance(config_path, environ["wsgi.errors"])
  62. return app(environ, start_response)