angel_proxy 1.1.1 copy "angel_proxy: ^1.1.1" to clipboard
angel_proxy: ^1.1.1 copied to clipboard

outdated

Angel middleware to forward requests to another server (i.e. pub serve).

example/main.dart

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

final Duration timeout = new Duration(seconds: 5);

main() async {
  var app = new Angel();
  var client = new http.Client();

  // 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 = new Proxy(
    app,
    client,
    'https://pub.dartlang.org',
    publicPath: '/pub',
    timeout: timeout,
  );
  app.use(pubProxy.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 = new Proxy(
    app,
    client,
    'https://dartlang.org',
    timeout: timeout,
    recoverFrom404: false,
  );
  app.use(dartlangProxy.handleRequest);

  // In case we can't connect to dartlang.org, show an error.
  app.use('Couldn\'t connect to Pub or dartlang.');

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

  var server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 8080);
  print('Listening at http://${server.address.address}:${server.port}');
  print('Check this out! http://${server.address.address}:${server.port}/pub/packages/angel_framework');
}
0
likes
40
pub points
19%
popularity

Publisher

verified publisherangel-dart.dev

Angel middleware to forward requests to another server (i.e. pub serve).

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

angel_framework, http

More

Packages that depend on angel_proxy