Skip to content

router parameter in Application constructor #463

@netwang

Description

@netwang

Application(
services=services,
show_error_details=configuration.show_error_details,
router=RouterWithPrefix(f'/{configuration.App_Name}/') if configuration.production else None,
)

class RouterWithPrefix(Router):
"""Router With Prefix."""

   def __init__(self, prefix: str) -> None:
        super().__init__()

        self.__prefix = prefix

  def add(self, method: str, pattern: AnyStr, handler: Any) -> None:
        """Add."""
        if isinstance(pattern, str):
              pattern = (self.__prefix + pattern).replace('//', '/')
       else:
             pattern = (self.__prefix.encode() + pattern).replace(b'//', b'/')

      super().add(method, pattern, handler)

Above code is for BlackSheep V1.2.18 to add prefix to all routes,. It changes
http://localhost:1400/login
to
http://localhost:1400/QMOSDS/login

I updated to BlackSheep 2.0.4. Above cold will not work when configuration.production is True.

Above cold will work when configuration.production is False. Use default_router

if router is None:
    router = default_router if env_settings.use_default_router else Router()

How to add prefix to all routes in BlackSheep 2.0.4?? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions