2016-02-28 13:11:17 +00:00
|
|
|
part of angel_framework.http;
|
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
typedef Route RouteAssigner(Pattern path, handler, {List middleware});
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-02-28 13:11:17 +00:00
|
|
|
/// A routable server that can handle dynamic requests.
|
2016-04-21 20:37:02 +00:00
|
|
|
class Routable extends Extensible {
|
2016-02-28 13:11:17 +00:00
|
|
|
/// Additional filters to be run on designated requests.
|
2016-04-18 03:27:23 +00:00
|
|
|
Map <String, Middleware> middleware = {};
|
2016-02-28 13:11:17 +00:00
|
|
|
|
|
|
|
/// Dynamic request paths that this server will respond to.
|
2016-04-18 03:27:23 +00:00
|
|
|
List<Route> routes = [];
|
|
|
|
|
|
|
|
/// A set of [Service] objects that have been mapped into routes.
|
|
|
|
Map <Pattern, Service> services = {};
|
|
|
|
|
|
|
|
/// Assigns a middleware to a name for convenience.
|
|
|
|
registerMiddleware(String name, Middleware middleware) {
|
|
|
|
this.middleware[name] = middleware;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the service assigned to the given path.
|
|
|
|
Service service(Pattern path) => services[path];
|
|
|
|
|
|
|
|
/// Incorporates another routable's routes into this one's.
|
|
|
|
use(Pattern path, Routable routable) {
|
|
|
|
middleware.addAll(routable.middleware);
|
|
|
|
for (Route route in routable.routes) {
|
|
|
|
Route provisional = new Route('', path);
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
if (route.path == '/') {
|
|
|
|
route.path = '';
|
|
|
|
route.matcher = new RegExp(r'^\/?$');
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
route.matcher = new RegExp(route.matcher.pattern.replaceAll(
|
|
|
|
new RegExp('^\\^'),
|
|
|
|
provisional.matcher.pattern.replaceAll(new RegExp(r'\$$'), '')));
|
|
|
|
route.path = "$path${route.path}";
|
|
|
|
|
|
|
|
routes.add(route);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routable is Service) {
|
2016-04-29 01:10:29 +00:00
|
|
|
services[path.toString().trim().replaceAll(new RegExp(r'(^\/+)|(\/+$)'), '')] = routable;
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
/// Adds a route that responds to the given path
|
|
|
|
/// for requests with the given method (case-insensitive).
|
|
|
|
/// Provide '*' as the method to respond to all methods.
|
|
|
|
addRoute(String method, Pattern path, Object handler, {List middleware}) {
|
|
|
|
var route = new Route(method.toUpperCase().trim(), path, (middleware ?? [])
|
|
|
|
..add(handler));
|
|
|
|
routes.add(route);
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to any request matching the given path.
|
|
|
|
all(Pattern path, Object handler, {List middleware}) {
|
|
|
|
return addRoute('*', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a GET request.
|
|
|
|
get(Pattern path, Object handler, {List middleware}) {
|
|
|
|
return addRoute('GET', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a POST request.
|
|
|
|
post(Pattern path, Object handler, {List middleware}) {
|
|
|
|
return addRoute('POST', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a PATCH request.
|
|
|
|
patch(Pattern path, Object handler, {List middleware}) {
|
|
|
|
return addRoute('PATCH', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a DELETE request.
|
|
|
|
delete(Pattern path, Object handler, {List middleware}) {
|
|
|
|
return addRoute('DELETE', path, handler, middleware: middleware);
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
Routable() {
|
|
|
|
}
|
|
|
|
|
2016-02-28 13:11:17 +00:00
|
|
|
}
|