radicale/radicale/__init__.py

75 lines
2.9 KiB
Python
Raw Normal View History

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