Skip to content

Commit

Permalink
Add shelf_router middleware examples (#417)
Browse files Browse the repository at this point in the history
Update the `shelf_router` README with an example of how to configure middleware

Fixes #416
  • Loading branch information
andyhorn committed Mar 6, 2024
1 parent da6a69b commit 1acbc67
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 10 additions & 3 deletions pkgs/shelf_router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
var app = Router();
// instantiate a router and configure your routes
var router = Router();
app.get('/hello', (Request request) {
router.get('/hello', (Request request) {
return Response.ok('hello-world');
});
app.get('/user/<user>', (Request request, String user) {
router.get('/user/<user>', (Request request, String user) {
return Response.ok('hello $user');
});
// use a Pipeline to configure your middleware,
// then add the router as the handler
final app = const Pipeline()
.addMiddleware(logRequests())
.addHandler(router);
var server = await io.serve(app, 'localhost', 8080);
```

Expand Down
6 changes: 5 additions & 1 deletion pkgs/shelf_router/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class Service {
return Response.notFound('Page not found');
});

return router.call;
// Set up your Pipeline with any middleware you want to use and set the
// router as the handler.
return const Pipeline()
.addMiddleware(logRequests())
.addHandler(router.call);
}
}

Expand Down

0 comments on commit 1acbc67

Please sign in to comment.