angel_proxy 2.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

proxy #

Pub build status

Angel middleware to forward requests to another server (i.e. webdev serve). Also supports WebSockets.

import 'package:angel_proxy/angel_proxy.dart';
import 'package:http/http.dart' as http;

main() async {
  // Forward requests instead of serving statically.
  // You can also pass a URI, instead of a string.
  var proxy1 = Proxy('http://localhost:3000');
  
  // handle all methods (GET, POST, ...)
  app.fallback(proxy.handleRequest);
}

You can also restrict the proxy to serving only from a specific root:

Proxy(baseUrl, publicPath: '/remote');

Also, you can map requests to a root path on the remote server:

Proxy(baseUrl.replace(path: '/path'));

Request bodies will be forwarded as well, if they are not empty. This allows things like POST requests to function.

For a request body to be forwarded, the body must not have already been parsed.

2.2.0 #

  • Use http.Client instead of http.BaseClient, and make it an optional parameter.
  • Allow baseUrl to accept Uri or String.
  • Add Proxy.pushState.

2.1.2 #

  • Apply lints.

2.1.1 #

  • Update for framework@2.0.0-alpha.15

2.1.0 #

  • Use Uri instead of archaic host, port, and mapTo. Also cleaner + safer + easier.
  • Enable WebSocket proxying.

2.0.0 #

  • Updates for Angel 2. Big thanks to @denkuy!
  • Use package:path for better path resolution.

1.1.1 #

  • Removed reference to io; now works with HTTP/2. Thanks to @daniel-v!

example/main.dart

import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_proxy/angel_proxy.dart';
import 'package:logging/logging.dart';

final Duration timeout = Duration(seconds: 5);

main() async {
  var app = Angel();

  // Forward any /api requests to pub.
  // By default, if the host throws a 404, the request will fall through to the next handler.
  var pubProxy = Proxy(
    'https://pub.dartlang.org',
    publicPath: '/pub',
    timeout: timeout,
  );
  app.all("/pub/*", pubProxy.handleRequest);

  // Surprise! We can also proxy WebSockets.
  //
  // Play around with this at http://www.websocket.org/echo.html.
  var echoProxy = Proxy(
    'http://echo.websocket.org',
    publicPath: '/echo',
    timeout: timeout,
  );
  app.get('/echo', echoProxy.handleRequest);

  // Pub's HTML assumes that the site's styles, etc. are on the absolute path `/static`.
  // This is not the case here. Let's patch that up:
  app.get('/static/*', (RequestContext req, res) {
    return pubProxy.servePath(req.path, req, res);
  });

  // Anything else should fall through to dartlang.org.
  var dartlangProxy = Proxy(
    'https://dartlang.org',
    timeout: timeout,
    recoverFrom404: false,
  );
  app.all('*', dartlangProxy.handleRequest);

  // In case we can't connect to dartlang.org, show an error.
  app.fallback(
      (req, res) => res.write('Couldn\'t connect to Pub or dartlang.'));

  app.logger = Logger('angel')
    ..onRecord.listen(
      (rec) {
        print(rec);
        if (rec.error != null) print(rec.error);
        if (rec.stackTrace != null) print(rec.stackTrace);
      },
    );

  var server =
      await AngelHttp(app).startServer(InternetAddress.loopbackIPv4, 8080);
  print('Listening at http://${server.address.address}:${server.port}');
  print(
      'Check this out! http://${server.address.address}:${server.port}/pub/packages/angel_framework');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_proxy: ^2.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_proxy/angel_proxy.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
55
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Nov 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_proxy/angel_proxy.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
angel_framework ^2.0.0-alpha 2.0.4+1 2.0.5-beta
http ^0.12.0 0.12.0+2
http_parser ^3.0.0 3.1.3
path ^1.0.0 1.6.4
Transitive dependencies
angel_container 1.1.0
angel_http_exception 1.1.0
angel_model 1.0.3
angel_route 3.0.6
async 2.4.0
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.12
combinator 1.1.0
convert 2.1.1
crypto 2.1.3
dart2_constant 1.0.2+dart2
file 5.1.0
http2 1.0.0
http_server 0.9.8+3
intl 0.16.0
matcher 0.12.6
merge_map 1.0.2
meta 1.1.8
mime 0.9.6+3
quiver 2.1.2+1
quiver_hashcode 2.0.0
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
tuple 1.0.3
typed_data 1.1.6
uuid 2.0.4
Dev dependencies
angel_test ^2.0.0-alpha
logging any 0.11.3+2
mock_request any 1.0.6
pedantic ^1.0.0 1.8.0+1
test ^1.0.0