#!/usr/bin/env python3
# Author(s): Toni Sissala
# Copyright 2020 Finnish Social Science Data Archive FSD / University of Tampere
# Licensed under the EUPL. See LICENSE.txt for full license.
"""Main entry point for starting OSMH Repo Handler
"""
import sys
import logging
import traceback
import kuha_common.server
from kuha_osmh_repo_handler.configure import configure
from kuha_osmh_repo_handler.handlers import (
SupportedVersionsHandler,
ListSupportedRecordTypesHandler,
ListRecordHeadersHandler,
GetRecordHandler
)
[docs]def get_app(api_version, app_settings=None):
"""Setup routes and return initialized Tornado web application.
:param api_version: HTTP Api version gets prepended to routes.
:type api_version: str
:param app_settings: Settings to store to application.
:type app_settings: dict or None.
:returns: Tornado web application.
:rtype: :obj:`tornado.web.Application`
"""
handlers = [
("/SupportedVersions", SupportedVersionsHandler),
(kuha_common.server.str_api_endpoint(
api_version, r"ListRecordHeaders"
), ListRecordHeadersHandler),
(kuha_common.server.str_api_endpoint(
api_version, r"ListSupportedRecordTypes"
), ListSupportedRecordTypesHandler),
(kuha_common.server.str_api_endpoint(
api_version,
r"ListRecordHeaders/(?P<record_type>Question|Study|StudyGroup|Variable)"
), ListRecordHeadersHandler),
(kuha_common.server.str_api_endpoint(
api_version,
r"GetRecord/(?P<record_type>Question|Study|StudyGroup|Variable)/(?P<identifier>.*)"),
GetRecordHandler)
]
web_app = kuha_common.server.WebApplication(handlers)
if app_settings:
web_app.settings = app_settings
return web_app
[docs]def main():
"""Application main function.
Parse commandline for settings. Pass settings to :func:`kuha_osmh_repo_handler.server.main`.
Exit on exceptions propagated at this level.
:returns: exit code, 1 on error, 0 on success.
:rtype: int
"""
rval = 0
settings = configure()
if settings.print_configuration:
print("Print active configuration and exit\n")
for key, value in settings._get_kwargs():
print("{} ... {}".format(key, value))
return rval
app_settings = {'kuha_settings': settings}
web_app = get_app(settings.osmh_repo_handler_api_version, app_settings)
try:
kuha_common.server.serve(web_app, settings.port)
except KeyboardInterrupt:
rval = 1
logging.warning("Shutdown by CTRL + C")
except Exception as exc:
rval = 1
logging.error(str(exc))
traceback.print_exc()
finally:
# Cleanup
logging.info("Shutdown")
return rval
if __name__ == '__main__':
sys.exit(main())