浏览代码

ability to set storage_secret for run_with

awfulness 1 年之前
父节点
当前提交
1c0eab7886
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      nicegui/run_with.py

+ 6 - 0
nicegui/run_with.py

@@ -1,10 +1,12 @@
 from typing import Optional
 
 from fastapi import FastAPI
+from starlette.middleware.sessions import SessionMiddleware
 
 from nicegui import globals
 from nicegui.language import Language
 from nicegui.nicegui import handle_shutdown, handle_startup
+from nicegui.storage import RequestTrackingMiddleware
 
 
 def run_with(
@@ -17,6 +19,7 @@ def run_with(
     binding_refresh_interval: float = 0.1,
     exclude: str = '',
     mount_path: str = '/',
+    storage_secret: Optional[str] = None,
 ) -> None:
     globals.ui_run_has_been_called = True
     globals.title = title
@@ -28,6 +31,9 @@ def run_with(
     globals.excludes = [e.strip() for e in exclude.split(',')]
     globals.tailwind = True
 
+    app.add_middleware(RequestTrackingMiddleware)
+    app.add_middleware(SessionMiddleware, secret_key=storage_secret)
+
     app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))
     app.on_event('shutdown')(lambda: handle_shutdown())