From aab3d6236533282517d9fc58a844b738c35a904d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 14 Oct 2022 07:04:56 +0200 Subject: [PATCH] Added api settings #70 --- src/bot/config/feature-flags.json | 3 +- src/bot_api/api.py | 7 ++- .../config/apisettings.edrafts-lapi.json | 18 ++++++- src/bot_api/configuration/api_settings.py | 14 +++++- .../configuration/authentication_settings.py | 48 +++++++++++++++++++ .../configuration/frontend_settings.py | 23 +++++++++ 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 src/bot_api/configuration/authentication_settings.py create mode 100644 src/bot_api/configuration/frontend_settings.py diff --git a/src/bot/config/feature-flags.json b/src/bot/config/feature-flags.json index 68bf6e002c..2d214f7f3e 100644 --- a/src/bot/config/feature-flags.json +++ b/src/bot/config/feature-flags.json @@ -10,6 +10,7 @@ "DatabaseModule": true, "ModeratorModule": true, "PermissionModule": true, - "PresenceModule": true + "PresenceModule": true, + "ApiOnly": true } } diff --git a/src/bot_api/api.py b/src/bot_api/api.py index 7a75e2a497..c404350521 100644 --- a/src/bot_api/api.py +++ b/src/bot_api/api.py @@ -4,6 +4,7 @@ from functools import partial from cpl_core.dependency_injection import ServiceProviderABC from flask import Flask, request +from bot_api.configuration.api_settings import ApiSettings from bot_api.logging.api_logger import ApiLogger from bot_api.route.route import Route @@ -14,6 +15,7 @@ class Api(Flask): self, logger: ApiLogger, services: ServiceProviderABC, + api_settings: ApiSettings, *args, **kwargs ): if not args: @@ -23,6 +25,7 @@ class Api(Flask): self._logger = logger self._services = services + self._apt_settings = api_settings # register before request self.before_request_funcs.setdefault(None, []).append(self.before_request) @@ -46,7 +49,7 @@ class Api(Flask): self._logger.trace(__name__, f'Body: {request.get_json(force=True, silent=True)}') def start(self): - self._logger.info(__name__, f'Starting API') + self._logger.info(__name__, f'Starting API {self._apt_settings.host}:{self._apt_settings.port}') self._register_routes() from waitress import serve - serve(self, host="0.0.0.0", port=5000) + serve(self, host=self._apt_settings.host, port=self._apt_settings.port) diff --git a/src/bot_api/config/apisettings.edrafts-lapi.json b/src/bot_api/config/apisettings.edrafts-lapi.json index 9e26dfeeb6..fb7c257c3b 100644 --- a/src/bot_api/config/apisettings.edrafts-lapi.json +++ b/src/bot_api/config/apisettings.edrafts-lapi.json @@ -1 +1,17 @@ -{} \ No newline at end of file +{ + "Api": { + "Port": 5000, + "Host": "0.0.0.0", + "RedirectToHTTPS": false + }, + "Authentication": { + "SecretKey": "F3b5LDz+#Jvzg=W!@gsa%xsF", + "Issuer": "http://localhost:5000", + "Audience": "http://localhost:5000", + "TokenExpireTime": 1, + "RefreshTokenExpireTime": 7 + }, + "Frontend": { + "URL": "http://localhost:4200/" + } +} \ No newline at end of file diff --git a/src/bot_api/configuration/api_settings.py b/src/bot_api/configuration/api_settings.py index e50f25fa95..237a272e19 100644 --- a/src/bot_api/configuration/api_settings.py +++ b/src/bot_api/configuration/api_settings.py @@ -4,19 +4,31 @@ from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC from cpl_core.console import Console -class APISettings(ConfigurationModelABC): +class ApiSettings(ConfigurationModelABC): def __init__(self): ConfigurationModelABC.__init__(self) + self._port = 80 + self._host = '' self._redirect_to_https = False + @property + def port(self) -> int: + return self._port + + @property + def host(self) -> str: + return self._host + @property def redirect_to_https(self) -> bool: return self._redirect_to_https def from_dict(self, settings: dict): try: + self._port = int(settings['Port']) + self._host = settings['Host'] self._redirect_to_https = bool(settings['RedirectToHTTPS']) except Exception as e: Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') diff --git a/src/bot_api/configuration/authentication_settings.py b/src/bot_api/configuration/authentication_settings.py new file mode 100644 index 0000000000..9a573a8b79 --- /dev/null +++ b/src/bot_api/configuration/authentication_settings.py @@ -0,0 +1,48 @@ +import traceback +from datetime import datetime + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class AuthenticationSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._secret_key = '' + self._issuer = '' + self._audience = '' + self._token_expire_time = 0 + self._refresh_token_expire_time = 0 + + @property + def secret_key(self) -> str: + return self._secret_key + + @property + def issuer(self) -> str: + return self._issuer + + @property + def audience(self) -> str: + return self._audience + + @property + def token_expire_time(self) -> int: + return self._token_expire_time + + @property + def refresh_token_expire_time(self) -> int: + return self._refresh_token_expire_time + + def from_dict(self, settings: dict): + try: + self._secret_key = settings['SecretKey'] + self._issuer = settings['Issuer'] + self._audience = settings['Audience'] + self._token_expire_time = int(settings['TokenExpireTime']) + self._refresh_token_expire_time = int(settings['RefreshTokenExpireTime']) + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/bot_api/configuration/frontend_settings.py b/src/bot_api/configuration/frontend_settings.py new file mode 100644 index 0000000000..2090f5a237 --- /dev/null +++ b/src/bot_api/configuration/frontend_settings.py @@ -0,0 +1,23 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class FrontendSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._url = '' + + @property + def url(self) -> str: + return self._url + + def from_dict(self, settings: dict): + try: + self._url = settings['URL'] + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')