refactor setup_webapp so that it only does webapp setup stuff
Gitea/photolib/pipeline/head This commit looks good Details

This commit is contained in:
dave 2023-01-30 21:58:54 -08:00
parent cb328659cc
commit d8cac3f641
2 changed files with 26 additions and 21 deletions

View File

@ -621,34 +621,30 @@ class SearchView(object):
def setup_webapp(
database_url,
library_url,
cache_url,
database_engine,
library_client,
cache_client,
thumb_service_url,
debug=False,
db_debug=False,
max_upload=1024**3
):
# Get database connection
engine = get_db_engine(database_url, debug=db_debug)
# Setup database in web framework
cherrypy.tools.db = SATool()
SAEnginePlugin(cherrypy.engine, engine).subscribe()
SAEnginePlugin(cherrypy.engine, database_engine).subscribe()
cherrypy.tools.user = UserTool()
# Create various internal tools
library_storage = uri_to_storage(library_url)
library_manager = LibraryManager(library_storage)
thumbnail_tool = ThumbGenerator(library_manager, uri_to_storage(cache_url), thumb_service_url)
jobs_client = JobsClient(engine)
JobSubscriber(jobs_client)
#TODO move thumbtool outside of setup_webapp and use the cherrypy bus to connect with it
thumbnail_tool = ThumbGenerator(library_client, cache_client, thumb_service_url)
# Setup and mount web ui
tpl_dir = os.path.join(APPROOT, "templates") if not debug else "templates"
web = PhotosWeb(library_manager, thumbnail_tool, tpl_dir)
web = PhotosWeb(library_client, thumbnail_tool, tpl_dir)
cherrypy.tree.mount(web, '/', {'/': {'tools.trailing_slash.on': False,
'tools.db.on': True,
'tools.user.on': True,
'error_page.403': web.error,
'error_page.404': web.error},
'/static': {"tools.staticdir.on": True,
@ -661,7 +657,7 @@ def setup_webapp(
'tools.auth_basic.checkpassword': validate_password}})
# Setup and mount API
api = PhotosApi(library_manager)
api = PhotosApi(library_client)
cherrypy.tree.mount(api, '/api', {'/': {'tools.sessions.on': False,
'tools.trailing_slash.on': False,
'tools.auth_basic.on': True,
@ -718,10 +714,16 @@ def main():
if not args.thumb_service:
logging.warning("THUMB_SERVICE_URL not set. Video thumbnails will be unavailable")
# Get database connections
library_storage = uri_to_storage(args.library)
library_manager = LibraryManager(library_storage)
cache_manager = uri_to_storage(args.cache)
# set up webapp
setup_webapp(
args.database,
args.library,
args.cache,
get_db_engine(args.database, debug=args.db_debug),
library_manager,
cache_manager,
args.thumb_service,
debug=args.debug,
db_debug=args.db_debug,

View File

@ -6,6 +6,9 @@ import cherrypy
from cherrypy.test import helper
from photoapp.daemon import setup_webapp
from photoapp.dbutils import get_db_engine
from photoapp.api import LibraryManager
from photoapp.storage import uri_to_storage
class MockAuth:
@ -24,9 +27,9 @@ class PhotolibTest(helper.CPWebCase):
os.mkdir(cached)
setup_webapp(
"sqlite:///{}".format(os.path.join(cls.tmpd.name, "testing.db")),
"file://{}".format(libd),
"file://{}".format(cached),
get_db_engine("sqlite:///{}".format(os.path.join(cls.tmpd.name, "testing.db"))),
LibraryManager(uri_to_storage("file://{}".format(libd))),
uri_to_storage("file://{}".format(cached)),
None,
debug=True,
)