Added api settings #70

This commit is contained in:
Sven Heidemann 2022-10-14 07:04:56 +02:00
parent 0927288bb5
commit aab3d62365
6 changed files with 108 additions and 5 deletions

View File

@ -10,6 +10,7 @@
"DatabaseModule": true, "DatabaseModule": true,
"ModeratorModule": true, "ModeratorModule": true,
"PermissionModule": true, "PermissionModule": true,
"PresenceModule": true "PresenceModule": true,
"ApiOnly": true
} }
} }

View File

@ -4,6 +4,7 @@ from functools import partial
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from flask import Flask, request from flask import Flask, request
from bot_api.configuration.api_settings import ApiSettings
from bot_api.logging.api_logger import ApiLogger from bot_api.logging.api_logger import ApiLogger
from bot_api.route.route import Route from bot_api.route.route import Route
@ -14,6 +15,7 @@ class Api(Flask):
self, self,
logger: ApiLogger, logger: ApiLogger,
services: ServiceProviderABC, services: ServiceProviderABC,
api_settings: ApiSettings,
*args, **kwargs *args, **kwargs
): ):
if not args: if not args:
@ -23,6 +25,7 @@ class Api(Flask):
self._logger = logger self._logger = logger
self._services = services self._services = services
self._apt_settings = api_settings
# register before request # register before request
self.before_request_funcs.setdefault(None, []).append(self.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)}') self._logger.trace(__name__, f'Body: {request.get_json(force=True, silent=True)}')
def start(self): 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() self._register_routes()
from waitress import serve 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)

View File

@ -1 +1,17 @@
{} {
"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/"
}
}

View File

@ -4,19 +4,31 @@ from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.console import Console from cpl_core.console import Console
class APISettings(ConfigurationModelABC): class ApiSettings(ConfigurationModelABC):
def __init__(self): def __init__(self):
ConfigurationModelABC.__init__(self) ConfigurationModelABC.__init__(self)
self._port = 80
self._host = ''
self._redirect_to_https = False self._redirect_to_https = False
@property
def port(self) -> int:
return self._port
@property
def host(self) -> str:
return self._host
@property @property
def redirect_to_https(self) -> bool: def redirect_to_https(self) -> bool:
return self._redirect_to_https return self._redirect_to_https
def from_dict(self, settings: dict): def from_dict(self, settings: dict):
try: try:
self._port = int(settings['Port'])
self._host = settings['Host']
self._redirect_to_https = bool(settings['RedirectToHTTPS']) self._redirect_to_https = bool(settings['RedirectToHTTPS'])
except Exception as e: except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')

View File

@ -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()}')

View File

@ -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()}')