From 3e3aa3ec5f9a53885e2b17a080f312172fe92333 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Thu, 11 Jan 2024 09:39:45 -0500 Subject: [PATCH] Clean up __init__ in librarian_server. --- alembic/env.py | 7 ++--- librarian_server/__init__.py | 26 +++++++++---------- .../librarian_server_start.py | 2 +- tests/conftest.py | 7 +++-- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 6a4c3ed..841eb4e 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -18,8 +18,6 @@ config = context.config fileConfig(config.config_file_name) -from librarian_server import app, engine - from librarian_server.settings import server_settings from librarian_server.database import Base, engine @@ -37,9 +35,8 @@ def run_migrations_offline(): render_as_batch=True, ) - with app.app_context(): - with context.begin_transaction(): - context.run_migrations() + with context.begin_transaction(): + context.run_migrations() def run_migrations_online(): diff --git a/librarian_server/__init__.py b/librarian_server/__init__.py index dda46f6..d88b0fe 100644 --- a/librarian_server/__init__.py +++ b/librarian_server/__init__.py @@ -5,23 +5,23 @@ asynchronously, and that background tasks can work on any available ASGI server. """ -import os - from .settings import server_settings -from .logger import log -from .database import engine, session - from fastapi import FastAPI -log.info("Starting Librarian v2.0 server.") -log.debug("Creating FastAPI app instance.") +def main() -> FastAPI: + from .logger import log + + log.info("Starting Librarian v2.0 server.") + log.debug("Creating FastAPI app instance.") + + app = FastAPI() -app = FastAPI() + log.debug("Adding API router.") -log.debug("Adding API router.") + from .api import upload_router, ping_router, clone_router -from .api import upload_router, ping_router, clone_router + app.include_router(upload_router) + app.include_router(ping_router) + app.include_router(clone_router) -app.include_router(upload_router) -app.include_router(ping_router) -app.include_router(clone_router) \ No newline at end of file + return app \ No newline at end of file diff --git a/librarian_server_scripts/librarian_server_start.py b/librarian_server_scripts/librarian_server_start.py index bc19e06..ae2bf50 100755 --- a/librarian_server_scripts/librarian_server_start.py +++ b/librarian_server_scripts/librarian_server_start.py @@ -75,7 +75,7 @@ def main(): import uvicorn uvicorn.run( - "librarian_server:app", + "librarian_server:main", port=server_settings.port, log_level=server_settings.log_level.lower(), ) diff --git a/tests/conftest.py b/tests/conftest.py index ff7ce16..3bb8311 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -76,11 +76,10 @@ def test_server(tmp_path_factory): import importlib import librarian_server + import librarian_server.database - importlib.reload(librarian_server) - - app = librarian_server.app - session = librarian_server.session + app = librarian_server.main() + session = librarian_server.database.session # Need to add our stores... from librarian_server.orm import StoreMetadata