platform/example/main.dart

57 lines
1.6 KiB
Dart
Raw Normal View History

2018-12-09 16:40:09 +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:angel_websocket/server.dart';
import 'package:file/local.dart';
import 'package:logging/logging.dart';
2018-07-10 16:54:55 +00:00
2018-12-09 16:40:09 +00:00
main(List<String> args) async {
2018-07-10 16:54:55 +00:00
var app = new Angel();
var http = new AngelHttp(app);
2018-12-09 16:40:09 +00:00
var ws = new AngelWebSocket(app, sendErrors: !app.isProduction);
var fs = const LocalFileSystem();
2019-01-06 02:41:46 +00:00
app.logger = new Logger('angel_websocket');
2018-07-10 16:54:55 +00:00
// This is a plug-in. It hooks all your services,
// to automatically broadcast events.
await app.configure(ws.configureServer);
2018-12-09 16:40:09 +00:00
app.get('/', (req, res) => res.streamFile(fs.file('example/index.html')));
2018-07-10 16:54:55 +00:00
// Listen for requests at `/ws`.
2018-12-09 16:40:09 +00:00
app.get('/ws', ws.handleRequest);
app.fallback((req, res) => throw AngelHttpException.notFound());
ws.onConnection.listen((socket) {
socket.onData.listen((x) {
socket.send('pong', x);
});
});
if (args.contains('http2')) {
var ctx = new 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,
);
}
2018-07-10 16:54:55 +00:00
2018-12-09 16:40:09 +00:00
var http2 = new AngelHttp2(app, ctx);
http2.onHttp1.forEach(http.handleRequest);
await http2.startServer('127.0.0.1', 3000);
print('Listening at ${http2.uri}');
} else {
await http.startServer('127.0.0.1', 3000);
print('Listening at ${http.uri}');
}
2018-07-10 16:54:55 +00:00
}