first commit

This commit is contained in:
ekazak
2022-12-14 12:36:37 +01:00
parent daedbff293
commit 2bde5c709a
18 changed files with 570 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
{
"application_credentials": {
"api_url":"http://app.livecarta.loc/meta/api/",
"api_key":"695e513c-xxxx-xxxx-a666-xxxxxxxxxx"
}
}

64
configs/config.py Normal file
View File

@@ -0,0 +1,64 @@
import json
import requests
from dynaconf import Dynaconf, Validator
from mongoengine import connect
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)
)
def get_bulk_insert_limit(self):
if not self.config.bulk_limit:
return 1
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!')
return self.config.sources[name]
def get_sources_list(self):
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)
}
r = requests.get(creds.api_url + "sources", headers=headers)
if r.status_code != 200:
raise Exception('Bad app response')
new_config = {"sources": {}}
for source in r.json():
new_config["sources"][source["source_name"]] = source
with open("./configs/sources.json", "w") as outfile:
outfile.write(json.dumps(new_config))
config = AppConfig()

6
configs/db.json Normal file
View File

@@ -0,0 +1,6 @@
{
"db": {
"host": "mongo_book_meta",
"database": "mongo_book_meta"
}
}

3
configs/main.json Normal file
View File

@@ -0,0 +1,3 @@
{
"bulk_limit": 100
}

0
configs/sources.json Normal file
View File