diff --git a/docs/source/conf.py b/docs/source/conf.py index 2bc68a3517330cdfab2ca7a8cbcc0c9fb59b2bd6..8a6ca804f98730651c57120454e1b8622ad2381a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -7,6 +7,7 @@ from datetime import datetime import json import os import sys +import tempfile sys.path.insert( 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) @@ -53,7 +54,19 @@ html_theme = 'sphinx_rtd_theme' html_static_path = ['_static'] -api_schema = create_app().openapi() +# we need a minimal/parseable config file in order to +# start the server and dump the schema +with tempfile.NamedTemporaryFile(delete=False) as f: + + bogus_config = {'inventory': 'http://bogus'} + with open(f.name, 'w') as f: + json.dump(bogus_config, f) + f.flush() + os.environ['SETTINGS_FILENAME'] = f.name + + api_schema = create_app().openapi() + openapi_filename = os.path.join(os.path.dirname(__file__), "openapi.json") with open(openapi_filename, 'w') as f: json.dump(api_schema, f, indent=4) + diff --git a/mapping_provider/__init__.py b/mapping_provider/__init__.py index 8660a8800eb6e9442eaa353215589a46772c44fc..1000f2af4fa33a441d0a75abe1a8ccbc64525f64 100644 --- a/mapping_provider/__init__.py +++ b/mapping_provider/__init__.py @@ -4,16 +4,19 @@ Default entry point for the FastAPI application. from fastapi import FastAPI +from mapping_provider import config, environment from mapping_provider.api import common, map -from mapping_provider import config -from mapping_provider import environment + def create_app() -> FastAPI: """ Creates the FastAPI application instance, with routers attached. """ environment.setup_logging() - environment.setup_sentry(config.load().sentry) + + app_config = config.load() + if app_config.sentry: + environment.setup_sentry(app_config.sentry) app = FastAPI( title="Mapping provider", diff --git a/mapping_provider/config.py b/mapping_provider/config.py index c61b44aee8a46c5eaa2f913285fbafe7beb59158..cf67282db6fa7984c799f83a9dd85b9383838dad 100644 --- a/mapping_provider/config.py +++ b/mapping_provider/config.py @@ -1,6 +1,8 @@ import os + from pydantic import BaseModel, Field, HttpUrl + class SentryConfig(BaseModel): dsn: str environment: str @@ -16,44 +18,11 @@ class SentryConfig(BaseModel): # return v class Configuration(BaseModel): - sentry: SentryConfig + sentry: SentryConfig | None = None inventory: HttpUrl -# CONFIG_SCHEMA = { -# '$schema': 'https://json-schema.org/draft/2020-12/schema', - -# 'definitions': { -# 'sentry-info': { -# 'type': 'object', -# 'properties': { -# 'dsn': {'type': 'string'}, -# 'environment': {'type': 'string'}, -# 'level': { -# 'type': 'string', -# 'enum': ['debug', 'info', 'warning', 'error'], -# 'default': DEFAULT_SENTRY_LOG_LEVEL -# } -# }, -# 'required': ['dsn'], -# 'additionalProperties': False, -# }, -# }, - -# 'type': 'object', -# 'properties': { -# 'sentry': {'$ref': '#/definitions/sentry-info'}, -# 'inventory': { -# 'type': 'string', -# 'format': 'uri', -# } -# }, -# 'required': ['sentry', 'inventory'], -# 'additionalProperties': False -# } - - -def load(): +def load() -> Configuration: """ Loads, validates and returns configuration parameters from the file named in the environment variable 'SETTINGS_FILENAME'. diff --git a/mapping_provider/environment.py b/mapping_provider/environment.py index 527ab4b6215a3067c3ec67c88b7ef3cda4070af7..2319555abc09ab5ca98794c7684a3514d3b797b4 100644 --- a/mapping_provider/environment.py +++ b/mapping_provider/environment.py @@ -8,8 +8,8 @@ Environment setup """ import json -import logging.config import logging +import logging.config import os from typing import TYPE_CHECKING @@ -19,7 +19,6 @@ if TYPE_CHECKING: import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration - LOGGING_DEFAULT_CONFIG = { 'version': 1, 'disable_existing_loggers': False, @@ -55,7 +54,7 @@ LOGGING_DEFAULT_CONFIG = { } -def setup_logging(): +def setup_logging() -> None: """ Sets up logging using the configured filename. @@ -71,15 +70,19 @@ def setup_logging(): logging.config.dictConfig(logging_config) -def setup_sentry(sentry_config_params: 'SentryConfig'): +def setup_sentry(sentry_config_params: 'SentryConfig') -> None: """ Sets up the sentry instrumentation based on the Configuration.sentry params. """ match str(sentry_config_params.level): - case 'debug': level = logging.DEBUG - case 'info': level = logging.INFO - case 'warning': level = logging.WARNING - case 'error': level = logging.ERROR + case 'debug': + level = logging.DEBUG + case 'info': + level = logging.INFO + case 'warning': + level = logging.WARNING + case 'error': + level = logging.ERROR sentry_sdk.init( dsn=sentry_config_params.dsn,