You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'Learn how to use existing Robyn plugins and create your own to extend the framework.'
3
+
1
4
## Plugins
2
5
3
-
Robyn is a versatile and extensible web framework that allows anyone to make plugins over the top of Robyn.
4
-
Plugins in Robyn allow you to enhance and customize the framework's functionality to suit your specific needs. Here are some noteworthy plugins that can supercharge your Robyn-based projects:
6
+
Robyn is a versatile and extensible web framework that allows anyone to build plugins on top of Robyn.
7
+
Plugins in Robyn are standard Python packages that use Robyn's public APIs — there is no special plugin runtime or registry. You build them using the same tools you use to build Robyn applications: routes, middleware, SubRouters, dependency injection, and lifecycle events.
8
+
9
+
## Community Plugins
5
10
6
11
### Rate Limit Plugin
7
12
8
-
- Description: This plugin enables you to implement rate limiting for your Robyn application's routes. It helps prevent abuse, and brute-force attacks and ensures fair usage of your resources.
In this example, robyn-rate-limits is used to enforce a rate limit of 3 requests per 100-seconds window for specific routes. If a client exceeds this limit, they will receive a "Too many requests" message.
36
+
---
34
37
35
-
The plugin integrates seamlessly with the Robyn web framework, enhancing the security and stability of your application by preventing excessive requests from a single client.
38
+
## Creating Your Own Plugin
36
39
37
-
## What's next?
40
+
A Robyn plugin is a regular Python package that depends on `robyn` and uses its public API. There is no special base class or registration mechanism — any pattern that works in a Robyn app works in a plugin.
41
+
42
+
### Plugin Patterns
43
+
44
+
There are several common patterns for building plugins:
45
+
46
+
#### 1. Middleware-Based Plugins
47
+
48
+
Middleware plugins hook into `before_request` or `after_request` to process every request. This is ideal for cross-cutting concerns like logging, authentication, or rate limiting.
49
+
50
+
```py
51
+
from robyn import Request, Response, Headers
52
+
53
+
classRequestLogger:
54
+
"""A plugin that logs all incoming requests."""
55
+
56
+
def__init__(self, log_headers: bool=False):
57
+
self.log_headers = log_headers
58
+
59
+
defhandle(self, request: Request):
60
+
print(f"[{request.method}] {request.url.path}")
61
+
ifself.log_headers:
62
+
print(f" Headers: {request.headers}")
63
+
64
+
# Usage in an app:
65
+
# logger = RequestLogger(log_headers=True)
66
+
# @app.before_request()
67
+
# def log_request(request: Request):
68
+
# logger.handle(request)
69
+
```
70
+
71
+
#### 2. SubRouter-Based Plugins
72
+
73
+
SubRouter plugins bundle a set of routes that can be included in any Robyn app via `app.include_router()`. This is ideal for reusable API modules like health checks, admin panels, or metrics endpoints.
0 commit comments