Commit 96c23853 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 5a67515b
......@@ -14,6 +14,7 @@
from typing import Any
from flask import Flask
from loguru import logger
class ConfigHelper:
......@@ -27,14 +28,18 @@ class ConfigHelper:
def __init__(self, app: Flask):
self.app = app
def get_config(self, key: str) -> Any:
def __getattr__(self, name):
"""
Fetch a config value based on the provided key.
Get a config value as an attribute.
:param key: The key for the config value.
:return: The value for the provided key.
:param name: The name of the config key.
:return: The value for the provided key, or None if it does not exist.
"""
return self.app.config.get(key)
try:
return self.app.config[name]
except KeyError:
logger.error(f'Key {name} error')
raise KeyError(f'Key {name} error')
def __repr__(self):
return f"<ConfigHelper with app {self.app}>"
......
......@@ -50,8 +50,8 @@ class FlaskElasticsearch:
if ctx.elasticsearch.ping():
logger.info('Connected to Elasticsearch')
else:
logger.error('Connected to Elasticsearch')
raise ConnectionError('Connected to Elasticsearch')
logger.error('Can not connect to Elasticsearch')
raise ConnectionError('Can not connect to Elasticsearch')
return getattr(ctx.elasticsearch, item)
@staticmethod
......@@ -60,7 +60,7 @@ class FlaskElasticsearch:
with current_app.app_context():
if current_app:
config_helper = ConfigHelper(current_app)
cfg = config_helper.get_config('Elasticsearch')
cfg = config_helper.Elasticsearch
if cfg is None:
raise KeyError('Key Elasticsearch is not defined')
......
......@@ -17,6 +17,7 @@ from marshmallow import ValidationError
from application.schemas import UserSchema
from application.utils import ElasticsearchUtils
from application.common import ConfigHelper
user_api = Blueprint('user_api', __name__)
......@@ -51,6 +52,9 @@ def create_user():
@user_api.route('/tests', methods=['POST'])
def test_user():
ch = ConfigHelper(current_app)
print(ch.System)
print(ch.Sys)
data = request.get_json()
start = data.get('start')
end = data.get('end')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment