forked from sh-edraft.de/sh_discord_bot
		
	Added api settings #70
This commit is contained in:
		| @@ -10,6 +10,7 @@ | ||||
|     "DatabaseModule": true, | ||||
|     "ModeratorModule": true, | ||||
|     "PermissionModule": true, | ||||
|     "PresenceModule": true | ||||
|     "PresenceModule": true, | ||||
|     "ApiOnly": true | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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/" | ||||
|   } | ||||
| } | ||||
| @@ -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') | ||||
|   | ||||
							
								
								
									
										48
									
								
								src/bot_api/configuration/authentication_settings.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/bot_api/configuration/authentication_settings.py
									
									
									
									
									
										Normal 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()}') | ||||
							
								
								
									
										23
									
								
								src/bot_api/configuration/frontend_settings.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/bot_api/configuration/frontend_settings.py
									
									
									
									
									
										Normal 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()}') | ||||
		Reference in New Issue
	
	Block a user