def list_all_brains() -> Dict[str, BrainConfiguration]: brains = {} for directory in listdir(service_settings().brains_directory): if path.isdir(path.join(service_settings().brains_directory, directory)) \ and BrainConfiguration.json_config_file_exists(directory): brains[directory] = BrainConfiguration.from_json_file( directory) return brains
def sanitize_configuration_filename(brainname: str, filename: str) -> str: brainpath = str(service_settings().brains_directory / brainname) fullpath = path.normpath(path.join(brainpath, filename)) if not fullpath.startswith(str(service_settings().brains_directory)): raise LearningHouseSecurityException( 'Configuration file name breaks configuration directory') return fullpath
def sensorsconfig(cls) -> Tuple[List[str], List[str]]: categoricals = [] numericals = [] filename = service_settings().brains_directory / 'sensors.json' with open(filename, 'r', encoding='utf-8') as json_file: sensors = json.load(json_file) categoricals = list( map( lambda x: x[0], filter(lambda x: x[1] == cls.CATEGORICAL_KEY, sensors.items()))) numericals = list( map( lambda x: x[0], filter(lambda x: x[1] == cls.NUMERICAL_KEY, sensors.items()))) categoricals.append('month_of_year') numericals.append('day_of_month') categoricals.append('day_of_week') numericals.append('hour_of_day') numericals.append('minute_of_hour') return categoricals, numericals
def get_application() -> FastAPI: settings = service_settings() initialize_logging(settings.logging_level) application = FastAPI(**settings.fastapi_kwargs) application.include_router(brain.router) application.include_router(configuration.router) application.include_router(docs.router) application.add_exception_handler( RequestValidationError, validation_error_handler) application.add_exception_handler( LearningHouseException, learninghouse_exception_handler) application.add_middleware( CORSMiddleware, allow_origins=['*'], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) application.mount( '/static', StaticFiles(directory=STATIC_DIRECTORY), name='static') return application
def run(): settings = service_settings() logger.info(f'Running {settings.title} {versions.service}') logger.info(versions.libraries_versions) logger.info(f'Running in {settings.environment} mode') logger.info(f'Listening on {settings.host}:{settings.port}') logger.info(f'Configuration directory {settings.brains_directory}') logger.info(f'URL to OpenAPI file {settings.openapi_url}') if settings.environment == 'production': if settings.debug: logger.warning( 'Debugging active. Recommendation: Do not use in production mode!' ) if settings.reload: logger.warning( 'Reloading active. Recommendation: Do not use in production mode!' ) if settings.documentation_url is not None: logger.info( f'See interactive documentation {settings.documentation_url}') uvicorn.run(app=APP_REFERENCE, log_config=None, **settings.uvicorn_kwargs)
from fastapi import APIRouter from fastapi.openapi.docs import (get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html) from starlette.responses import HTMLResponse from learninghouse.core.settings import service_settings settings = service_settings() router = APIRouter(include_in_schema=False) if settings.docs_url is not None: @router.get(settings.docs_url) async def custom_swagger_ui_html() -> HTMLResponse: return get_swagger_ui_html( openapi_url=settings.openapi_file, title=settings.title + " - Swagger UI", oauth2_redirect_url=settings.oauth2_redirect_url, swagger_js_url="/static/docs/swagger-ui-bundle.js", swagger_css_url="/static/docs/swagger-ui.css", swagger_favicon_url="/static/favicon.ico", ) if settings.oauth2_redirect_url: @router.get(settings.oauth2_redirect_url) async def swagger_ui_redirect() -> HTMLResponse: return get_swagger_ui_oauth2_redirect_html()