Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato committed Jan 17, 2024
1 parent 52ace47 commit f034f6a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.6] - 2024-01-16 :kr: :heart:
## [2.0.6] - 2024-01-17 :kr: :heart:

- Adds built-in support for [Server-Sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).
- Adds a function to detect when the server process is terminating because it
Expand All @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Adds support for request handler normalization for methods defined as
asynchronous generators. This feature is enabled by default only for
ServerSentEvents, but can be configured for user defined types.
- Raises exception when the default router is used to register routes, but not
associated to an application object. Fixes [#470](https://github.com/Neoteroi/BlackSheep/issues/470).

Refer to the [BlackSheep documentation](https://www.neoteroi.dev/blacksheep/server-sent-events/)
and to the [examples repository](https://github.com/Neoteroi/BlackSheep-Examples/tree/main/server-sent-events) for more information on server-sent events support.
Expand Down
3 changes: 2 additions & 1 deletion blacksheep/server/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
RoutesRegistry,
)
from blacksheep.server.routing import router as default_router
from blacksheep.server.routing import validate_router
from blacksheep.server.routing import validate_default_router, validate_router
from blacksheep.server.websocket import WebSocket, format_reason
from blacksheep.sessions import SessionMiddleware, SessionSerializer
from blacksheep.settings.di import di_settings
Expand Down Expand Up @@ -706,6 +706,7 @@ async def start(self):
if self.on_start:
await self.on_start.fire()

validate_default_router()
self.use_controllers()
self.normalize_handlers()
self.configure_middlewares()
Expand Down
33 changes: 32 additions & 1 deletion blacksheep/server/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class InvalidRouterConfigurationError(RouteException):
"""Base class for router configuration errors"""


class OrphanDefaultRouterError(InvalidRouterConfigurationError):
"""
Error raised when the default router was configured with routes, but it is not
associated to any application.
"""

def __init__(self) -> None:
super().__init__(
"Invalid router configuration: the default router was used to register "
"routes, but it is not associated to any application object. To resolve, "
"ensure that the router bound to your application is used to register "
"routes. Do not use routing methods imported from the library."
)


class SharedRouterError(InvalidRouterConfigurationError):
"""
Error raised when the more than one application is using the same router.
Expand Down Expand Up @@ -941,13 +956,29 @@ def validate_router(app):
# by uvicorn reload feature, when uvicorn is started programmatically.
# See https://github.com/Neoteroi/BlackSheep/issues/438
logging.getLogger("blacksheep.server").warning(
"[BlackSheep] The application was reloaded, resetting its router."
"The application was reloaded, resetting its router."
)
app_router.reset()
return
raise SharedRouterError()


def validate_default_router():
"""
This method ensures that the default router is associated to an application, if it
defines any route.
"""
if set(router):
# The default router has routes defined, ensure that it is bound to an
# application
# verify that
try:
_apps_by_router_id[id(router)]
except KeyError:
# Not good
raise OrphanDefaultRouterError() from None


# Singleton router used to store initial configuration,
# before the application starts
# this is used as *default* router, but it can be overridden;
Expand Down

0 comments on commit f034f6a

Please sign in to comment.