angel_shelf 2.0.0

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 shelf apps into your Angel 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 = new Angel();
  var http = new AngelHttp(app);

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

  // Re-route all other traffic to an
  // existing application.
  app.fallback(embedShelf(
    new 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', new Foo());

  // `req.container` is also available.
  var container = request.context['angel_shelf.container'] as Container;
  container.make<Truck>().drive();
}

2.0.0 #

  • Removed supportShelf.

1.2.0 #

  • Upgraded for >=1.1.0 compatibility.

example/main.dart

import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_shelf/angel_shelf.dart';
import 'package:shelf_static/shelf_static.dart';

main() async {
  var app = new Angel();
  var http = new AngelHttp(app);

  // `shelf` request handler
  var shelfHandler = createStaticHandler('.',
      defaultDocument: 'index.html', listDirectories: true);

  // Use `embedShelf` to adapt a `shelf` handler for use within Angel.
  var wrappedHandler = embedShelf(shelfHandler);

  // A normal Angel route.
  app.get('/angel', (req, ResponseContext res) {
    res.write('Hooray for `package:angel_shelf`!');
    return false; // End execution of handlers, so we don't proxy to dartlang.org when we don't need to.
  });

  // Proxy any other request through to the static file handler
  app.fallback(wrappedHandler);

  await http.startServer(InternetAddress.loopbackIPv4, 8080);
  print('Proxying at ${http.uri}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_shelf: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_shelf/angel_shelf.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
41
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
30
Learn more about scoring.

We analyzed this package on Jul 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_shelf/angel_shelf.dart with components: io, isolate.

Health issues and suggestions

Fix lib/src/convert.dart. (-58.02 points)

Analysis of lib/src/convert.dart failed with 3 errors, 1 hint:

line 42 col 30: The method 'parseRawRequestBuffer' isn't defined for the class 'HttpRequestContext'.

line 61 col 30: The method 'parseRawRequestBuffer' isn't defined for the class 'Http2RequestContext'.

line 86 col 24: The method 'parseRawRequestBuffer' isn't defined for the class 'RequestContext'.

line 46 col 9: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Fix lib/src/embed_shelf.dart. (-1.49 points)

Analysis of lib/src/embed_shelf.dart reported 3 hints:

line 16 col 29: Use = to separate a named parameter from its default value.

line 17 col 10: This function has a return type of 'Future

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (stream_channel).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_framework ^2.0.0-alpha 2.0.4
path ^1.0.0 1.6.2
shelf ^0.7.0 0.7.5
stream_channel ^1.0.0 1.7.0 2.0.0
Transitive dependencies
angel_container 1.0.4
angel_http_exception 1.1.0
angel_model 1.0.3
angel_route 3.0.6
async 2.3.0
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.11
combinator 1.1.0
convert 2.1.1
crypto 2.0.6
dart2_constant 1.0.2+dart2
file 5.0.8+1
http2 1.0.0
http_parser 3.1.3
http_server 0.9.8+3
intl 0.15.8
logging 0.11.3+2
matcher 0.12.5
merge_map 1.0.2
meta 1.1.7
mime 0.9.6+3
mock_request 1.0.6
pedantic 1.8.0+1
quiver 2.0.3
quiver_hashcode 2.0.0
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
tuple 1.0.2
typed_data 1.1.6
uuid 2.0.2
Dev dependencies
angel_test ^2.0.0-alpha
shelf_static ^0.2.8
test ^1.0.0

Admin