Added changes to database server logic.

This commit is contained in:
Michael Woods
2025-12-21 23:04:48 -05:00
parent 64b3d46586
commit f960baaf37

View File

@@ -15,6 +15,7 @@ import sys
import uvicorn import uvicorn
import ZODB.FileStorage import ZODB.FileStorage
import ZODB.DB import ZODB.DB
import logging
from packetserver.http.server import app from packetserver.http.server import app
# Global DB and connection for reuse in the FastAPI dependency # Global DB and connection for reuse in the FastAPI dependency
@@ -25,21 +26,18 @@ _connection = None
def open_database(db_arg: str) -> ZODB.DB: def open_database(db_arg: str) -> ZODB.DB:
""" """
Open a ZODB database from either a local FileStorage path or ZEO address. Open a ZODB database from either a local FileStorage path or ZEO address.
Reuses the same logic as http_user_manager.py.
""" """
if ":" in db_arg and db_arg.count(":") == 1 and "." in db_arg.split(":")[0]: if ":" in db_arg and db_arg.count(":") == 1 and "." in db_arg.split(":")[0]:
logging.debug("using ZEO storage")
import ZEO import ZEO
host, port_str = db_arg.split(":") host, port_str = db_arg.split(":")
try:
port = int(port_str) port = int(port_str)
except ValueError: storage = ZEO.client_storage((host, port))
raise ValueError(f"Invalid port in ZEO address: {db_arg}") return ZODB.DB(storage) # return the DB
storage = ZEO.DB((host, port))
return storage
else: else:
# Local FileStorage path logging.debug("using standard storage")
storage = ZODB.FileStorage.FileStorage(db_arg) storage = ZODB.FileStorage.FileStorage(db_arg)
return ZODB.DB(storage) return ZODB.DB(storage) # return the DB
def get_db_connection(): def get_db_connection():