-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from francescovallone/feat/18
Feat/18
- Loading branch information
Showing
43 changed files
with
452 additions
and
731 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Hooks | ||
|
||
Hooks are a way to execute custom code at specific points in the Serinus request lifecycle. They can be used to add custom logic to your application, such as tracing, loggin, or error handling. | ||
|
||
## Creating a Hook | ||
|
||
To create a hook, you need to create a class that extends the `Hook` class. Each hook has multiple methods that you can override to add custom logic. | ||
|
||
These are the methods that you can override: | ||
|
||
- `onRequest`: This method is called before the router is called. | ||
- `beforeHandle`: This method is called before the route handler is called. | ||
- `afterHandle`: This method is called after the route handler is called. | ||
- `onResponse`: This method is called after the response is generated. | ||
|
||
```dart | ||
import 'package:serinus/serinus.dart'; | ||
class MyHook extends Hook { | ||
@override | ||
Future<void> onRequest(Request request, InternalResponse response) async { | ||
print('Request received'); | ||
} | ||
@override | ||
Future<void> beforeHandle(Request request, InternalResponse response) async { | ||
print('Before handle'); | ||
} | ||
@override | ||
Future<void> afterHandle(Request request, InternalResponse response) async { | ||
print('After handle'); | ||
} | ||
@override | ||
Future<void> onResponse(Response response) async { | ||
print('Response sent'); | ||
} | ||
} | ||
``` | ||
|
||
In the `MyHook` class, you can override the methods that you want to add custom logic to. You can access the `Request` object and the `InternalResponse` object in the `onRequest`, `beforeHandle`, and `afterHandle` methods, and the `Response` object in the `onResponse` method. | ||
|
||
The `onResponse` can also know if the response was successful or not by checking the `response.isError` property. | ||
This property will be `true` if the response status code is greater than or equal to 400. | ||
|
||
## Adding a Hook | ||
|
||
To add a hook to your application, you can use the `use` method on the `SerinusApplication` object. | ||
|
||
```dart | ||
import 'package:serinus/serinus.dart'; | ||
void main() { | ||
final app = SerinusApplication(); | ||
app.use(MyHook()); | ||
await app.serve(); | ||
} | ||
``` | ||
|
||
In the example above, the `MyHook` hook is added to the application using the `use` method. This will execute the hook methods at the specified points in the request lifecycle. | ||
|
||
Hooks are executed in the order that they are added to the application. If you need to execute a hook before another hook, you can add it before the other hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
| Server | Req/sec | Trans/sec | Req/sec DIFF | Avg Latency | | ||
|:-|:-|:-|:-|:-| | ||
| serinus | 24529.39 | 5.50MB | +0.00% | 45.08 | | ||
| dart_http | 32624.36 | 6.16MB | +33.00% | 31.33 | | ||
| serinus | 29499.5 | 72.85 KB/s | +0.00% | 3.389893 | | ||
| dart_http | 37617.8 | 95.35 KB/s | +27.52% | 2.658316 | |
Oops, something went wrong.