# Copyright CESSDA ERIC 2021
#
# Licensed under the EUPL, Version 1.2 (the "License"); you may not
# use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Entrypoint to start serving the OAI-PMH Repo Handler.
Handle command line arguments, application setup, discovery & load of plugins,
server startup and critical exception logging.
"""
import logging
from tornado.httputil import HTTPServerRequest
from py12flogging.log_formatter import (
set_ctx_populator,
setup_app_logging
)
from kuha_common import (
conf,
server
)
from kuha_oai_pmh_repo_handler import (
http_api,
controller
)
from kuha_oai_pmh_repo_handler.serve import load_metadataformats
from cdcagg_oai import metrics
_logger = logging.getLogger(__name__)
[docs]
def app_setup(settings, mdformats):
"""Setup and return Tornado web application
:param :obj:`argparse.Namespace` settings: Loaded settings
:param list mdformats: Loaded & configured metadataformats
:returns: Tornado web application instance
"""
ctrl = controller.from_settings(settings, mdformats)
app = http_api.get_app(settings.api_version, controller=ctrl, app_class=metrics.CDCAggWebApp)
# Dynamically resolve handler for oai requests
app.set_oai_route_handler_class(app.find_handler(
HTTPServerRequest('GET', f'/{settings.api_version}/oai')).handler_class)
app.add_handlers('.*', [('/metrics', metrics.CDCAggMetricsHandler)])
return app
[docs]
def main():
"""Starts the server.
Load metadataformats using entrypoint discovery group
`cdcagg.oai.metadataformats`. Call :func:`configure` to
define, load and setup configurations. Initiate controller
and start server.
"""
mdformats = load_metadataformats('cdcagg.oai.metadataformats')
settings = configure(mdformats)
if settings.print_configuration:
print('Print active configuration and exit\n')
conf.print_conf()
return
try:
app = app_setup(settings, mdformats)
except Exception:
_logger.exception('Exception in application setup')
raise
try:
server.serve(app, settings.port)
except KeyboardInterrupt:
_logger.warning('Shutdown by CTRL + C', exc_info=True)
except Exception:
_logger.exception('Unhandled exception in main()')
raise
finally:
_logger.info('Exiting')