Spaces:
Running
Running
Delete main.py
Browse files
main.py
DELETED
|
@@ -1,191 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
# main.py
|
| 3 |
-
# This is the main entry point of the application.
|
| 4 |
-
# It now handles asynchronous initialization of the fundament modules.
|
| 5 |
-
import sys
|
| 6 |
-
import logging
|
| 7 |
-
import asyncio
|
| 8 |
-
import os
|
| 9 |
-
from typing import Dict, Any, Optional
|
| 10 |
-
|
| 11 |
-
import importlib.util
|
| 12 |
-
import datetime
|
| 13 |
-
|
| 14 |
-
if 'fundaments' in sys.modules:
|
| 15 |
-
del sys.modules['fundaments']
|
| 16 |
-
|
| 17 |
-
# We import our core modules from the "fundaments" directory.
|
| 18 |
-
try:
|
| 19 |
-
from fundaments.config_handler import config_service
|
| 20 |
-
from fundaments.postgresql import init_db_pool, close_db_pool
|
| 21 |
-
from fundaments.encryption import Encryption
|
| 22 |
-
from fundaments.access_control import AccessControl
|
| 23 |
-
from fundaments.user_handler import UserHandler
|
| 24 |
-
from fundaments.security import Security
|
| 25 |
-
from fundaments.debug import PyFundamentsDebug
|
| 26 |
-
except ImportError as e:
|
| 27 |
-
print(f"Error: Failed to import a fundament module: {e}")
|
| 28 |
-
print("Please ensure the modules and dependencies are present.")
|
| 29 |
-
sys.exit(1)
|
| 30 |
-
# Debug run
|
| 31 |
-
debug = PyFundamentsDebug()
|
| 32 |
-
debug.run()
|
| 33 |
-
|
| 34 |
-
# Logger configuration - conditional based on ENV
|
| 35 |
-
log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
|
| 36 |
-
log_to_tmp = os.getenv('LOG_TO_TMP', 'false').lower() == 'true'
|
| 37 |
-
enable_public_logs = os.getenv('ENABLE_PUBLIC_LOGS', 'true').lower() == 'true'
|
| 38 |
-
|
| 39 |
-
if enable_public_logs:
|
| 40 |
-
if log_to_tmp:
|
| 41 |
-
log_file = '/tmp/pyfundaments.log'
|
| 42 |
-
logging.basicConfig(
|
| 43 |
-
level=getattr(logging, log_level),
|
| 44 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 45 |
-
handlers=[
|
| 46 |
-
logging.FileHandler(log_file),
|
| 47 |
-
logging.StreamHandler()
|
| 48 |
-
]
|
| 49 |
-
)
|
| 50 |
-
else:
|
| 51 |
-
logging.basicConfig(
|
| 52 |
-
level=getattr(logging, log_level),
|
| 53 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 54 |
-
)
|
| 55 |
-
else:
|
| 56 |
-
# Silent mode - only critical errors
|
| 57 |
-
logging.basicConfig(level=logging.CRITICAL)
|
| 58 |
-
|
| 59 |
-
logger = logging.getLogger('main_app_loader')
|
| 60 |
-
|
| 61 |
-
async def initialize_fundaments() -> Dict[str, Any]:
|
| 62 |
-
"""
|
| 63 |
-
Initializes core application services conditionally based on available ENV variables.
|
| 64 |
-
Only loads services for which the required configuration is present.
|
| 65 |
-
"""
|
| 66 |
-
logger.info("Starting conditional initialization of fundament modules...")
|
| 67 |
-
|
| 68 |
-
fundaments = {
|
| 69 |
-
"config": config_service
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
# --- Database Initialization (PostgreSQL) ---
|
| 73 |
-
# Only initialize if DATABASE_URL is available
|
| 74 |
-
database_url = config_service.get("DATABASE_URL")
|
| 75 |
-
if database_url and database_url != "your_database_dsn_here":
|
| 76 |
-
try:
|
| 77 |
-
db_service = await init_db_pool(database_url)
|
| 78 |
-
fundaments["db"] = db_service
|
| 79 |
-
logger.info("Database service initialized.")
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.warning(f"Database initialization failed, continuing without DB: {e}")
|
| 82 |
-
fundaments["db"] = None
|
| 83 |
-
else:
|
| 84 |
-
logger.info("No valid DATABASE_URL found, skipping database initialization.")
|
| 85 |
-
fundaments["db"] = None
|
| 86 |
-
|
| 87 |
-
# --- Encryption Initialization ---
|
| 88 |
-
# Only initialize if both encryption keys are available
|
| 89 |
-
master_key = config_service.get("MASTER_ENCRYPTION_KEY")
|
| 90 |
-
persistent_salt = config_service.get("PERSISTENT_ENCRYPTION_SALT")
|
| 91 |
-
|
| 92 |
-
if master_key and persistent_salt and master_key != "your_256_bit_key_here":
|
| 93 |
-
try:
|
| 94 |
-
encryption_service = Encryption(master_key=master_key, salt=persistent_salt)
|
| 95 |
-
fundaments["encryption"] = encryption_service
|
| 96 |
-
logger.info("Encryption service initialized.")
|
| 97 |
-
except Exception as e:
|
| 98 |
-
logger.warning(f"Encryption initialization failed, continuing without encryption: {e}")
|
| 99 |
-
fundaments["encryption"] = None
|
| 100 |
-
else:
|
| 101 |
-
logger.info("Encryption keys not found or using defaults, skipping encryption initialization.")
|
| 102 |
-
fundaments["encryption"] = None
|
| 103 |
-
|
| 104 |
-
# --- Access Control Initialization ---
|
| 105 |
-
# Only initialize if we have a database connection
|
| 106 |
-
if fundaments["db"] is not None:
|
| 107 |
-
try:
|
| 108 |
-
access_control_service = AccessControl()
|
| 109 |
-
fundaments["access_control"] = access_control_service
|
| 110 |
-
logger.info("Access Control service initialized.")
|
| 111 |
-
except Exception as e:
|
| 112 |
-
logger.warning(f"Access Control initialization failed: {e}")
|
| 113 |
-
fundaments["access_control"] = None
|
| 114 |
-
else:
|
| 115 |
-
logger.info("No database available, skipping Access Control initialization.")
|
| 116 |
-
fundaments["access_control"] = None
|
| 117 |
-
|
| 118 |
-
# --- User Handler Initialization ---
|
| 119 |
-
# Only initialize if we have a database connection
|
| 120 |
-
if fundaments["db"] is not None:
|
| 121 |
-
try:
|
| 122 |
-
user_handler_service = UserHandler(fundaments["db"])
|
| 123 |
-
fundaments["user_handler"] = user_handler_service
|
| 124 |
-
logger.info("User Handler service initialized.")
|
| 125 |
-
except Exception as e:
|
| 126 |
-
logger.warning(f"User Handler initialization failed: {e}")
|
| 127 |
-
fundaments["user_handler"] = None
|
| 128 |
-
else:
|
| 129 |
-
logger.info("No database available, skipping User Handler initialization.")
|
| 130 |
-
fundaments["user_handler"] = None
|
| 131 |
-
|
| 132 |
-
# --- Security Manager Initialization ---
|
| 133 |
-
# Only initialize if we have the required sub-services
|
| 134 |
-
available_services = {k: v for k, v in fundaments.items() if v is not None and k != "config"}
|
| 135 |
-
|
| 136 |
-
if len(available_services) >= 1: # At least one service beyond config
|
| 137 |
-
try:
|
| 138 |
-
# Filter out None services for Security manager
|
| 139 |
-
fundament_services = {
|
| 140 |
-
k: v for k, v in {
|
| 141 |
-
"user_handler": fundaments.get("user_handler"),
|
| 142 |
-
"access_control": fundaments.get("access_control"),
|
| 143 |
-
"encryption": fundaments.get("encryption")
|
| 144 |
-
}.items() if v is not None
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
if fundament_services: # Only if we have actual services
|
| 148 |
-
security_service = Security(fundament_services)
|
| 149 |
-
fundaments["security"] = security_service
|
| 150 |
-
logger.info("Security manager initialized.")
|
| 151 |
-
else:
|
| 152 |
-
logger.info("No services available for Security manager, skipping initialization.")
|
| 153 |
-
fundaments["security"] = None
|
| 154 |
-
except Exception as e:
|
| 155 |
-
logger.warning(f"Security manager initialization failed: {e}")
|
| 156 |
-
fundaments["security"] = None
|
| 157 |
-
else:
|
| 158 |
-
logger.info("Insufficient services for Security manager, skipping initialization.")
|
| 159 |
-
fundaments["security"] = None
|
| 160 |
-
|
| 161 |
-
# Log what was actually initialized
|
| 162 |
-
initialized_services = [k for k, v in fundaments.items() if v is not None]
|
| 163 |
-
logger.info(f"Successfully initialized services: {', '.join(initialized_services)}")
|
| 164 |
-
|
| 165 |
-
return fundaments
|
| 166 |
-
|
| 167 |
-
async def main():
|
| 168 |
-
"""
|
| 169 |
-
The main asynchronous function of the application.
|
| 170 |
-
"""
|
| 171 |
-
logger.info("Starting main.py...")
|
| 172 |
-
|
| 173 |
-
fundaments = await initialize_fundaments()
|
| 174 |
-
|
| 175 |
-
try:
|
| 176 |
-
# Load the actual app logic here.
|
| 177 |
-
# This is where your 'app/app.py' would be imported and run.
|
| 178 |
-
logger.info("Fundament modules are ready for the app logic.")
|
| 179 |
-
# Example:
|
| 180 |
-
from app.app import start_application
|
| 181 |
-
await start_application(fundaments)
|
| 182 |
-
|
| 183 |
-
finally:
|
| 184 |
-
# Ensure the database pool is closed gracefully on exit (if it was initialized)
|
| 185 |
-
if fundaments.get("db") is not None:
|
| 186 |
-
await close_db_pool()
|
| 187 |
-
logger.info("Database pool closed.")
|
| 188 |
-
logger.info("Application shut down.")
|
| 189 |
-
|
| 190 |
-
if __name__ == "__main__":
|
| 191 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|