mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Pre Refactor of main loop
This commit is contained in:
45
pyShelf.py
vendored
45
pyShelf.py
vendored
@@ -6,14 +6,15 @@ from pathlib import Path
|
||||
from threading import Thread
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.routing import APIRoute
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from src.backend.lib.config import Config
|
||||
from src.backend.pyShelf_MakeCollections import MakeCollections
|
||||
from src.backend.pyShelf_ScanLibrary import execute_scan
|
||||
|
||||
# import websockets
|
||||
|
||||
|
||||
@@ -22,7 +23,8 @@ config = Config(root)
|
||||
PRG_PATH = Path.cwd().__str__()
|
||||
sys.path.insert(0, PRG_PATH)
|
||||
app = FastAPI()
|
||||
|
||||
app.mount("/static", StaticFiles(directory="src/frontend/static"), name="static")
|
||||
templates = Jinja2Templates(directory="src/frontend/templates")
|
||||
|
||||
def RunImport():
|
||||
"""Begin live import of books."""
|
||||
@@ -34,51 +36,54 @@ def RunImport():
|
||||
|
||||
|
||||
def use_route_names_as_operation_ids(app: FastAPI) -> None:
|
||||
"""Use route name as operation id."""
|
||||
for route in app.routes:
|
||||
if isinstance(route, APIRoute):
|
||||
route.operation_id = route.name
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index():
|
||||
return """
|
||||
<html>
|
||||
<head>
|
||||
<title>pyShelf eBook Server</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>pyShelf Open Source Content Server</h3>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
async def index(request: Request):
|
||||
"""Home page responder."""
|
||||
return templates.TemplateResponse(
|
||||
"index.html",
|
||||
{"request": request})
|
||||
|
||||
|
||||
@app.get("/users/me")
|
||||
async def about_me():
|
||||
"""About me page responder."""
|
||||
return {"user_id": "CurrentUser"}
|
||||
|
||||
|
||||
@app.get("/users/{user_id}")
|
||||
async def about_user(user_id: int):
|
||||
"""About user page responder."""
|
||||
return {"user_id": user_id}
|
||||
|
||||
|
||||
@app.get("/dev/test/echo/{_test_item_}")
|
||||
async def echo_test(_test_item_):
|
||||
"""Test echo responder function."""
|
||||
return {"Test Object": _test_item_}
|
||||
|
||||
|
||||
async def fe_server():
|
||||
"""Front end server entrypoint."""
|
||||
config.logger.info("Starting FastAPI server.")
|
||||
fe_config = uvicorn.Config("__main__:app", port=8080, log_level="info", reload=True)
|
||||
fe_config = uvicorn.Config("__main__:app", port=8080,
|
||||
log_level="info", reload=True)
|
||||
fe_server = uvicorn.Server(fe_config)
|
||||
await fe_server.serve()
|
||||
|
||||
|
||||
async def main():
|
||||
"""Program entrypoint."""
|
||||
_import_thread = Thread(target=RunImport)
|
||||
_import_thread.start()
|
||||
asyncio.create_task(fe_server())
|
||||
_task = await asyncio.create_task(fe_server())
|
||||
breakpoint()
|
||||
return [_task, _import_thread]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -87,8 +92,8 @@ if __name__ == "__main__":
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.create_task(main())
|
||||
loop.run_forever()
|
||||
_main_task = loop.create_task(main())
|
||||
loop.run_until_complete()
|
||||
loop.close()
|
||||
exit
|
||||
# asyncio.get_event_loop(asyncio.run(main())).run_forever()
|
||||
loop.shutdown_default_executor()
|
||||
exit(0)
|
||||
|
||||
Reference in New Issue
Block a user