Last active
March 27, 2016 21:24
-
-
Save pylaligand/1d0acc22916f025ebff1 to your computer and use it in GitHub Desktop.
Proper use of shelf_route's Routeable and Router#addAll.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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