From f7ead39722b7654642de7823f5550947188a5161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Tue, 21 Nov 2023 11:23:59 +0800 Subject: [PATCH] Update project --- application/__init__.py | 2 -- application/extensions/__init__.py | 32 +++++++++++++++++++++- application/extensions/init_apscheduler.py | 2 +- application/extensions/init_consul.py | 21 ++++++++++++++ application/extensions/init_dotenv.py | 20 ++++++++++++-- 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/application/__init__.py b/application/__init__.py index 5086922..4b51ca5 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -44,7 +44,5 @@ def create_app() -> Flask: # This could include data migration scripts, administrative tasks, etc. init_script(app) - print(app.config) - # Return the fully initialized Flask application return app diff --git a/application/extensions/__init__.py b/application/extensions/__init__.py index 29e667b..0130c7d 100644 --- a/application/extensions/__init__.py +++ b/application/extensions/__init__.py @@ -13,6 +13,7 @@ from flask import Flask +# Import the initialization functions in alphabetical order from .init_apispec import init_apispec from .init_apscheduler import init_tasks from .init_config import init_config @@ -23,14 +24,43 @@ from .init_elasticsearch import init_elasticsearch from .init_logger import init_logger from .init_sqlalchemy import init_database - def init_plugs(app: Flask) -> None: + """ + This function initializes various plugins for a Flask application. + + It accepts a Flask application instance as an argument, and calls a series of initialization + functions on this instance, setting up several plugins. + + Each plugin is initialized by a function imported from a separate module. These initialization + functions should take the Flask application instance as an argument, and set up the plugin for + this instance. + + Note that the order in which these plugins are initialized can be important, as some plugins may + depend on others being already initialized. + """ + # Initialize the environment variables init_dotenv() + + # Initialize the application configuration init_config(app) + + # Initialize the application logging init_logger(app) + + # Initialize the database connection init_database(app) + + # Initialize the API specification init_apispec(app) + + # Initialize the Elasticsearch client init_elasticsearch(app) + + # Initialize the CORS policy init_cors(app) + + # Initialize the Consul client init_consul(app) + + # Initialize the scheduled tasks init_tasks(app) diff --git a/application/extensions/init_apscheduler.py b/application/extensions/init_apscheduler.py index 9445c2e..f8d2e38 100644 --- a/application/extensions/init_apscheduler.py +++ b/application/extensions/init_apscheduler.py @@ -13,8 +13,8 @@ from apscheduler.triggers.interval import IntervalTrigger from flask import Flask -from application.libs.tasks import task from application.libs.flask_apscheduler import APScheduler +from application.libs.tasks import task scheduler = APScheduler() diff --git a/application/extensions/init_consul.py b/application/extensions/init_consul.py index 0c0dde6..c12d1b6 100644 --- a/application/extensions/init_consul.py +++ b/application/extensions/init_consul.py @@ -10,10 +10,31 @@ # @Description : """ +# Import built-in modules from flask import Flask +# Import custom modules from application.libs import FlaskConsulService def init_consul(app: Flask) -> None: + """ + Initialize the Consul service. + + This function creates an instance of the FlaskConsulService class, passing + the Flask application instance as an argument. This sets up the Consul + service for the Flask application. + + Consul is a service networking solution to automate network configurations, + discover services, and enable secure connectivity across any cloud or runtime. + + Args: + app (Flask): The Flask application instance. + + Returns: + None + """ + # Initialize the FlaskConsulService with the Flask application instance. + # The FlaskConsulService constructor is responsible for setting up + # the Consul service for the Flask application. FlaskConsulService(app) diff --git a/application/extensions/init_dotenv.py b/application/extensions/init_dotenv.py index 9a7b19f..bef1f58 100644 --- a/application/extensions/init_dotenv.py +++ b/application/extensions/init_dotenv.py @@ -10,18 +10,34 @@ # @Description : """ +# Import standard library modules import os +# Import third-party library modules from dotenv import load_dotenv def init_dotenv() -> None: """ - Initialize the dotenv extension + Initialize the dotenv extension. - :return: None + This function locates the `.flaskenv` file at the root of the project + and loads it into environment variables using the dotenv package. + + The `.flaskenv` file is a convenient place to put configuration variables + that should be set early in the application's life cycle, such as + settings for the Flask command-line interface. + + Returns: + None """ + # Get the root path to the project by removing the 'extensions' + # part from the current file's absolute path root_path = os.path.abspath(os.path.dirname(__file__)).split('extensions')[0] + + # Construct the path to the .flaskenv file flask_env_path = os.path.join(root_path, '.flaskenv') + + # If the .flaskenv file exists, load it into environment variables if os.path.exists(flask_env_path): load_dotenv(flask_env_path) -- GitLab