Skip to content

Commit

Permalink
refactor: Group state setup in startup event
Browse files Browse the repository at this point in the history
  • Loading branch information
jamilraichouni committed Jan 29, 2025
1 parent f124761 commit 8ecebe7
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions capella_model_explorer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def _reset_state() -> None:
state.template = None


def _app_startup() -> None:
try:
MODEL = os.environ["CME_MODEL"]
except KeyError as err:
raise SystemExit("CME_MODEL environment variable is not set") from err

try:
_1 = state.model
except AttributeError:
state.model = capellambse.loadcli(MODEL)
templates.load()
state.jinja_env.finalize = _finalize
state.jinja_env.filters["make_href"] = _make_href_filter


app = (
fh.FastHTMLWithLiveReload(**c.FASTHTML_APP_CONFIG)
if c.LIVE_MODE
Expand All @@ -86,20 +101,6 @@ def _reset_state() -> None:
app.static_route_exts(f"{c.ROUTE_PREFIX}/static", "./static")


try:
MODEL = os.environ["CME_MODEL"]
except KeyError as err:
raise SystemExit("CME_MODEL environment variable is not set") from err

try:
_1 = state.model
except AttributeError:
state.model = capellambse.loadcli(MODEL)

state.jinja_env.finalize = _finalize
state.jinja_env.filters["make_href"] = _make_href_filter


@app.middleware("http")
async def update_last_interaction_time(request: fh.Request, call_next):
if request.url.path not in ("/metrics", "/favicon.ico"):
Expand Down Expand Up @@ -327,5 +328,5 @@ def render_template(template_id: str, model_element_uuid: str = "") -> t.Any:
)


app.on_event("startup")(lambda: templates.load())
app.on_event("startup")(lambda: _app_startup())
ar.to_app(app)

0 comments on commit 8ecebe7

Please sign in to comment.