pub repository
mirror.p-hatan.comshelf_route
0.3.0Routing middleware for Shelf
Readme
Router for Dart Shelf
Introduction
Provides Shelf middleware for defining routes.
Shelf Route makes it easy to define routes in a modular way. How?
- A
Routeris simply a Shelf Middleware component. - You can define a single Router for your whole application or as many as you like in a heirarchical structure.
- When a Router passes a request to a child route it first modifies the request as follows:
- The route's
pathis removed from the start ofpathInfo. e.g./banking/accounts->/accounts - The route's
pathis appended to thescriptName. e.g./->/banking - That supports modularity in the child routes as the pathInfo only contains the path relative to that component.
- The route's
Using
Basics
First set up a little test handler that just echos the request
shelf.Response _echoRequest(shelf.Request request) {
return new shelf.Response.ok('Request for "${request.pathInfo}"');
}
To route all requests that start with /public to this handler
var router = (route.router()
..addRoute(_echoRequest, path: '/public'))
.handler;
Then you can create a simple server passing in the router you just created
io.serve(router, 'localhost', 8080).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
To route only 'GET' requests
var router = (route.router()
..addRoute(_echoRequest, path: '/public', method: 'GET'))
.handler;
or better still the simpler form
var router = (route.router()
..get('/public', _echoRequest))
.handler;
Path Variables
You can include variables in the paths that are passed to the router. Path variables are contained within {}. For example {name} is a path variable called name.
To route a 'GET' with a variable for the users name
var router = (route.router()
..get('/user/{name}', _echoRequest))
.handler;
To POST a new deposit into an account resource
var router = (route.router()
..post('/account/{accountNumber}/deposit', _echoRequest))
.handler;
In a future release you will be able to specify constraints like type and regex on path variables. Currently only strings are supported.
You can then access these variables from inside the handler
var pv = route.getPathVariables(request);
var accountNumber = pv['accountNumber'];
Currently they are added to the request headers with a prefix of shelf.route.. So, whilst this is discouraged as it will change in the future, you can also get the value as follows
request.headers['shelf.route.accountNumber']
Note that once shelf supports extra parameters on messages Shelf Route will switch over to using them and deprecate the use of headers.
This will be seemless to anyone that uses getPathVariables
Note that the path argument is parsed as a UriTemplate so you can do more than simple {name} style paths.
Heirarchical Routers
To improve modularity you can break up your routes into a series of nested routes. For example you can set up a top level /banking route
var router = route.router();
var bankingRouter = router.addChildRouter('/banking');
var handler = router.handler;
and then set up the bankingRouter
bankingRouter
..post('/account/{accountNumber}/deposit', _echoRequest)
.handler;
Note in this case the full path of the deposit resource is actually /banking/account/{accountNumber}/deposit.
To try this out, fire up the server and do
curl -d 'lots of money' http://localhost:8080/banking/account/1235/deposit
More Complex Example
The following example shows several of these features together.
Full source at example/routing_example.dart
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart' as route;
void main() {
var publicRoute = const shelf.Stack()
.addHandler(_echoRequest);
var router = (route.router()
..addRoute(publicRoute, path: '/public')
..addRoute(_bankingRoutes(), path: '/banking'))
.handler;
var handler = const shelf.Stack()
.addMiddleware(shelf.logRequests())
.addHandler(router);
io.serve(handler, 'localhost', 8080).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}
shelf.Response _echoRequest(shelf.Request request) {
return new shelf.Response.ok('Request for "${request.pathInfo}"');
}
// supports modularity with routes. i.e. here the banking section has it's own routes
// these are relative to where the main router places them
shelf.Handler _bankingRoutes() {
var transfersRoute = const shelf.Stack()
.addHandler(_echoRequest);
var bankingRouter = (route.router()
..addRoute(_accountsRoute(), path: '/account')
..addRoute(transfersRoute, path: '/transfer'))
.handler;
var bankingRoute = const shelf.Stack()
// .addMiddleware(authenticator); // obviously they would be authenticated
.addHandler(bankingRouter);
return bankingRoute;
}
// all accounts
shelf.Handler _accountsRoute() {
return (route.router()
..addRoute(_individualAccountRoute(), path: '/{accountNumber}')
..get('/', _searchAccounts))
.handler;
}
// a single account with an account number. Note accountNumber is now
// in request.extraParams
shelf.Handler _individualAccountRoute() {
return (route.router()
..get('/', _fetchAccount)
..post('/deposit', _deposit))
.handler;
}
shelf.Response _fetchAccount(shelf.Request request) {
return new shelf.Response.ok(
"Fetch account for acct num ${request.headers['shelf.route.accountNumber']}");
}
shelf.Response _deposit(shelf.Request request) {
return new shelf.Response.ok(
"Deposit into account for acct num ${request.headers['shelf.route.accountNumber']}");
}
shelf.Response _searchAccounts(shelf.Request request) {
return new shelf.Response.ok("Search accounts");
}
TODO
- Add support for types in path variables
- Add support for constraints on path variables
- Add support for custom UriPatterns
Authors
Changelog
0.3.0
-
Use UriPattern and UriTemplate instead of custom path matching
- path variables now use
{}instead of:
- path variables now use
0.2.0+1
- Fixed handling of routes that are just a path variable
0.2.0
- Upgraded to shelf 0.4.0
0.1.2+1
- Fixed bug where body was missing on copied request
0.1.2
- Added helper function
getPathVariablesto encapsulate extracting path variables stored in the request
0.1.1
-
Added support for path variables.
-
Improved tests.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add shelf_routepubspec.yaml
name: shelf_route
version: 0.3.0
author: Anders Holmgren <andersmholmgren@gmail.com>
description: Routing middleware for Shelf
homepage: https://bitbucket.org/andersmholmgren/shelf_route
environment:
sdk: '>=1.2.0 <2.0.0'
dependencies:
matcher: '>=0.10.0 <0.11.0'
path: '>=1.1.0 <2.0.0'
shelf: '>=0.4.0 <0.5.0'
uri: '>=0.9.0 <0.10.0'
dev_dependencies:
unittest: '>=0.10.1 <0.11.0'
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | 2014-04-05T01:42:32.871200Z | Download | 25b49b0ef930d513e3ae576d718d992c852d80b9b412d180d8e9bbf59b5d287f |
| 0.1.0+1 | 2014-04-05T01:45:53.983660Z | Download | e31120b07b38136a984b7acb224572f0c13b3e81f70ae142dc3111155cd4f0bd |
| 0.1.0+2 | 2014-04-05T07:11:36.732650Z | Download | fe74e11d82321c6d138877c494d7ffbb62261a2ef7e28db3e399983b775c4907 |
| 0.1.1+1 | 2014-04-06T05:36:58.393550Z | Download | 23481e07612d85f39a4fa7e0e39bfd3681552c080d69f9c0bd724d2dfb9f1f2c |
| 0.1.2 | 2014-04-07T01:55:19.090490Z | Download | 772251271f22b23c1203a3d58f61bc9138030fe8a5ba0542dc3b73b8c6c5d1f3 |
| 0.1.2+1 | 2014-04-07T02:54:32.068370Z | Download | 0ee0ac1a807e3f490de280492ce7fff6075550866914adc1e81fedd46210a867 |
| 0.2.0 | 2014-04-11T05:21:21.528060Z | Download | 70609f8398e271ab3ad145b6d514fee3d0f1896f009e6bace7ba0998777ec688 |
| 0.2.0+1 | 2014-04-12T00:53:31.449850Z | Download | 1c152007cc727a37ed49eb51d433b110c5e12bd46c18409c176eba3a7d9f7654 |
| 0.3.0 | 2014-04-12T03:38:15.180330Z | Download | 1a80fd1f86b7a79dc239877daa0c2e5028818fa6fa2ac173a1dc60c1c353e506 |
| 0.3.1 | 2014-04-12T05:22:34.890670Z | Download | ef54b51195251c63a54d11372a396d03334aabace49315e37e52b942aaeb4204 |
| 0.4.0 | 2014-04-17T22:07:44.016450Z | Download | f2e5458b8f96a11770a9354d0ddfc9e3d58ac3f7eb485fdb17ee0197307c1533 |
| 0.5.0 | 2014-04-20T01:27:29.741040Z | Download | 3493204b1501efdad70b301df95f0ec032380e4e35b053fc9e00e4341a83abc3 |
| 0.6.0 | 2014-07-13T07:21:40.436370Z | Download | a44a8a992e9a0fa9a5ebd4fcdd45466719e0a93abd9599b7baf0c31bf022238f |
| 0.7.0 | 2014-07-22T20:51:52.644530Z | Download | f6bad62a92f15d74e839f2379fafe5a8b200ff1e3559d0e15a37168b2a8607c7 |
| 0.7.0+1 | 2014-08-06T09:55:26.066900Z | Download | 3fb49faf50659dd6f7824f6fdc082963fb821350469da7730fce1de2d0acc363 |
| 0.7.1 | 2014-08-09T07:40:50.426980Z | Download | e233b1103355bc35d16bae8da188d57a1cbe0d13ea2b81fb1554293a802f80dc |
| 0.8.0 | 2014-09-30T20:17:50.098080Z | Download | 74333ef252c861fe64872fdb4c8db0240784d592a5a35bbd68116d0126f8a7a5 |
| 0.8.0+1 | 2014-10-08T07:06:03.210550Z | Download | 6fe60973c8988e10c2208538f83bc4bd655685d05475cc3cb2739ca7023c1e1b |
| 0.9.0 | 2014-10-11T01:23:32.159570Z | Download | 16e19c6634258cd0013a43b7b395048f4b28464c41a613e532daa2efd5587ca8 |
| 0.10.0 | 2014-10-17T05:51:45.692520Z | Download | 2893c304ff16aaa25a56fc98f152f051f175ce9e3d3440a479b6cc372c3f9fdd |
| 0.10.1 | 2014-10-17T23:59:42.257370Z | Download | 3127353c2819f0c568411bff33fdf61fce023071bd58771217816e480e5971ea |
| 0.11.0 | 2014-11-01T01:43:28.127Z | Download | 933c6355649cdd6995100b9859637d6ff20f1680d1e38f5b9a7484903c0e31c1 |
| 0.11.0+1 | 2014-11-08T23:13:46.970400Z | Download | 42373cb88fe67876cc2a68fcc3462d4f28d3ac87a50d0ac79a9524ef1d5341c7 |
| 0.12.0 | 2015-03-20T23:25:56.164460Z | Download | 0ad04e64806ec2e6b0ee545ccb945ce1bf6ab10c660b06d446b106521097f044 |
| 0.13.0-beta.1 | 2015-06-20T21:29:49.961Z | Download | e818d7205e1066d1c485852faf007807039836d19f6a119e127dc36b2cde12a9 |
| 0.13.0-beta.2 | 2015-06-21T18:59:01.659Z | Download | 55a9c835cf2de813c07463ce4b9a25957452f2dcdfa6908bd5b92c2772ee3545 |
| 0.13.0-beta.3 | 2015-06-23T20:50:28.882Z | Download | f3271a77d3a045444276dc1c30691b666525d41cc23664a26cce521fac1bcd0f |
| 0.13.0-beta.4 | 2015-06-25T10:37:40.689Z | Download | f3c08f381ee829cc685f772f82418570c9128d593260feeaf82a05d746410e5e |
| 0.13.0 | 2015-06-25T11:08:03.385Z | Download | 1de8644648417292dfe0f9569a52dc33c3a9ad726e5b96ce5742babed2caf2b3 |
| 0.13.1 | 2015-06-25T12:22:52.783Z | Download | e4cd7d8046fa13bfdc691a94882c512e807717f3cb5ff360692ace54ed62f3ee |
| 0.13.2 | 2015-06-26T06:58:26.421Z | Download | f38e980a87f3b4395c2b7d3f566254c174afb2bb1cb356b926521d535081927d |
| 0.13.3 | 2015-07-01T21:24:04.810Z | Download | d2cc5d7e4cb543941ece4e89754f6282c46742cd61cf105656953e71eedc4e88 |
| 0.13.4 | 2015-07-04T05:13:28.163Z | Download | dc74b577bb8f76fe223266b8358be88dc4f04d875d6200913ade3c0162b5bc4d |
| 0.13.5 | 2015-08-12T23:10:04.490Z | Download | 0e1cdb8883f3aac8e500795d08e626288b44b7615076ef570691f0f162dd401a |
| 0.14.0 | 2015-08-30T22:11:53.122Z | Download | 03a3c4faa535ca26da1b629ed0ede7636a977e37cee4e0e0b49d826deff6b153 |
| 0.14.1 | 2016-01-06T06:48:13.797Z | Download | 1639423b95704088bae9187584b67865d4e1068424957c9f9aa4b978208e669b |
| 0.14.2 | 2016-04-10T01:56:50.717Z | Download | 19a05d0c7a350cbc9c9b0c8870c836737bcbffe1270830406f9d0878e39af654 |
| 0.14.3 | 2016-10-03T00:44:06.878Z | Download | 5507724c06f120d8aa50e3925e2871e68f95129b86741d29050ebc388f071891 |
Dependencies
matcher "\u003e=0.10.0 \u003c0.11.0"
path "\u003e=1.1.0 \u003c2.0.0"
shelf "\u003e=0.4.0 \u003c0.5.0"
uri "\u003e=0.9.0 \u003c0.10.0"