Spaces:
Running
Running
| from __future__ import annotations | |
| import environment # isort: skip | |
| import logging | |
| from pathlib import Path | |
| import uvicorn | |
| from fastapi import FastAPI | |
| from nicegui import Client, app, ui | |
| from velai import static | |
| from velai.app_context import get_or_create_app_context | |
| from velai.storage import storage_endpoint | |
| from velai.velai_app import VelaiApp | |
| async def create_app(client: Client) -> VelaiApp: | |
| ui.page_title("VELAI") | |
| # show spinner on load | |
| with ui.column().classes("w-full items-center") as info: | |
| ui.label("VELAI").classes("text-2xl font-bold") | |
| ui.label("Connecting...") | |
| ui.spinner() | |
| # wait until client has connected | |
| await client.connected() | |
| info.visible = False | |
| # create app context | |
| context = await get_or_create_app_context(client) | |
| # create app | |
| app_ui = VelaiApp(context) | |
| return app_ui | |
| async def main_page(client: Client) -> None: | |
| velai_app = await create_app(client) | |
| await velai_app.run_main_page() | |
| async def admin_page(client: Client) -> None: | |
| velai_app = await create_app(client) | |
| await velai_app.run_admin_page() | |
| storage_endpoint.register() | |
| def setup_logging(): | |
| # configure uvicorn logger | |
| uvicorn_error = logging.getLogger("uvicorn.error") | |
| uvicorn_error.disabled = False | |
| uvicorn_access = logging.getLogger("uvicorn.access") | |
| uvicorn_access.disabled = True | |
| loglevel = environment.VELAI_LOG_LEVEL | |
| logging.basicConfig( | |
| level=loglevel, | |
| format="%(levelname)s - %(asctime)s.%(msecs)03d - %(module)s - %(message)s", | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| force=True, | |
| ) | |
| # mount static folder | |
| static_path = Path(static.__file__).parent.absolute() | |
| app.add_static_files(static.STATIC_PATH_NAME, static_path) | |
| fastapi_app = FastAPI() | |
| # Setup fastapi | |
| ui.run_with( | |
| fastapi_app, | |
| storage_secret=environment.VELAI_STORAGE_SECRET, | |
| favicon=static_path / "images/favicon.ico", | |
| dark=True, | |
| title="VELAI", | |
| ) | |
| if __name__ in {"__main__", "__mp_main__"}: | |
| uvicorn.run( | |
| "main:fastapi_app", | |
| host=environment.VELAI_HOST, | |
| port=environment.VELAI_PORT, | |
| proxy_headers=True, | |
| forwarded_allow_ips="*", | |
| ) | |