Update app.py
Browse files
app.py
CHANGED
|
@@ -49,42 +49,35 @@ except Exception as e:
|
|
| 49 |
#mongo_connection_string = os.environ.get("MONGODB_CONNECTION_STRING")
|
| 50 |
cosmos_mongodb_connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Verificar que la colecci贸n existe
|
| 80 |
-
if 'text_analysis' in mongo_db.list_collection_names():
|
| 81 |
-
print("Colecci贸n 'text_analysis' encontrada")
|
| 82 |
-
else:
|
| 83 |
-
print("Advertencia: La colecci贸n 'text_analysis' no existe. Se crear谩 autom谩ticamente al insertar el primer documento.")
|
| 84 |
-
|
| 85 |
-
except Exception as e:
|
| 86 |
-
logger.error(f"Error al conectar con Cosmos DB MongoDB API: {str(e)}", exc_info=True)
|
| 87 |
-
raise
|
| 88 |
|
| 89 |
# Funci贸n para insertar un documento
|
| 90 |
def insert_document(document):
|
|
|
|
| 49 |
#mongo_connection_string = os.environ.get("MONGODB_CONNECTION_STRING")
|
| 50 |
cosmos_mongodb_connection_string = os.getenv("MONGODB_CONNECTION_STRING")
|
| 51 |
|
| 52 |
+
# Variable global para el cliente de MongoDB
|
| 53 |
+
mongo_client = None
|
| 54 |
+
db = None
|
| 55 |
+
analysis_collection = None
|
| 56 |
|
| 57 |
+
def initialize_mongodb_connection():
|
| 58 |
+
global mongo_client, db, analysis_collection
|
| 59 |
+
try:
|
| 60 |
+
# Crear el cliente de MongoDB con configuraci贸n TLS
|
| 61 |
+
mongo_client = MongoClient(cosmos_mongodb_connection_string,
|
| 62 |
+
tls=True,
|
| 63 |
+
tlsCAFile=certifi.where(),
|
| 64 |
+
retryWrites=False,
|
| 65 |
+
serverSelectionTimeoutMS=5000,
|
| 66 |
+
connectTimeoutMS=10000,
|
| 67 |
+
socketTimeoutMS=10000)
|
| 68 |
+
|
| 69 |
+
# Forzar una conexi贸n para verificar
|
| 70 |
+
mongo_client.admin.command('ping')
|
| 71 |
+
|
| 72 |
+
# Seleccionar la base de datos y la colecci贸n
|
| 73 |
+
db = mongo_client['aideatext_db']
|
| 74 |
+
analysis_collection = db['text_analysis']
|
| 75 |
+
|
| 76 |
+
logger.info("Conexi贸n a Cosmos DB MongoDB API exitosa")
|
| 77 |
+
return True
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Error al conectar con Cosmos DB MongoDB API: {str(e)}", exc_info=True)
|
| 80 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Funci贸n para insertar un documento
|
| 83 |
def insert_document(document):
|