AngelShelf docs

This commit is contained in:
Tobe O 2019-10-14 12:45:37 -04:00
parent 071129902a
commit 1c00345e4e
3 changed files with 46 additions and 3 deletions

View file

@ -4,8 +4,8 @@
Shelf interop with Angel. This package lets you run `package:shelf` handlers via a custom adapter. Shelf interop with Angel. This package lets you run `package:shelf` handlers via a custom adapter.
Use the code in this repo to embed existing shelf apps into Use the code in this repo to embed existing Angel/shelf apps into
your Angel applications. This way, you can migrate legacy applications without other Angel/shelf applications. This way, you can migrate legacy applications without
having to rewrite your business logic. having to rewrite your business logic.
This will make it easy to layer your API over a production application, This will make it easy to layer your API over a production application,
@ -14,6 +14,7 @@ rather than having to port code.
- [Usage](#usage) - [Usage](#usage)
- [embedShelf](#embedshelf) - [embedShelf](#embedshelf)
- [Communicating with Angel](#communicating-with-angel-with-embedshelf) - [Communicating with Angel](#communicating-with-angel-with-embedshelf)
- [`AngelShelf`](#angelshelf)
# Usage # Usage
@ -69,3 +70,36 @@ handleRequest(shelf.Request request) {
container.make<Truck>().drive(); container.make<Truck>().drive();
} }
``` ```
### AngelShelf
Angel 2 brought about the generic `Driver` class, which is implemented
by `AngelHttp`, `AngelHttp2`, `AngelGopher`, etc., and provides the core
infrastructure for request handling in Angel.
`AngelShelf` is an implementation that wraps shelf requests and responses in their
Angel equivalents. Using it is as simple using as using `AngelHttp`, or any other
driver:
```dart
// Create an AngelShelf driver.
// 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.
var angelShelf = AngelShelf(app);
await angelShelf.startServer();
await shelf_io.serve(angelShelf.handler, InternetAddress.loopbackIPv4, 8081);
```
You can also use the `AngelShelf` driver as a shelf middleware - just use
`angelShelf.middleware` instead of `angelShelf.handler`. When used as a middleware,
if the Angel response context is still open after all handlers run (i.e. no routes were
matched), the next shelf handler will be called.
```dart
var handler = shelf.Pipeline()
.addMiddleware(angelShelf.middleware)
.addHandler(createStaticHandler(...));
```

View file

@ -29,8 +29,12 @@ main() async {
})); }));
// Next, create an AngelShelf driver. // Next, create an AngelShelf driver.
//
// If we have startup hooks we want to run, we need to call // If we have startup hooks we want to run, we need to call
// `startServer`. Otherwise, it can be omitted. // `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.
var angelShelf = AngelShelf(app); var angelShelf = AngelShelf(app);
await angelShelf.startServer(); await angelShelf.startServer();

View file

@ -25,6 +25,11 @@ class AngelShelf extends Driver<shelf.Request, ShelfResponseContext,
}); });
} }
Future<Stream<shelf.Request>> close() {
incomingRequests.close();
return super.close();
}
Future<Stream<shelf.Request>> Function(dynamic, int) get serverGenerator => Future<Stream<shelf.Request>> Function(dynamic, int) get serverGenerator =>
(_, __) async => incomingRequests.stream; (_, __) async => incomingRequests.stream;