diff --git a/application/__init__.py b/application/__init__.py index 19dfdf17d69394b472b4ca5d8ef56d120fe03002..319d05d5ab77bc673637803a636eef042919cc1b 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -18,7 +18,7 @@ from application.extensions import init_plugs from application.views import init_views from application.script import init_script from application.utils import make_celery -from celery.schedules import crontab +from application.common import config def create_app() -> Flask: @@ -38,22 +38,8 @@ def create_app() -> Flask: # This could include things like database connectors, authentication systems, etc. init_plugs(app) - celery = make_celery(app) - celery.conf.update(app.config) - - @celery.task(bind=True) - def update_database(self): - # Add your database operations here - print("update database") - - celery.conf.beat_schedule = { - 'update-database-every-minute': { - 'task': 'application.__init__.update_database', - 'schedule': crontab(minute='*') # Execute every minute - } - } - - app.celery = celery + # Set universal config + config.Universal = app.config # Register the routes that this application will respond to. # This includes both the route URLs and the handlers for each route. diff --git a/application/common/__init__.py b/application/common/__init__.py index ab260d70dc3101e5fe2eb712a1d0d7d290977966..52f613cec30802fe71d0c34e2ebb413835011c62 100644 --- a/application/common/__init__.py +++ b/application/common/__init__.py @@ -10,6 +10,4 @@ # @Description : """ -from .config import ConfigHelper -from .env import EnvVarHelper -from .file import FileHelper +from .config import Config diff --git a/application/common/config.py b/application/common/config.py index 6b463d562ad3e9eb310d1688f769d2c67504d69d..6da156853ce6ac14e0e538298dffe522e258126c 100644 --- a/application/common/config.py +++ b/application/common/config.py @@ -5,46 +5,18 @@ # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com -# @Date : 2023/11/3 19:43 +# @Date : 2023/11/11 14:11 # @File : config.py # @Description : """ -from typing import Any +# config.py +class Config: + """Configuration for the application.""" + def __init__(self): + self.Universal = None -from flask import Flask -from loguru import logger -from application.libs import ConfigKeyError - - -class ConfigHelper: - """ - The ConfigHelper class is a utility for fetching configuration values - from a Flask application. - - :param app: The Flask application instance from which to fetch configuration values. - """ - - def __init__(self, app: Flask): - self.app = app - - def __getattr__(self, name: str) -> Any: - """ - Get a config value as an attribute. - - :param name: The name of the config key. - :return: The value for the provided key, or None if it does not exist. - """ - try: - return self.app.config[name] - except KeyError: - logger.error(f'Key {name} not found in configuration') - raise ConfigKeyError(name) - - def __repr__(self): - return f"" - - def __str__(self): - return f"ConfigHelper for app {self.app}" +# This creates a global instance of the Config class +config = Config() diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index 45921e759701391ba8b20a86e22bd44992f44cf7..eabf9170e3b0721a0b1743c952f4cc08da23886e 100644 --- a/application/extensions/__init__.py +++ b/application/extensions/__init__.py @@ -21,7 +21,6 @@ from .init_migrate import init_migrate from .init_apispec import init_apispec from .init_marshmallow import init_marshmallow from .init_elasticsearch import init_elasticsearch -# from .init_celery import init_celery def init_plugs(app: Flask) -> None: @@ -34,4 +33,3 @@ def init_plugs(app: Flask) -> None: init_marshmallow(app) init_elasticsearch(app) init_cors(app) - # init_celery(app) diff --git a/application/extensions/init_celery.py b/application/extensions/init_celery.py deleted file mode 100644 index 8df4e59e47b701b76437f5b396e9cd4c8c3777d0..0000000000000000000000000000000000000000 --- a/application/extensions/init_celery.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/11/7 16:10 -# @File : init_celery.py -# @Description : -""" - -from flask import Flask -from celery.schedules import crontab - -from application.utils import make_celery - - -def init_celery(app: Flask) -> None: - celery = make_celery(app) - app.celery = celery - - celery.conf.beat_schedule = { - 'update-database-every-day': { - 'task': 'application.libs.tasks.task.update_database', - 'schedule': crontab(minute='*') # Execute every minute - } - } diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index e4225b4701cb3cd71886772f94cb6f09fb4bd695..e9eaa5ba38438629cc2210d707369bb81f93f926 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -36,4 +36,7 @@ def init_database(app: Flask) -> None: driver=cfg.Driver, ) app.config.setdefault('SQLALCHEMY_DATABASE_URI', uri.create()) + db.app = app db.init_app(app) + with app.app_context(): + db.create_all() diff --git a/application/libs/flask_elasticsearch/elasticsearch.py b/application/libs/flask_elasticsearch/elasticsearch.py index 7be6ee4e67b46537f7a9e4bd6125a6422f475524..e0f5dafa6acc7502775ee3a05861301e293c731d 100644 --- a/application/libs/flask_elasticsearch/elasticsearch.py +++ b/application/libs/flask_elasticsearch/elasticsearch.py @@ -19,7 +19,7 @@ from flask import _app_ctx_stack as stack_context from flask_elasticsearch import FlaskElasticsearch as BaseFlaskElasticsearch from loguru import logger -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper class FlaskElasticsearch(BaseFlaskElasticsearch): diff --git a/application/libs/flask_loguru/logger.py b/application/libs/flask_loguru/logger.py index 98b57ff4601a93fa01f5c7dbd9eeb0458f4c8d27..95a369b978265865acf830e3a341e2f09bbde543 100644 --- a/application/libs/flask_loguru/logger.py +++ b/application/libs/flask_loguru/logger.py @@ -17,7 +17,7 @@ from flask import Flask, request, g from loguru import logger from .format import patching -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper class FlaskLoguru: diff --git a/application/libs/helper/__init__.py b/application/libs/helper/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c304e7b93b349722399ddef7016efb0dd9d3272a --- /dev/null +++ b/application/libs/helper/__init__.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/11 14:05 +# @File : __init__.py +# @Description : +""" + +from .config import ConfigHelper +from .env import EnvVarHelper +from .file import FileHelper diff --git a/application/libs/helper/config.py b/application/libs/helper/config.py new file mode 100644 index 0000000000000000000000000000000000000000..6b463d562ad3e9eb310d1688f769d2c67504d69d --- /dev/null +++ b/application/libs/helper/config.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +# @Version : Python 3.11.4 +# @Software : Sublime Text 4 +# @Author : StudentCWZ +# @Email : StudentCWZ@outlook.com +# @Date : 2023/11/3 19:43 +# @File : config.py +# @Description : +""" + + +from typing import Any + +from flask import Flask +from loguru import logger + +from application.libs import ConfigKeyError + + +class ConfigHelper: + """ + The ConfigHelper class is a utility for fetching configuration values + from a Flask application. + + :param app: The Flask application instance from which to fetch configuration values. + """ + + def __init__(self, app: Flask): + self.app = app + + def __getattr__(self, name: str) -> Any: + """ + Get a config value as an attribute. + + :param name: The name of the config key. + :return: The value for the provided key, or None if it does not exist. + """ + try: + return self.app.config[name] + except KeyError: + logger.error(f'Key {name} not found in configuration') + raise ConfigKeyError(name) + + def __repr__(self): + return f"" + + def __str__(self): + return f"ConfigHelper for app {self.app}" diff --git a/application/common/env.py b/application/libs/helper/env.py similarity index 100% rename from application/common/env.py rename to application/libs/helper/env.py diff --git a/application/common/file.py b/application/libs/helper/file.py similarity index 100% rename from application/common/file.py rename to application/libs/helper/file.py diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index 2116426f31e4f176e44e39ff853d75bdf32cf2b0..bc0c3bbcd2541dae9ac7e75d1e0fc863cfa26146 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -9,13 +9,3 @@ # @File : task.py # @Description : """ - - -from flask import current_app - - -@current_app.celery.task -def update_database(): - # Add your database operations here - print(123312312) - pass diff --git a/application/utils/loaders/consul_loader.py b/application/utils/loaders/consul_loader.py index d0ffa44ddf50a9a1329e5f713ec98dacb7947ebe..10f09b5a9e9467f52158e9a8a229f75e30e28a46 100644 --- a/application/utils/loaders/consul_loader.py +++ b/application/utils/loaders/consul_loader.py @@ -16,8 +16,8 @@ import requests from dynaconf.base import LazySettings from dynaconf.utils.parse_conf import parse_conf_data -from application.common import EnvVarHelper from application.libs import ConsulConfig +from application.libs.helper import EnvVarHelper IDENTIFIER = "consul_loader" diff --git a/application/utils/loaders/yaml_loader.py b/application/utils/loaders/yaml_loader.py index 0ddcd23b5d1d3dfac2e6a8139de15e0de4462b4b..fe92c6e0ba763d662cacc6597fc812bc0480d8eb 100644 --- a/application/utils/loaders/yaml_loader.py +++ b/application/utils/loaders/yaml_loader.py @@ -17,8 +17,8 @@ from typing import Union from dynaconf.base import LazySettings from dynaconf.utils.parse_conf import parse_conf_data -from application.common import FileHelper from application.libs import LocalConfig +from application.libs.helper import FileHelper IDENTIFIER = "yaml_loader" diff --git a/application/views/user/user.py b/application/views/user/user.py index dd947cb3ae97f3ea4e4cd56becc3d5c7e86f6cb7..b5ed835f1965c1f319a88ade56aba216f829af3e 100644 --- a/application/views/user/user.py +++ b/application/views/user/user.py @@ -18,7 +18,7 @@ from marshmallow import ValidationError from application.schemas import UserSchema from application.utils import ElasticsearchUtil -from application.common import ConfigHelper +from application.libs.helper import ConfigHelper user_api = Blueprint('user_api', __name__) api_users = Api(user_api) @@ -54,16 +54,16 @@ def create_user(): @user_api.route('/tests', methods=['POST']) def test_user(): - data = request.get_json() - start = data.get('start') - end = data.get('end') - dsl = ElasticsearchUtil.dsl(start, end) - index = current_app.config.Elasticsearch.Index - res = ElasticsearchUtil.search(index, dsl) - print(len(res)) - data = { + # data = request.get_json() + # start = data.get('start') + # end = data.get('end') + # dsl = ElasticsearchUtil.dsl(start, end) + # index = current_app.config.Elasticsearch.Index + # res = ElasticsearchUtil.search(index, dsl) + # print(len(res)) + res = { "user_name": "libai", "user_age": 18, } - res_json = json.dumps(data) + res_json = json.dumps(res) return res_json, 200