Skip to content

Commit

Permalink
expose router class (#127)
Browse files Browse the repository at this point in the history
* Expose router_class for customization
  • Loading branch information
devkral authored Jan 6, 2025
1 parent b41378f commit d18c9ce
Show file tree
Hide file tree
Showing 3 changed files with 618 additions and 609 deletions.
3 changes: 3 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ hide:
### Added

- Add `ReceiveSendSniffer`. This sniffer allows to detect communication events and to replay receive messages.
- `Include` and `BaseLilya` (application) have now a ClassVar `router_class` to provide a custom router.
- Subclasses of `BaseLilya` (application) can set the `router_class` to None to provide a completely custom router
which initialization parameters aren't required to match the ones of `Router`.
- Expose `fall_through` on `StaticFile`.

### Changed
Expand Down
26 changes: 15 additions & 11 deletions lilya/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from collections.abc import Awaitable, Callable, Mapping, Sequence
from functools import cached_property
from typing import Annotated, Any, cast
from typing import Annotated, Any, ClassVar, cast

from lilya._internal._module_loading import import_string
from lilya._utils import is_class_and_subclass
Expand Down Expand Up @@ -44,6 +44,9 @@


class BaseLilya:
router_class: ClassVar[type[Router] | None] = Router
router: Router

def __init__(
self,
debug: Annotated[bool, Doc("Enable or disable debug mode. Defaults to False.")] = False,
Expand Down Expand Up @@ -392,16 +395,17 @@ async def create_user(request: Request):
self.state = State()
self.middleware_stack: ASGIApp | None = None

self.router: Router = Router(
routes=routes,
redirect_slashes=redirect_slashes,
permissions=self.custom_permissions,
on_startup=on_startup,
on_shutdown=on_shutdown,
lifespan=lifespan,
include_in_schema=include_in_schema,
settings_module=self.settings,
)
if self.router_class is not None:
self.router = self.router_class(
routes=routes,
redirect_slashes=redirect_slashes,
permissions=self.custom_permissions,
on_startup=on_startup,
on_shutdown=on_shutdown,
lifespan=lifespan,
include_in_schema=include_in_schema,
settings_module=self.settings,
)
self.__set_settings_app(self.settings, self)

@property
Expand Down
Loading

0 comments on commit d18c9ce

Please sign in to comment.