2017-06-20 19:31:35 +00:00
|
|
|
import 'dart:io';
|
2021-06-10 08:47:05 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
|
|
|
import 'package:angel3_proxy/angel3_proxy.dart';
|
2017-09-24 18:49:18 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2017-06-20 19:31:35 +00:00
|
|
|
|
2019-05-02 23:19:30 +00:00
|
|
|
final Duration timeout = Duration(seconds: 5);
|
2017-06-20 19:31:35 +00:00
|
|
|
|
2021-06-10 08:47:05 +00:00
|
|
|
void main() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
var app = Protevus();
|
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.
|
2019-05-02 23:19:30 +00:00
|
|
|
var pubProxy = Proxy(
|
2019-10-12 15:19:57 +00:00
|
|
|
'https://pub.dartlang.org',
|
2017-09-24 18:49:18 +00:00
|
|
|
publicPath: '/pub',
|
|
|
|
timeout: timeout,
|
|
|
|
);
|
2021-06-10 08:47:05 +00:00
|
|
|
app.all('/pub/*', pubProxy.handleRequest);
|
2017-06-20 19:31:35 +00:00
|
|
|
|
2018-11-09 00:14:32 +00:00
|
|
|
// Surprise! We can also proxy WebSockets.
|
|
|
|
//
|
|
|
|
// Play around with this at http://www.websocket.org/echo.html.
|
2019-05-02 23:19:30 +00:00
|
|
|
var echoProxy = Proxy(
|
2019-10-12 15:19:57 +00:00
|
|
|
'http://echo.websocket.org',
|
2018-11-09 00:14:32 +00:00
|
|
|
publicPath: '/echo',
|
|
|
|
timeout: timeout,
|
|
|
|
);
|
|
|
|
app.get('/echo', echoProxy.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.
|
2019-05-02 23:19:30 +00:00
|
|
|
var dartlangProxy = Proxy(
|
2019-10-12 15:19:57 +00:00
|
|
|
'https://dartlang.org',
|
2017-09-24 18:49:18 +00:00
|
|
|
timeout: timeout,
|
|
|
|
recoverFrom404: false,
|
|
|
|
);
|
2018-11-02 04:22:32 +00:00
|
|
|
app.all('*', dartlangProxy.handleRequest);
|
2017-06-20 19:31:35 +00:00
|
|
|
|
|
|
|
// In case we can't connect to dartlang.org, show an error.
|
2018-11-08 22:13:26 +00:00
|
|
|
app.fallback(
|
|
|
|
(req, res) => res.write('Couldn\'t connect to Pub or dartlang.'));
|
2017-06-20 19:31:35 +00:00
|
|
|
|
2024-10-12 10:35:14 +00:00
|
|
|
app.logger = Logger('protevus')
|
2017-09-24 18:49:18 +00:00
|
|
|
..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
|
|
|
|
2018-11-08 22:13:26 +00:00
|
|
|
var server =
|
2024-10-12 10:35:14 +00:00
|
|
|
await ProtevusHttp(app).startServer(InternetAddress.loopbackIPv4, 8080);
|
2017-06-20 19:31:35 +00:00
|
|
|
print('Listening at http://${server.address.address}:${server.port}');
|
2018-11-08 22:13:26 +00:00
|
|
|
print(
|
|
|
|
'Check this out! http://${server.address.address}:${server.port}/pub/packages/angel_framework');
|
2017-06-20 19:31:35 +00:00
|
|
|
}
|