2016-12-01 22:32:04 +00:00
|
|
|
# shelf
|
2017-06-12 23:53:08 +00:00
|
|
|
[![Pub](https://img.shields.io/pub/v/angel_shelf.svg)](https://pub.dartlang.org/packages/angel_shelf)
|
2017-01-13 15:33:35 +00:00
|
|
|
[![build status](https://travis-ci.org/angel-dart/shelf.svg)](https://travis-ci.org/angel-dart/shelf)
|
|
|
|
|
2017-06-12 23:53:08 +00:00
|
|
|
Shelf interop with Angel. This package lets you run `package:shelf` handlers via a custom adapter.
|
2016-12-01 22:40:23 +00:00
|
|
|
|
2017-06-12 23:53:08 +00:00
|
|
|
Use the code in this repo to embed existing shelf apps into
|
|
|
|
your Angel applications. This way, you can migrate legacy applications without
|
|
|
|
having to rewrite your business logic.
|
2016-12-01 22:40:23 +00:00
|
|
|
|
|
|
|
This will make it easy to layer your API over a production application,
|
|
|
|
rather than having to port code.
|
|
|
|
|
2018-11-11 17:35:37 +00:00
|
|
|
- [Usage](#usage)
|
|
|
|
- [embedShelf](#embedshelf)
|
|
|
|
- [Communicating with Angel](#communicating-with-angel-with-embedshelf)
|
2017-06-12 23:53:08 +00:00
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
|
|
|
## embedShelf
|
2018-11-11 17:35:37 +00:00
|
|
|
|
2017-06-12 23:53:08 +00:00
|
|
|
This is a compliant `shelf` adapter that acts as an Angel request handler. You can use it as a middleware,
|
|
|
|
or attach it to individual routes.
|
|
|
|
|
2016-12-01 22:40:23 +00:00
|
|
|
```dart
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_shelf/angel_shelf.dart';
|
|
|
|
import 'package:shelf/shelf.dart' as shelf;
|
|
|
|
import 'api/api.dart';
|
|
|
|
|
|
|
|
main() async {
|
2018-11-11 17:35:37 +00:00
|
|
|
var app = new Angel();
|
|
|
|
var http = new AngelHttp(app);
|
|
|
|
|
2016-12-01 22:40:23 +00:00
|
|
|
// Angel 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(
|
2017-01-13 15:33:35 +00:00
|
|
|
new 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
|
|
|
|
|
|
|
### Communicating with Angel with embedShelf
|
2018-11-11 17:35:37 +00:00
|
|
|
|
2017-06-12 23:53:08 +00:00
|
|
|
You can communicate with Angel:
|
|
|
|
|
|
|
|
```dart
|
|
|
|
handleRequest(shelf.Request request) {
|
|
|
|
// Access original Angel request...
|
|
|
|
var req = request.context['angel_shelf.request'] as RequestContext;
|
|
|
|
|
2018-11-11 17:35:37 +00:00
|
|
|
// ... And then interact with it.
|
|
|
|
req.container.registerNamedSingleton<Foo>('from_shelf', new 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
|
|
|
}
|
|
|
|
```
|