diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..de2210c9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: dart \ No newline at end of file diff --git a/README.md b/README.md index c2f4bba3..d8fd5f3e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # shelf +[![version 1.0.0](https://img.shields.io/badge/pub-v1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_shelf) +[![build status](https://travis-ci.org/angel-dart/shelf.svg)](https://travis-ci.org/angel-dart/shelf) + Shelf interop with Angel. Will be deprecated by v2.0.0. By version 2 of Angel, I will migrate the server to run on top of `shelf`. @@ -23,8 +26,8 @@ main() async { // Re-route all other traffic to an // existing shelf/Redstone application. - app.mount('/', embedShelf( - shelf.Pipeline() + app.after.add(embedShelf( + new shelf.Pipeline() .addMiddleware(shelf.logRequests()) .addHandler(_echoRequest) )); diff --git a/lib/angel_shelf.dart b/lib/angel_shelf.dart new file mode 100644 index 00000000..efa0d5a5 --- /dev/null +++ b/lib/angel_shelf.dart @@ -0,0 +1,13 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:shelf/shelf.dart' as shelf; +import 'package:shelf/shelf_io.dart' as io; + +/// Simply passes an incoming request to a `shelf` handler. +RequestHandler embedShelf(shelf.Handler handler) { + return (RequestContext req, ResponseContext res) async { + res + ..willCloseItself = true + ..end(); + io.handleRequest(req.io, handler); + }; +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..0f018d5b --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,13 @@ +name: angel_shelf +description: Shelf interop with Angel. Will be deprecated by v2.0.0. +version: 1.0.0 +author: Tobe O +homepage: https://github.com/angel-dart/shelf +environment: + sdk: ">=1.19.0" +dependencies: + angel_framework: ^1.0.0-dev + shelf: ^0.6.0 +dev_dependencies: + angel_test: ^1.0.0-dev + test: ^0.12.0 \ No newline at end of file diff --git a/test/all_test.dart b/test/all_test.dart new file mode 100644 index 00000000..d03ce8cc --- /dev/null +++ b/test/all_test.dart @@ -0,0 +1,41 @@ +import 'dart:convert'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_shelf/angel_shelf.dart'; +import 'package:angel_test/angel_test.dart'; +import 'package:shelf/shelf.dart' as shelf; +import 'package:shelf/shelf_io.dart' as io; +import 'package:test/test.dart'; + +main() async { + Angel app; + TestClient client; + + setUp(() async { + var handler = new shelf.Pipeline() + .addMiddleware(shelf.logRequests()) + .addHandler(_echoRequest); + + app = new Angel(); + app.get('/angel', 'Angel'); + app.after.add(embedShelf(handler)); + + client = await connectTo(app); + }); + + tearDown(() => client.close()); + + test('expose angel side', () async { + var response = await client.get('/angel'); + expect(JSON.decode(response.body), equals('Angel')); + }); + + test('expose shelf side', () async { + var response = await client.get('/foo'); + expect(response, hasStatus(200)); + expect(response.body, equals('Request for "foo"')); + }); +} + +shelf.Response _echoRequest(shelf.Request request) { + return new shelf.Response.ok('Request for "${request.url}"'); +}