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';
|
|
|
|
import 'package:file/local.dart';
|
2018-12-11 03:22:34 +00:00
|
|
|
import 'package:lumberjack/lumberjack.dart';
|
|
|
|
import 'package:lumberjack/io.dart';
|
2018-11-08 04:11:10 +00:00
|
|
|
|
|
|
|
main() async {
|
|
|
|
var app = new Angel();
|
2018-12-11 03:22:34 +00:00
|
|
|
app.logger = new Logger('angel');
|
|
|
|
app.logger.pipe(new AnsiLogPrinter.toStdout());
|
2018-11-08 04:11:10 +00:00
|
|
|
|
2018-12-09 15:49:59 +00:00
|
|
|
var publicDir = new Directory('example/http2/public');
|
2018-11-08 04:11:10 +00:00
|
|
|
var indexHtml =
|
|
|
|
const LocalFileSystem().file(publicDir.uri.resolve('index.html'));
|
|
|
|
var styleCss =
|
|
|
|
const LocalFileSystem().file(publicDir.uri.resolve('style.css'));
|
|
|
|
var appJs = const LocalFileSystem().file(publicDir.uri.resolve('app.js'));
|
|
|
|
|
|
|
|
// Send files when requested
|
|
|
|
app
|
|
|
|
..get('/style.css', (req, res) => res.streamFile(styleCss))
|
|
|
|
..get('/app.js', (req, res) => res.streamFile(appJs));
|
|
|
|
|
|
|
|
app.get('/', (req, res) async {
|
|
|
|
// Regardless of whether we pushed other resources, let's still send /index.html.
|
|
|
|
await res.streamFile(indexHtml);
|
|
|
|
|
|
|
|
// If the client is HTTP/2 and supports server push, let's
|
|
|
|
// send down /style.css and /app.js as well, to improve initial load time.
|
|
|
|
if (res is Http2ResponseContext && res.canPush) {
|
|
|
|
await res.push('/style.css').streamFile(styleCss);
|
|
|
|
await res.push('/app.js').streamFile(appJs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var ctx = new SecurityContext()
|
|
|
|
..useCertificateChain('dev.pem')
|
|
|
|
..usePrivateKey('dev.key', password: 'dartdart');
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.setAlpnProtocols(['h2'], true);
|
|
|
|
} catch (e, st) {
|
2018-12-11 03:19:16 +00:00
|
|
|
app.logger.error(
|
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-11 03:19:16 +00:00
|
|
|
error: e,
|
|
|
|
stackTrace: st,
|
2018-11-08 04:11:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var http1 = new AngelHttp(app);
|
|
|
|
var http2 = new AngelHttp2(app, ctx);
|
|
|
|
|
|
|
|
// HTTP/1.x requests will fallback to `AngelHttp`
|
|
|
|
http2.onHttp1.listen(http1.handleRequest);
|
|
|
|
|
|
|
|
var server = await http2.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at https://${server.address.address}:${server.port}');
|
|
|
|
}
|