Skip to content

Commit

Permalink
Register middlewares as late as possible (#4155)
Browse files Browse the repository at this point in the history
This PR moves the middleware registration from import time to the actual
call of `ui.run`. Thereby the middlewares are also run before
middlewares which are added by user code. See the the authentication
example for an actual use-case where this is convinient:


https://github.com/zauberzeug/nicegui/blob/09f4c34049b37907174bfe3109aae4b834f80863/examples/authentication/main.py#L28C2-L33C40

Without this change, the `RedirectResponse` does not work properly
behind a reverse proxy with subpath or NiceGUI On Air, because the
rewriting of the path prefix which is done by our
`RedirectWithPrefixMiddleware`. FastAPI / Starlette call the middlewares
in reverse order of registration. Therefore, without this PR, the user
middleware is the last one added and hence called first, leaving the
path unmodified.

ToDos:

- [x] test with simple NiceGUI App which has no `if __name__ in
('__mp_main__', '__main__')` guard
- [x] implement and test `ui.run_with`

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
  • Loading branch information
rodja and falkoschindler authored Jan 13, 2025
1 parent e6fa211 commit 6140d10
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 0 additions & 4 deletions nicegui/nicegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import socketio
from fastapi import HTTPException, Request
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import FileResponse, Response

from . import air, background_tasks, binding, core, favicon, helpers, json, run, welcome
Expand All @@ -17,7 +16,6 @@
from .error import error_content
from .json import NiceGUIJSONResponse
from .logging import log
from .middlewares import RedirectWithPrefixMiddleware
from .page import page
from .slot import Slot
from .staticfiles import CacheControlledStaticFiles
Expand Down Expand Up @@ -54,8 +52,6 @@ async def __call__(self, scope, receive, send):
mimetypes.add_type('text/javascript', '.js')
mimetypes.add_type('text/css', '.css')

app.add_middleware(GZipMiddleware)
app.add_middleware(RedirectWithPrefixMiddleware)
static_files = CacheControlledStaticFiles(
directory=(Path(__file__).parent / 'static').resolve(),
follow_symlink=True,
Expand Down
5 changes: 5 additions & 0 deletions nicegui/ui_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Any, List, Literal, Optional, Tuple, TypedDict, Union

from fastapi.middleware.gzip import GZipMiddleware
from starlette.routing import Route
from uvicorn.main import STARTUP_FAILURE
from uvicorn.supervisors import ChangeReload, Multiprocess
Expand All @@ -16,6 +17,7 @@
from .client import Client
from .language import Language
from .logging import log
from .middlewares import RedirectWithPrefixMiddleware
from .server import CustomServerConfig, Server

APP_IMPORT_STRING = 'nicegui:app'
Expand Down Expand Up @@ -122,6 +124,9 @@ def run(*,
show_welcome_message=show_welcome_message,
)
core.app.config.endpoint_documentation = endpoint_documentation
if not helpers.is_pytest():
core.app.add_middleware(GZipMiddleware)
core.app.add_middleware(RedirectWithPrefixMiddleware)

for route in core.app.routes:
if not isinstance(route, Route):
Expand Down
5 changes: 4 additions & 1 deletion nicegui/ui_run_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from typing import Literal, Optional, Union

from fastapi import FastAPI
from fastapi.middleware.gzip import GZipMiddleware

from . import core, storage
from .air import Air
from .language import Language
from .middlewares import RedirectWithPrefixMiddleware
from .nicegui import _shutdown, _startup


Expand Down Expand Up @@ -59,8 +61,9 @@ def run_with(
prod_js=prod_js,
show_welcome_message=show_welcome_message,
)

storage.set_storage_secret(storage_secret)
core.app.add_middleware(GZipMiddleware)
core.app.add_middleware(RedirectWithPrefixMiddleware)

if on_air:
core.air = Air('' if on_air is True else on_air)
Expand Down

0 comments on commit 6140d10

Please sign in to comment.