Skip to content

Reference

dsg_lib.robyn_functions.default_endpoints

Default endpoints for a Robyn application, mirroring dsg_lib.fastapi_functions.default_endpoints.

Example:

from robyn import Robyn
from dsg_lib.robyn_functions import default_endpoints

app = Robyn(__file__)

config = [
    {"bot": "GPTBot", "allow": False},
    {"bot": "Googlebot", "allow": True},
]
app.include_router(default_endpoints.create_default_router(config))
# GET /robots.txt -> text/plain robots.txt built from config

Author: Mike Ryan License: MIT

create_default_router(config, prefix='', tags=None)

Creates a Robyn SubRouter with default endpoints, including a configurable robots.txt.

Unlike FastAPI's app.include_router(router, prefix=...), Robyn's include_router takes no prefix, so the prefix and OpenAPI tags are set here instead.

Parameters:

Name Type Description Default
config List[Dict[str, Union[str, bool]]]

A list of dictionaries specifying which bots are allowed or disallowed, e.g. {"bot": "GPTBot", "allow": False}.

required
prefix str

Path prefix for the routes. Defaults to "" (root).

''
tags Optional[List[str]]

OpenAPI tags applied to every route.

None

Returns:

Name Type Description
SubRouter SubRouter

A Robyn SubRouter with the default endpoints. Register it

SubRouter

with app.include_router(router).

Source code in dsg_lib/robyn_functions/default_endpoints.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def create_default_router(
    config: List[Dict[str, Union[str, bool]]],
    prefix: str = "",
    tags: Optional[List[str]] = None,
) -> SubRouter:
    """
    Creates a Robyn SubRouter with default endpoints, including a configurable
    robots.txt.

    Unlike FastAPI's `app.include_router(router, prefix=...)`, Robyn's
    `include_router` takes no prefix, so the prefix and OpenAPI tags are set
    here instead.

    Args:
        config (List[Dict[str, Union[str, bool]]]): A list of dictionaries
            specifying which bots are allowed or disallowed, e.g.
            `{"bot": "GPTBot", "allow": False}`.
        prefix (str): Path prefix for the routes. Defaults to `""` (root).
        tags (Optional[List[str]]): OpenAPI tags applied to every route.

    Returns:
        SubRouter: A Robyn SubRouter with the default endpoints. Register it
        with `app.include_router(router)`.
    """
    router = SubRouter(prefix=prefix, tags=tags)

    @router.get("/robots.txt")
    def robots_txt(request):
        """
        Generates a robots.txt file based on the provided configuration.

        Returns:
            Response: The robots.txt content as `text/plain`.
        """
        return Response(
            status_code=200,
            headers={"Content-Type": "text/plain"},
            description=build_robots_txt(config),
        )

    return router