| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-08 09:34:11 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  | from contexts.wrapper import RecyclableContextVar | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  | from dify_app import DifyApp | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ---------------------------- | 
					
						
							|  |  |  | # Application Factory Function | 
					
						
							|  |  |  | # ---------------------------- | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  | def create_flask_app_with_configs() -> DifyApp: | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     create a raw flask app | 
					
						
							|  |  |  |     with configs loaded from .env file | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     dify_app = DifyApp(__name__) | 
					
						
							|  |  |  |     dify_app.config.from_mapping(dify_config.model_dump()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  |     # add before request hook | 
					
						
							|  |  |  |     @dify_app.before_request | 
					
						
							|  |  |  |     def before_request(): | 
					
						
							|  |  |  |         # add an unique identifier to each request | 
					
						
							|  |  |  |         RecyclableContextVar.increment_thread_recycles() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  |     return dify_app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  | def create_app() -> DifyApp: | 
					
						
							|  |  |  |     start_time = time.perf_counter() | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  |     app = create_flask_app_with_configs() | 
					
						
							|  |  |  |     initialize_extensions(app) | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |     end_time = time.perf_counter() | 
					
						
							|  |  |  |     if dify_config.DEBUG: | 
					
						
							|  |  |  |         logging.info(f"Finished create_app ({round((end_time - start_time) * 1000, 2)} ms)") | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  |     return app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  | def initialize_extensions(app: DifyApp): | 
					
						
							|  |  |  |     from extensions import ( | 
					
						
							|  |  |  |         ext_app_metrics, | 
					
						
							|  |  |  |         ext_blueprints, | 
					
						
							|  |  |  |         ext_celery, | 
					
						
							|  |  |  |         ext_code_based_extension, | 
					
						
							|  |  |  |         ext_commands, | 
					
						
							|  |  |  |         ext_compress, | 
					
						
							|  |  |  |         ext_database, | 
					
						
							|  |  |  |         ext_hosting_provider, | 
					
						
							|  |  |  |         ext_import_modules, | 
					
						
							|  |  |  |         ext_logging, | 
					
						
							|  |  |  |         ext_login, | 
					
						
							|  |  |  |         ext_mail, | 
					
						
							|  |  |  |         ext_migrate, | 
					
						
							| 
									
										
										
										
											2025-04-11 17:04:06 +08:00
										 |  |  |         ext_otel, | 
					
						
							| 
									
										
										
										
											2025-04-22 17:04:20 +09:00
										 |  |  |         ext_otel_patch, | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |         ext_proxy_fix, | 
					
						
							|  |  |  |         ext_redis, | 
					
						
							| 
									
										
										
										
											2025-04-17 12:48:52 +09:00
										 |  |  |         ext_repositories, | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |         ext_sentry, | 
					
						
							|  |  |  |         ext_set_secretkey, | 
					
						
							|  |  |  |         ext_storage, | 
					
						
							|  |  |  |         ext_timezone, | 
					
						
							|  |  |  |         ext_warnings, | 
					
						
							| 
									
										
										
										
											2024-10-18 02:23:36 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |     extensions = [ | 
					
						
							|  |  |  |         ext_timezone, | 
					
						
							|  |  |  |         ext_logging, | 
					
						
							|  |  |  |         ext_warnings, | 
					
						
							|  |  |  |         ext_import_modules, | 
					
						
							|  |  |  |         ext_set_secretkey, | 
					
						
							|  |  |  |         ext_compress, | 
					
						
							|  |  |  |         ext_code_based_extension, | 
					
						
							|  |  |  |         ext_database, | 
					
						
							|  |  |  |         ext_app_metrics, | 
					
						
							|  |  |  |         ext_migrate, | 
					
						
							|  |  |  |         ext_redis, | 
					
						
							|  |  |  |         ext_storage, | 
					
						
							| 
									
										
										
										
											2025-04-17 12:48:52 +09:00
										 |  |  |         ext_repositories, | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |         ext_celery, | 
					
						
							|  |  |  |         ext_login, | 
					
						
							|  |  |  |         ext_mail, | 
					
						
							|  |  |  |         ext_hosting_provider, | 
					
						
							|  |  |  |         ext_sentry, | 
					
						
							|  |  |  |         ext_proxy_fix, | 
					
						
							|  |  |  |         ext_blueprints, | 
					
						
							|  |  |  |         ext_commands, | 
					
						
							| 
									
										
										
										
											2025-04-22 17:04:20 +09:00
										 |  |  |         ext_otel_patch,  # Apply patch before initializing OpenTelemetry | 
					
						
							| 
									
										
										
										
											2025-04-11 17:04:06 +08:00
										 |  |  |         ext_otel, | 
					
						
							| 
									
										
										
										
											2024-11-30 23:05:22 +08:00
										 |  |  |     ] | 
					
						
							|  |  |  |     for ext in extensions: | 
					
						
							|  |  |  |         short_name = ext.__name__.split(".")[-1] | 
					
						
							|  |  |  |         is_enabled = ext.is_enabled() if hasattr(ext, "is_enabled") else True | 
					
						
							|  |  |  |         if not is_enabled: | 
					
						
							|  |  |  |             if dify_config.DEBUG: | 
					
						
							|  |  |  |                 logging.info(f"Skipped {short_name}") | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         start_time = time.perf_counter() | 
					
						
							|  |  |  |         ext.init_app(app) | 
					
						
							|  |  |  |         end_time = time.perf_counter() | 
					
						
							|  |  |  |         if dify_config.DEBUG: | 
					
						
							|  |  |  |             logging.info(f"Loaded {short_name} ({round((end_time - start_time) * 1000, 2)} ms)") | 
					
						
							| 
									
										
										
										
											2024-12-18 09:05:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def create_migrations_app(): | 
					
						
							|  |  |  |     app = create_flask_app_with_configs() | 
					
						
							|  |  |  |     from extensions import ext_database, ext_migrate | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Initialize only required extensions | 
					
						
							|  |  |  |     ext_database.init_app(app) | 
					
						
							|  |  |  |     ext_migrate.init_app(app) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return app |