__init__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # This file is part of Radicale Server - Calendar 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. import pkg_resources
  27. from radicale import config, log
  28. from radicale.app import Application
  29. VERSION = pkg_resources.get_distribution("radicale").version
  30. _application = None
  31. _application_config_path = None
  32. _application_lock = threading.Lock()
  33. def _init_application(config_path, wsgi_errors):
  34. global _application, _application_config_path
  35. with _application_lock:
  36. if _application is not None:
  37. return
  38. log.setup()
  39. with log.register_stream(wsgi_errors):
  40. _application_config_path = config_path
  41. configuration = config.load(config.parse_compound_paths(
  42. config.DEFAULT_CONFIG_PATH,
  43. config_path))
  44. log.set_level(configuration.get("logging", "level"))
  45. # Log configuration after logger is configured
  46. configuration.log_config_sources()
  47. _application = Application(configuration)
  48. def application(environ, start_response):
  49. """Entry point for external WSGI servers."""
  50. config_path = environ.get("RADICALE_CONFIG",
  51. os.environ.get("RADICALE_CONFIG"))
  52. if _application is None:
  53. _init_application(config_path, environ["wsgi.errors"])
  54. if _application_config_path != config_path:
  55. raise ValueError("RADICALE_CONFIG must not change: %s != %s" %
  56. (repr(config_path), repr(_application_config_path)))
  57. return _application(environ, start_response)