Skip to content

router parameter in Application constructor #464

Answered by RobertoPrevato
netwang asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @netwang

The below works fine:

import os
from blacksheep import Application, Router


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

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

        self.__prefix = prefix

    def add(self, method: str, pattern, handler) -> 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)


prod = os.environ.get("PROD") == "1"


app = Application(router=RouterWithPrefix(f"/example/") if prod else None)


@app.

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@netwang
Comment options

@RobertoPrevato
Comment options

Answer selected by RobertoPrevato
Comment options

You must be logged in to vote
1 reply
@netwang
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #463 on January 09, 2024 18:47.