From e50cc762cf9259407c233fd7346d246027e945a4 Mon Sep 17 00:00:00 2001 From: Egor Svitin Date: Mon, 30 Jan 2023 10:45:26 +0300 Subject: [PATCH] add build settings --- .env.sample | 5 +++++ build/build.xml | 41 +++++++++++++++++++++++++++++++++++++++++ configs/config.py | 24 ++++++------------------ configs/configs.py | 12 ++++++++++++ sources/BaseSource.py | 4 +++- 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 .env.sample create mode 100644 build/build.xml create mode 100644 configs/configs.py diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..ecf5645 --- /dev/null +++ b/.env.sample @@ -0,0 +1,5 @@ +DB_NAME=@lcDbName@ +DB_HOST=@lcDbHost@ + +API_URL=@lcApiUrl@ +API_KEY=@lcApiKey@ diff --git a/build/build.xml b/build/build.xml new file mode 100644 index 0000000..852f115 --- /dev/null +++ b/build/build.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/configs/config.py b/configs/config.py index 1b09ae5..af7f991 100644 --- a/configs/config.py +++ b/configs/config.py @@ -4,24 +4,19 @@ import requests from dynaconf import Dynaconf, Validator from mongoengine import connect +from configs.configs import CONFIGS + class AppConfig: def __init__(self): self.config = Dynaconf(settings_files=[ "/app/configs/main.json", - "/app/configs/application_credentials.json", - "/app/configs/db.json", "/app/configs/sources.json" ]) - self.config.validators.register( - Validator('db', 'db.host', 'db.database', must_exist=True), - ) - self.config.validators.validate() - creds = self.get_db_config() connect( - db=creds.database, - host="mongodb://{host}:27017/{database}".format(host=creds.host, database=creds.database) + db=CONFIGS['db']['name'], + host="mongodb://{host}:27017/{database}".format(host=CONFIGS['db']['host'], database=CONFIGS['db']['name']) ) def get_bulk_insert_limit(self): @@ -30,12 +25,6 @@ class AppConfig: else: return self.config.bulk_limit - def get_db_config(self): - return self.config.db - - def get_main_app_creds(self): - return self.config.application_credentials - def get_source_by_name(self, name: str): if name not in self.config.sources: raise ValueError(f'"{name}" source not exists!') @@ -45,12 +34,11 @@ class AppConfig: return self.config.sources.keys() def update_sources(self): - creds = self.get_main_app_creds() headers = { 'Content-type': 'application/json', - 'Authorization': 'Bearer {key}'.format(key=creds.api_key) + 'Authorization': 'Bearer {key}'.format(key=CONFIGS['application_credentials']['api_key']) } - r = requests.get(creds.api_url + "sources", headers=headers) + r = requests.get(CONFIGS['application_credentials']['api_url'] + "sources", headers=headers) if r.status_code != 200: raise Exception('Bad app response') diff --git a/configs/configs.py b/configs/configs.py new file mode 100644 index 0000000..7983a96 --- /dev/null +++ b/configs/configs.py @@ -0,0 +1,12 @@ +import os + +CONFIGS = { + 'db' : { + 'name' : os.environ['DB_NAME'], + 'host' : os.environ['DB_HOST'] + }, + 'application_credentials' : { + 'api_url' : os.environ['API_URL'], + 'api_key' : os.environ['API_KEY'] + } +} \ No newline at end of file diff --git a/sources/BaseSource.py b/sources/BaseSource.py index aac4d9d..a3c76b0 100644 --- a/sources/BaseSource.py +++ b/sources/BaseSource.py @@ -7,10 +7,12 @@ from pydantic import BaseModel, Extra from components.SenderComponent import SenderComponent from configs.config import config +from configs.configs import CONFIGS from models.MetaData import MetaData from sources.source_types.AbstractSource import AbstractSource + class BaseSource(BaseModel): source_name: str source: dict @@ -24,7 +26,7 @@ class BaseSource(BaseModel): def __init__(self, **kwargs): super().__init__(**kwargs) - self.senderComponent = SenderComponent(**config.get_main_app_creds()) + self.senderComponent = SenderComponent(api_url=CONFIGS['application_credentials']['api_url'], api_key=CONFIGS['application_credentials']['api_key']) def get_source_object(config): class_name = config.type.capitalize() + "Source"