platform/packages/framework/example/http2/main.dart

44 lines
1.1 KiB
Dart
Raw Permalink Normal View History

2018-11-08 04:11:10 +00:00
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_framework/http2.dart';
2018-12-14 02:31:21 +00:00
import 'package:logging/logging.dart';
import 'common.dart';
2018-11-08 04:11:10 +00:00
main() async {
2019-05-02 22:48:31 +00:00
var app = Angel()
2018-11-08 04:11:10 +00:00
..encoders.addAll({
'gzip': gzip.encoder,
'deflate': zlib.encoder,
});
2019-05-02 22:48:31 +00:00
app.logger = Logger('angel')..onRecord.listen(dumpError);
2018-11-08 04:11:10 +00:00
2018-11-11 01:07:09 +00:00
app.get('/', (req, res) => 'Hello HTTP/2!!!');
2018-11-08 04:11:10 +00:00
2019-05-02 22:48:31 +00:00
app.fallback((req, res) => throw AngelHttpException.notFound(
2018-12-09 15:49:59 +00:00
message: 'No file exists at ${req.uri}'));
2018-11-08 04:11:10 +00:00
2019-05-02 22:48:31 +00:00
var ctx = SecurityContext()
2018-11-08 04:11:10 +00:00
..useCertificateChain('dev.pem')
..usePrivateKey('dev.key', password: 'dartdart');
try {
ctx.setAlpnProtocols(['h2'], true);
} catch (e, st) {
2018-12-14 02:31:21 +00:00
app.logger.severe(
2018-11-08 04:11:10 +00:00
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
2018-12-14 02:31:21 +00:00
e,
st,
2018-11-08 04:11:10 +00:00
);
}
2019-05-02 22:48:31 +00:00
var http1 = AngelHttp(app);
var http2 = AngelHttp2(app, ctx);
2018-11-08 04:11:10 +00:00
// HTTP/1.x requests will fallback to `AngelHttp`
http2.onHttp1.listen(http1.handleRequest);
2018-11-11 01:07:09 +00:00
await http2.startServer('127.0.0.1', 3000);
print('Listening at ${http2.uri}');
2018-11-08 04:11:10 +00:00
}