2024-10-12 10:35:14 +00:00
# Protevus Shelf
2017-01-13 15:33:35 +00:00
2024-10-13 01:45:27 +00:00
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/protevus_shelf?include_prereleases )
2021-09-14 01:23:03 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord ](https://img.shields.io/discord/1060322353214660698 )](https://discord.gg/3X6bxTUdCM)
2024-10-12 10:35:14 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/belatuk-common-utilities )](https://github.com/dart-backend/protevus/tree/master/packages/shelf/LICENSE)
2021-09-14 01:23:03 +00:00
2021-09-14 01:45:16 +00:00
**Replacement of `package:angel_shelf` with breaking changes to support NNBD.**
2024-10-12 10:39:20 +00:00
Shelf interop with Protevus. This package lets you run `package:shelf` handlers via a custom adapter. Use the code in this repo to embed existing Protevus/shelf apps into other Protevus/shelf applications. This way, you can migrate legacy applications without having to rewrite your business logic. This will make it easy to layer your API over a production application, rather than having to port code.
2016-12-01 22:40:23 +00:00
2024-10-12 10:41:18 +00:00
- [Protevus Shelf ](#protevus-shelf )
2021-09-14 01:23:03 +00:00
- [Usage ](#usage )
- [embedShelf ](#embedshelf )
2024-10-12 10:39:20 +00:00
- [Communicating with Protevus with embedShelf ](#communicating-with-protevus-with-embedshelf )
2024-10-13 02:17:24 +00:00
- [ProtevusShelf ](#angelshelf )
2017-06-12 23:53:08 +00:00
2021-09-14 01:23:03 +00:00
## Usage
2017-06-12 23:53:08 +00:00
2021-09-14 01:23:03 +00:00
### embedShelf
2018-11-11 17:35:37 +00:00
2024-10-12 10:39:20 +00:00
This is a compliant `shelf` adapter that acts as an Protevus request handler. You can use it as a middleware,
2017-06-12 23:53:08 +00:00
or attach it to individual routes.
2016-12-01 22:40:23 +00:00
```dart
import 'dart:io';
2024-10-13 01:45:27 +00:00
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_shelf/protevus_shelf.dart';
2016-12-01 22:40:23 +00:00
import 'package:shelf/shelf.dart' as shelf;
import 'api/api.dart';
2021-09-14 01:23:03 +00:00
void main() async {
2024-10-12 10:39:20 +00:00
var app = Protevus();
2024-10-13 02:17:24 +00:00
var http = ProtevusHttp(app);
2018-11-11 17:35:37 +00:00
2024-10-12 10:39:20 +00:00
// Protevus routes on top
2018-11-11 17:35:37 +00:00
await app.mountController< ApiController > ();
2016-12-01 22:40:23 +00:00
// Re-route all other traffic to an
2017-01-23 12:57:08 +00:00
// existing application.
2018-11-11 17:35:37 +00:00
app.fallback(embedShelf(
2019-10-14 15:28:50 +00:00
shelf.Pipeline()
2016-12-01 22:40:23 +00:00
.addMiddleware(shelf.logRequests())
.addHandler(_echoRequest)
));
2018-11-11 17:35:37 +00:00
// Or, only on a specific route:
app.get('/shelf', wrappedShelfHandler);
await http.startServer(InternetAddress.loopbackIPV4, 3000);
print(http.uri);
2016-12-01 22:40:23 +00:00
}
```
2017-06-12 23:53:08 +00:00
2024-10-12 10:39:20 +00:00
### Communicating with Protevus with embedShelf
2018-11-11 17:35:37 +00:00
2024-10-12 10:35:14 +00:00
You can communicate with Protevus:
2017-06-12 23:53:08 +00:00
```dart
handleRequest(shelf.Request request) {
2024-10-12 10:39:20 +00:00
// Access original Protevus request...
2017-06-12 23:53:08 +00:00
var req = request.context['angel_shelf.request'] as RequestContext;
2018-11-11 17:35:37 +00:00
// ... And then interact with it.
2019-10-14 15:28:50 +00:00
req.container.registerNamedSingleton< Foo > ('from_shelf', Foo());
2017-06-12 23:53:08 +00:00
2018-11-11 17:35:37 +00:00
// `req.container` is also available.
var container = request.context['angel_shelf.container'] as Container;
container.make< Truck > ().drive();
2017-06-12 23:53:08 +00:00
}
```
2019-10-14 16:45:37 +00:00
2024-10-13 02:17:24 +00:00
### ProtevusShelf
2019-10-14 16:45:37 +00:00
2024-10-13 02:17:24 +00:00
Protevus brought about the generic `Driver` class, which is implemented by `ProtevusHttp` , `ProtevusHttp2` , `ProtevusGopher` , etc., and provides the core infrastructure for request handling in Protevus. `ProtevusShelf` is an implementation that wraps shelf requests and responses in their Protevus equivalents. Using it is as simple using as using `ProtevusHttp` , or any other driver:
2019-10-14 16:45:37 +00:00
```dart
2024-10-13 02:17:24 +00:00
// Create an ProtevusShelf driver.
2019-10-14 16:45:37 +00:00
// If we have startup hooks we want to run, we need to call
// `startServer` . Otherwise, it can be omitted.
// Of course, if you call `startServer` , know that to run
// shutdown/cleanup logic, you need to call `close` eventually,
// too.
2024-10-13 02:17:24 +00:00
var angelShelf = ProtevusShelf(app);
2019-10-14 16:45:37 +00:00
await angelShelf.startServer();
await shelf_io.serve(angelShelf.handler, InternetAddress.loopbackIPv4, 8081);
```
2024-10-13 02:17:24 +00:00
You can also use the `ProtevusShelf` driver as a shelf middleware - just use
2019-10-14 16:45:37 +00:00
`angelShelf.middleware` instead of `angelShelf.handler` . When used as a middleware,
2024-10-12 10:39:20 +00:00
if the Protevus response context is still open after all handlers run (i.e. no routes were
2019-10-14 16:45:37 +00:00
matched), the next shelf handler will be called.
```dart
var handler = shelf.Pipeline()
.addMiddleware(angelShelf.middleware)
.addHandler(createStaticHandler(...));
2021-09-14 01:23:03 +00:00
```