-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Integrated config parser to read .ini
removed unused settings and config_manager modified config_service to be the class that handles configuration modified all instances of config_manager to use config_service
- Loading branch information
Showing
34 changed files
with
202 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[MAILER] | ||
DEFAULT_EMAIL = | ||
DEFAULT_EMAIL_NAME = | ||
FORGOT_PASSWORD_MAIL_TEMPLATE_ID = | ||
|
||
[INSPECTLET] | ||
KEY = | ||
|
||
[PAPERTRAIL] | ||
HOST = | ||
PORT = | ||
|
||
[SENDGRID] | ||
API_KEY = | ||
|
||
[OTP] | ||
DEFAULT_PHONE_NUMBER = | ||
DEFAULT_OTP = | ||
|
||
[TWILIO] | ||
ACCOUNT_SID = | ||
AUTH_TOKEN = | ||
MESSAGING_SERVICE_SID = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[SERVER_CONFIG] | ||
SERVER_PORT = 8080 | ||
WEB_APP_HOST = http://localhost:3000 | ||
MONGODB_CONNECTION_CACHING = True | ||
|
||
[LOGGER] | ||
LOGGER_TRANSPORTS = console | ||
|
||
|
||
[ACCOUNTS] | ||
TOKEN_SIGNING_KEY = JWT_TOKEN | ||
TOKEN_EXPIRY_DAYS = 1 | ||
TOKEN_EXPIRES_IN_SECONDS = 3600 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[LOGGER] | ||
LOGGER_TRANSPORTS = console | ||
|
||
[MONGODB] | ||
URI = mongodb://localhost:27017/frm-boilerplate-dev | ||
|
||
[SMS] | ||
SMS_ENABLED = False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[LOGGER] | ||
LOGGER_TRANSPORTS = console | ||
|
||
[MONGODB] | ||
URI = mongodb://db:27017/frm-boilerplate-dev | ||
|
||
[SMS] | ||
SMS_ENABLED = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
[MONGODB] | ||
URI = mongodb://db:27017/frm-boilerplate-test | ||
|
||
[MAILER] | ||
DEFAULT_EMAIL = DEFAULT_EMAIL | ||
DEFAULT_EMAIL_NAME = DEFAULT_EMAIL_NAME | ||
FORGOT_PASSWORD_MAIL_TEMPLATE_ID = FORGOT_PASSWORD_MAIL_TEMPLATE_ID | ||
|
||
[SMS] | ||
SMS_ENABLED = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[LOGGER] | ||
LOGGER_TRANSPORTS = console,papertrail | ||
|
||
[SMS] | ||
SMS_ENABLED = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[LOGGER] | ||
LOGGER_TRANSPORTS = console,papertrail | ||
|
||
[SMS] | ||
SMS_ENABLED = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[MONGODB] | ||
URI = mongodb://localhost:27017/frm-boilerplate-test | ||
|
||
[MAILER] | ||
DEFAULT_EMAIL = DEFAULT_EMAIL | ||
DEFAULT_EMAIL_NAME = DEFAULT_EMAIL_NAME | ||
FORGOT_PASSWORD_MAIL_TEMPLATE_ID = FORGOT_PASSWORD_MAIL_TEMPLATE_ID | ||
|
||
[SMS] | ||
SMS_ENABLED = False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,78 @@ | ||
from modules.common.dict_util import DictUtil | ||
from modules.config.config_manager import ConfigManager | ||
from modules.config.types import PapertrailConfig | ||
import configparser | ||
import os | ||
from typing import Any | ||
from pathlib import Path | ||
from modules.error.custom_errors import MissingKeyError | ||
from modules.common.types import ErrorCode | ||
import json | ||
|
||
|
||
class ConfigService: | ||
@staticmethod | ||
def get_string(key: str) -> str: | ||
return DictUtil.required_get_str(input_dict=ConfigManager.config, key=key) | ||
|
||
@staticmethod | ||
def get_bool(key: str) -> bool: | ||
return DictUtil.required_get_bool(input_dict=ConfigManager.config, key=key) | ||
|
||
@staticmethod | ||
def get_db_uri() -> str: | ||
return DictUtil.required_get_str(input_dict=ConfigManager.config, key="MONGODB_URI") | ||
|
||
@staticmethod | ||
def get_logger_transports() -> tuple: | ||
return DictUtil.required_get_tuple(input_dict=ConfigManager.config, key="LOGGER_TRANSPORTS") | ||
|
||
@staticmethod | ||
def get_papertrail_config() -> PapertrailConfig: | ||
return PapertrailConfig( | ||
host=DictUtil.required_get_str(input_dict=ConfigManager.config, key="PAPERTRAIL_HOST"), | ||
port=int(DictUtil.required_get_str(input_dict=ConfigManager.config, key="PAPERTRAIL_PORT")), | ||
) | ||
|
||
@staticmethod | ||
def get_accounts_config() -> dict: | ||
return DictUtil.required_get_dict(input_dict=ConfigManager.config, key="ACCOUNTS") | ||
|
||
@staticmethod | ||
def get_token_signing_key() -> str: | ||
return DictUtil.required_get_str(input_dict=ConfigService.get_accounts_config(), key="token_signing_key") | ||
|
||
@staticmethod | ||
def get_token_expiry_days() -> int: | ||
return DictUtil.required_get_int(input_dict=ConfigService.get_accounts_config(), key="token_expiry_days") | ||
def get_parent_directory(directory: str, levels: int) -> Path: | ||
parent_dir = Path(directory) | ||
for _ in range(levels): | ||
parent_dir = parent_dir.parent | ||
return parent_dir | ||
|
||
@staticmethod | ||
def get_web_app_host() -> str: | ||
return DictUtil.required_get_str(input_dict=ConfigManager.config, key="WEB_APP_HOST") | ||
|
||
@staticmethod | ||
def get_sendgrid_api_key() -> str: | ||
return str(DictUtil.required_get_dict(input_dict=ConfigManager.config, key="SENDGRID")["api_key"]) | ||
|
||
@staticmethod | ||
def get_mailer_config(key: str) -> str: | ||
return str(DictUtil.required_get_dict(input_dict=ConfigManager.config, key="MAILER")[key]) | ||
|
||
@staticmethod | ||
def get_password_reset_token() -> dict: | ||
return DictUtil.required_get_dict(input_dict=ConfigManager.config, key="PASSWORD_RESET_TOKEN") | ||
|
||
@staticmethod | ||
def get_twilio_config(key: str) -> str: | ||
return str(DictUtil.required_get_dict(input_dict=ConfigManager.config, key="TWILIO")[key]) | ||
|
||
@staticmethod | ||
def get_otp_config(key: str) -> str: | ||
return str(DictUtil.required_get_dict(input_dict=ConfigManager.config, key="OTP")[key]) | ||
|
||
@staticmethod | ||
def has_key(key: str) -> bool: | ||
return key in ConfigManager.config | ||
|
||
@staticmethod | ||
def has_default_phone_number() -> bool: | ||
if ConfigService.has_key("OTP") and "default_phone_number" in ConfigManager.config["OTP"]: | ||
return True | ||
return False | ||
class ConfigService: | ||
_config = None | ||
config_path = get_parent_directory(__file__, 6) / "config" | ||
|
||
@staticmethod | ||
def load_config(): | ||
config = configparser.ConfigParser() | ||
default_config = ConfigService.config_path / "default.ini" | ||
app_env = os.environ.get('APP_ENV', "development") | ||
app_env_config = ConfigService.config_path / f"{app_env}.ini" | ||
config.read([default_config, app_env_config]) | ||
ConfigService.__ensure_all_sections_exist(config, default_config) | ||
ConfigService.__load_environment_variables(config=config) | ||
ConfigService._config = config | ||
config_dict = { | ||
section: { | ||
key: value | ||
for key, value in ConfigService._config[section].items() | ||
} | ||
for section in ConfigService._config.sections() | ||
} | ||
print("config:", config_dict) | ||
|
||
@staticmethod | ||
def __ensure_all_sections_exist(config: configparser.ConfigParser, default_config_path: Path): | ||
default_config = configparser.ConfigParser() | ||
default_config.read(default_config_path) | ||
|
||
for section in default_config.sections(): | ||
if section not in config: | ||
config.add_section(section) | ||
|
||
@staticmethod | ||
def __load_environment_variables(config: configparser.ConfigParser): | ||
env_config = configparser.ConfigParser() | ||
custom_env_file = ConfigService.config_path / "custom-environment-variables.ini" | ||
|
||
if custom_env_file.exists(): | ||
env_config.read(custom_env_file) | ||
config.read(custom_env_file) | ||
|
||
for section in env_config.sections(): | ||
if section not in config: | ||
config.add_section(section) | ||
for key, value in env_config[section].items(): | ||
env_var = os.environ.get(value) | ||
if env_var is not None: | ||
config[section][key] = env_var | ||
|
||
@staticmethod | ||
def get_value(*, key: str, section: str = 'DEFAULT') -> Any: | ||
try: | ||
value = ConfigService._config.get(section, key, fallback=None) | ||
return value if value else None | ||
except (configparser.NoOptionError, configparser.NoSectionError): | ||
raise MissingKeyError(missing_key=key, error_code=ErrorCode.MISSING_KEY) | ||
|
||
@staticmethod | ||
def has_value(*, key: str, section: str = 'DEFAULT') -> bool: | ||
if ConfigService._config.has_option(section, key): | ||
value = ConfigService._config.get(section, key, fallback=None) | ||
return bool(value) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.