platform/example/main.dart

56 lines
1.7 KiB
Dart
Raw Normal View History

2017-06-20 19:31:35 +00:00
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_proxy/angel_proxy.dart';
2017-09-24 18:49:18 +00:00
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
2017-06-20 19:31:35 +00:00
2017-09-24 18:49:18 +00:00
final Duration timeout = new Duration(seconds: 5);
2017-06-20 19:31:35 +00:00
main() async {
var app = new Angel();
2017-09-24 18:49:18 +00:00
var client = new http.Client();
2017-06-20 19:31:35 +00:00
// Forward any /api requests to pub.
// By default, if the host throws a 404, the request will fall through to the next handler.
2017-09-24 18:49:18 +00:00
var pubProxy = new Proxy(
app,
client,
'https://pub.dartlang.org',
publicPath: '/pub',
timeout: timeout,
);
app.use(pubProxy.handleRequest);
2017-06-20 19:31:35 +00:00
// 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) {
2017-09-24 18:49:18 +00:00
return pubProxy.servePath(req.path, req, res);
2017-06-20 19:31:35 +00:00
});
// Anything else should fall through to dartlang.org.
2017-09-24 18:49:18 +00:00
var dartlangProxy = new Proxy(
app,
client,
'https://dartlang.org',
timeout: timeout,
recoverFrom404: false,
);
app.use(dartlangProxy.handleRequest);
2017-06-20 19:31:35 +00:00
// In case we can't connect to dartlang.org, show an error.
2017-09-24 18:49:18 +00:00
app.use('Couldn\'t connect to Pub or dartlang.');
2017-06-20 19:31:35 +00:00
2017-09-24 18:49:18 +00:00
app.logger = new Logger('angel')
..onRecord.listen(
(rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
},
);
2017-06-20 19:31:35 +00:00
var server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 8080);
print('Listening at http://${server.address.address}:${server.port}');
2017-09-24 18:49:18 +00:00
print('Check this out! http://${server.address.address}:${server.port}/pub/packages/angel_framework');
2017-06-20 19:31:35 +00:00
}