|
1 | 1 | export const description = |
2 | 2 | 'Welcome to the Robyn API documentation. You will find comprehensive guides and documentation to help you start working with Robyn as quickly as possible, as well as support if you get stuck.' |
3 | 3 |
|
| 4 | +## Code Organization with SubRouters |
4 | 5 |
|
5 | | -## SubRouter and Views |
| 6 | +As the application grew, Batman needed a way to organize his routes better. He decided to use Robyn's SubRouter feature to group related routes together. |
6 | 7 |
|
7 | | -After implementing the application application, Batman wanted to split the codebase into multiple files. |
8 | | - |
9 | | -This is when Robyn introduced him to the concept of routers and views. |
10 | | - |
11 | | -### Routers |
12 | | - |
13 | | -Routers are a way to split your application into multiple files. They allow you to group related endpoints together and make it easier to maintain your codebase. |
14 | | - |
15 | | -For example, if you wanted to create a router for the frontend, you would create a file called `frontend.py`. This file would contain all the endpoints related to the frontend. |
16 | | - |
17 | | -So the folder structure would look like this: |
18 | | - |
19 | | -```bash |
20 | | -├── app.py |
21 | | -├── frontend.py |
22 | | -├── Dockerfile |
23 | | -└── requirements.txt |
24 | | -``` |
25 | | - |
26 | | -And the code would look like this: |
27 | | - |
28 | | -```python {{ title: 'Creating a Router' }} |
29 | | -# frontend.py |
30 | | - |
31 | | -from robyn.templating import JinjaTemplate |
| 8 | +```python |
32 | 9 | from robyn import SubRouter |
33 | | -import os |
34 | | -import pathlib |
35 | | - |
36 | 10 |
|
37 | | -current_file_path = pathlib.Path(__file__).parent.resolve() |
38 | | -jinja_template = JinjaTemplate(os.path.join(current_file_path, "templates")) |
| 11 | +# Create a subrouter for crime-related routes |
| 12 | +crime_router = SubRouter(__file__, prefix="/crimes") |
39 | 13 |
|
| 14 | +@crime_router.get("/list") |
| 15 | +def list_crimes(): |
| 16 | + return {"crimes": get_all_crimes()} |
40 | 17 |
|
41 | | -frontend = SubRouter(__name__, prefix="/frontend") |
| 18 | +@crime_router.post("/report") |
| 19 | +def report_crime(request): |
| 20 | + crime_data = request.json() |
| 21 | + return {"id": create_crime_report(crime_data)} |
42 | 22 |
|
43 | | -@frontend.get("/") |
44 | | -async def get_frontend(request): |
45 | | - context = {"framework": "Robyn", "templating_engine": "Jinja2"} |
46 | | - return jinja_template.render_template("index.html", **context) |
47 | | -``` |
| 23 | +# Create a subrouter for suspect-related routes |
| 24 | +suspect_router = SubRouter(__file__, prefix="/suspects") |
48 | 25 |
|
49 | | -```python {{ title: 'Including a Router' }} |
50 | | -# app.py |
| 26 | +@suspect_router.get("/list") |
| 27 | +def list_suspects(): |
| 28 | + return {"suspects": get_all_suspects()} |
51 | 29 |
|
52 | | -from .frontend import frontend |
| 30 | +@suspect_router.get("/:id") |
| 31 | +def get_suspect(request, path_params): |
| 32 | + suspect_id = path_params.id |
| 33 | + return {"suspect": get_suspect_by_id(suspect_id)} |
53 | 34 |
|
54 | | - |
55 | | -app.include_router(frontend) |
| 35 | +# Include the subrouters in the main app |
| 36 | +app.include_router(crime_router) |
| 37 | +app.include_router(suspect_router) |
56 | 38 | ``` |
57 | 39 |
|
| 40 | +SubRouters help organize related routes under a common prefix, making the code more maintainable and easier to understand. In this example: |
58 | 41 |
|
59 | | -### Views |
60 | | - |
61 | | -Views are a way to split your application into multiple files. They allow you to group related endpoints together and make it easier to maintain your codebase. |
62 | | - |
63 | | -For example, if you wanted to create a view for the frontend, you would create a file called `frontend.py`. This file would contain all the endpoints related to the frontend. |
64 | | - |
65 | | - |
66 | | -The code would look like this: |
67 | | - |
68 | | -<CodeGroup> |
69 | | - ```python {{ title: 'Creating a decorator View' }} |
70 | | - from robyn import SyncView |
71 | | - |
72 | | - @app.view("/sync/view/decorator") |
73 | | - def sync_decorator_view(): |
74 | | - def get(): |
75 | | - return "Hello, world!" |
76 | | - |
77 | | - def post(request: Request): |
78 | | - body = request.body |
79 | | - return body |
80 | | - ``` |
81 | | - |
82 | | - |
83 | | - ```python {{ title: 'Creating a View' }} |
84 | | - def sync_decorator_view(): |
85 | | - def get(): |
86 | | - return "Hello, world!" |
87 | | - |
88 | | - def post(request: Request): |
89 | | - body = request.body |
90 | | - return body |
| 42 | +- All crime-related routes are under `/crimes` |
| 43 | +- All suspect-related routes are under `/suspects` |
91 | 44 |
|
92 | | - app.add_view("/sync/view/decorator", sync_decorator_view) |
93 | | - ``` |
94 | | -</CodeGroup> |
| 45 | +This organization makes it clear which routes handle what functionality and keeps related code together. |
95 | 46 |
|
96 | 47 |
|
97 | 48 |
|
|
0 commit comments