This commit is contained in:
thosakwe 2017-07-23 23:33:31 -04:00
parent e05d35ee67
commit 487e825f8d
4 changed files with 13 additions and 3 deletions

View file

@ -5,6 +5,9 @@ import 'package:angel_framework/angel_framework.dart';
final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?'); final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)'); final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
/// Used to mount a route for a [ProxyLayer].
typedef Route ProxyLayerRouteAssigner(Router router, String path, handler);
String _pathify(String path) { String _pathify(String path) {
var p = path.replaceAll(_straySlashes, ''); var p = path.replaceAll(_straySlashes, '');
@ -53,6 +56,7 @@ class ProxyLayer {
final int port; final int port;
final String protocol; final String protocol;
final Duration timeout; final Duration timeout;
ProxyLayerRouteAssigner routeAssigner;
ProxyLayer( ProxyLayer(
this.host, this.host,
@ -65,10 +69,13 @@ class ProxyLayer {
this.recoverFrom404: true, this.recoverFrom404: true,
this.streamToIO: false, this.streamToIO: false,
this.timeout, this.timeout,
this.routeAssigner,
SecurityContext securityContext, SecurityContext securityContext,
}) { }) {
_client = new HttpClient(context: securityContext); _client = new HttpClient(context: securityContext);
_prefix = publicPath.replaceAll(_straySlashes, ''); _prefix = publicPath.replaceAll(_straySlashes, '');
routeAssigner ??=
(Router router, String path, handler) => router.get(path, handler);
} }
call(Angel app) async => serve(this.app = app); call(Angel app) async => serve(this.app = app);
@ -83,7 +90,7 @@ class ProxyLayer {
return serveFile(path, req, res); return serveFile(path, req, res);
} }
router.all('$publicPath/*', handler); routeAssigner(router, '$publicPath/*', handler);
} }
serveFile(String path, RequestContext req, ResponseContext res) async { serveFile(String path, RequestContext req, ResponseContext res) async {

View file

@ -12,6 +12,7 @@ class PubServeLayer extends ProxyLayer {
int port: 8080, int port: 8080,
String protocol: 'http', String protocol: 'http',
String publicPath: '/', String publicPath: '/',
ProxyLayerRouteAssigner routeAssigner,
Duration timeout}) Duration timeout})
: super(host, port, : super(host, port,
debug: debug, debug: debug,
@ -21,6 +22,7 @@ class PubServeLayer extends ProxyLayer {
recoverFromDead: recoverFromDead != false, recoverFromDead: recoverFromDead != false,
recoverFrom404: recoverFrom404 != false, recoverFrom404: recoverFrom404 != false,
streamToIO: streamToIO != false, streamToIO: streamToIO != false,
routeAssigner: routeAssigner,
timeout: timeout); timeout: timeout);
@override @override

View file

@ -1,6 +1,6 @@
name: angel_proxy name: angel_proxy
description: Angel middleware to forward requests to another server (i.e. pub serve). description: Angel middleware to forward requests to another server (i.e. pub serve).
version: 1.0.8 version: 1.0.9
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/proxy homepage: https://github.com/angel-dart/proxy
environment: environment:

View file

@ -20,7 +20,8 @@ main() {
await app.configure(new ProxyLayer( await app.configure(new ProxyLayer(
testServer.address.address, testServer.port, testServer.address.address, testServer.port,
publicPath: '/proxy')); publicPath: '/proxy',
routeAssigner: (router, path, handler) => router.all(path, handler)));
await app.configure(new ProxyLayer( await app.configure(new ProxyLayer(
testServer.address.address, testServer.port, testServer.address.address, testServer.port,
mapTo: '/foo')); mapTo: '/foo'));