Skip to content
Snippets Groups Projects
Commit 68ff9548 authored by Erik Reid's avatar Erik Reid
Browse files

ruffed and typing

parent 6699c096
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......@@ -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",
......
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'.
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment