Skip to content

Instantly share code, notes, and snippets.

@pylaligand
Last active March 27, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pylaligand/1d0acc22916f025ebff1 to your computer and use it in GitHub Desktop.
Save pylaligand/1d0acc22916f025ebff1 to your computer and use it in GitHub Desktop.
Proper use of shelf_route's Routeable and Router#addAll.
import 'dart:async' show runZoned;
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart';
void main() {
final mainRouter = router()
// localhost:9999/foo works, but not localhost:9999/bar
..addAll(new MyRouteable('/foo'))
..addAll(new MyRouteable('/bar'));
/* both paths work
..addAll(new MyRouteable('/'), path: '/foo')
..addAll(new MyRouteable('/'), path: '/bar');
*/
final handler = mainRouter.handler;
runZoned(() {
print('Server ready');
io.serve(handler, '0.0.0.0', 9999);
});
}
class MyRouteable extends Routeable {
final String _path;
MyRouteable(this._path);
@override
createRoutes(Router router) {
router.get(_path, _handle);
}
shelf.Response _handle(shelf.Request request) {
return new shelf.Response.ok('Hello from $_path');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment