2024-09-23 01:44:59 +00:00
|
|
|
import 'dart:io';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_core/core.dart';
|
|
|
|
import 'package:platform_core/http.dart';
|
|
|
|
import 'package:platform_core/http2.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
void main() async {
|
2024-09-28 23:14:48 +00:00
|
|
|
var app = Application()
|
2024-09-23 01:44:59 +00:00
|
|
|
..encoders.addAll({
|
|
|
|
'gzip': gzip.encoder,
|
|
|
|
'deflate': zlib.encoder,
|
|
|
|
});
|
2024-09-23 20:35:32 +00:00
|
|
|
app.logger = Logger('protevus')..onRecord.listen(dumpError);
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
app.get('/', (req, res) => 'Hello HTTP/2!!!');
|
|
|
|
|
2024-09-28 23:14:48 +00:00
|
|
|
app.fallback((req, res) => throw PlatformHttpException.notFound(
|
|
|
|
message: 'No file exists at ${req.uri}'));
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
var ctx = SecurityContext()
|
|
|
|
..useCertificateChain('dev.pem')
|
|
|
|
..usePrivateKey('dev.key', password: 'dartdart');
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.setAlpnProtocols(['h2'], true);
|
|
|
|
} catch (e, st) {
|
|
|
|
app.logger.severe(
|
|
|
|
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
|
|
|
|
e,
|
|
|
|
st,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-28 23:14:48 +00:00
|
|
|
var http1 = PlatformHttp(app);
|
|
|
|
var http2 = PlatformHttp2(app, ctx);
|
2024-09-23 01:44:59 +00:00
|
|
|
|
2024-09-28 23:14:48 +00:00
|
|
|
// HTTP/1.x requests will fallback to `PlatformHttp`
|
2024-09-23 01:44:59 +00:00
|
|
|
http2.onHttp1.listen(http1.handleRequest);
|
|
|
|
|
|
|
|
await http2.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at ${http2.uri}');
|
|
|
|
}
|