shelf

Pubbuild 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 intoother Angel/shelf applications. This way, you can migrate legacy applications withouthaving 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.

  1. import 'dart:io';
  2. import 'package:angel_framework/angel_framework.dart';
  3. import 'package:angel_shelf/angel_shelf.dart';
  4. import 'package:shelf/shelf.dart' as shelf;
  5. import 'api/api.dart';
  6.  
  7. main() async {
  8. var app = Angel();
  9. var http = AngelHttp(app);
  10.  
  11. // Angel routes on top
  12. await app.mountController<ApiController>();
  13.  
  14. // Re-route all other traffic to an
  15. // existing application.
  16. app.fallback(embedShelf(
  17. shelf.Pipeline()
  18. .addMiddleware(shelf.logRequests())
  19. .addHandler(_echoRequest)
  20. ));
  21.  
  22. // Or, only on a specific route:
  23. app.get('/shelf', wrappedShelfHandler);
  24.  
  25. await http.startServer(InternetAddress.loopbackIPV4, 3000);
  26. print(http.uri);
  27. }

Communicating with Angel with embedShelf

You can communicate with Angel:

  1. handleRequest(shelf.Request request) {
  2. // Access original Angel request...
  3. var req = request.context['angel_shelf.request'] as RequestContext;
  4.  
  5. // ... And then interact with it.
  6. req.container.registerNamedSingleton<Foo>('from_shelf', Foo());
  7.  
  8. // `req.container` is also available.
  9. var container = request.context['angel_shelf.container'] as Container;
  10. container.make<Truck>().drive();
  11. }

AngelShelf

Angel 2 brought about the generic Driver class, which is implementedby AngelHttp, AngelHttp2, AngelGopher, etc., and provides the coreinfrastructure for request handling in Angel.

AngelShelf is an implementation that wraps shelf requests and responses in theirAngel equivalents. Using it is as simple using as using AngelHttp, or any otherdriver:

  1. // Create an AngelShelf driver.
  2. // If we have startup hooks we want to run, we need to call
  3. // `startServer`. Otherwise, it can be omitted.
  4. // Of course, if you call `startServer`, know that to run
  5. // shutdown/cleanup logic, you need to call `close` eventually,
  6. // too.
  7. var angelShelf = AngelShelf(app);
  8. await angelShelf.startServer();
  9.  
  10. await shelf_io.serve(angelShelf.handler, InternetAddress.loopbackIPv4, 8081);

You can also use the AngelShelf driver as a shelf middleware - just useangelShelf.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 werematched), the next shelf handler will be called.

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