platform/packages/shelf
2021-06-20 20:37:20 +08:00
..
.idea Add 'packages/shelf/' from commit '1923132532665f8a26d9a12fc9db2f2073611e45' 2020-02-15 18:22:23 -05:00
example Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
lib Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
test Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
.gitignore Add 'packages/shelf/' from commit '1923132532665f8a26d9a12fc9db2f2073611e45' 2020-02-15 18:22:23 -05:00
.travis.yml Add 'packages/shelf/' from commit '1923132532665f8a26d9a12fc9db2f2073611e45' 2020-02-15 18:22:23 -05:00
analysis_options.yaml Add 'packages/shelf/' from commit '1923132532665f8a26d9a12fc9db2f2073611e45' 2020-02-15 18:22:23 -05:00
AUTHORS.md Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
CHANGELOG.md Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
LICENSE Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
pubspec.yaml Merged from sdk-2.12.x_nnbd 2021-06-20 20:37:20 +08:00
README.md Add 'packages/shelf/' from commit '1923132532665f8a26d9a12fc9db2f2073611e45' 2020-02-15 18:22:23 -05:00

shelf

Pub build status

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 Angel/shelf apps into other Angel/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.

Usage

embedShelf

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.

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 {
  var app = Angel();
  var http = AngelHttp(app);

  // Angel routes on top
  await app.mountController<ApiController>();

  // Re-route all other traffic to an
  // existing application.
  app.fallback(embedShelf(
    shelf.Pipeline()
      .addMiddleware(shelf.logRequests())
      .addHandler(_echoRequest)
  ));

  // Or, only on a specific route:
  app.get('/shelf', wrappedShelfHandler);

  await http.startServer(InternetAddress.loopbackIPV4, 3000);
  print(http.uri);
}

Communicating with Angel with embedShelf

You can communicate with Angel:

handleRequest(shelf.Request request) {
  // Access original Angel request...
  var req = request.context['angel_shelf.request'] as RequestContext;

  // ... And then interact with it.
  req.container.registerNamedSingleton<Foo>('from_shelf', Foo());

  // `req.container` is also available.
  var container = request.context['angel_shelf.container'] as Container;
  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:

// 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.

var handler = shelf.Pipeline()
  .addMiddleware(angelShelf.middleware)
  .addHandler(createStaticHandler(...));