platform/packages/shelf/example/angel_in_shelf.dart

54 lines
1.8 KiB
Dart
Raw Normal View History

2019-10-14 16:38:30 +00:00
import 'dart:io';
2021-09-14 01:23:03 +00:00
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_shelf/angel3_shelf.dart';
2019-10-14 16:38:30 +00:00
import 'package:logging/logging.dart';
2021-09-14 01:23:03 +00:00
import 'package:angel3_pretty_logging/angel3_pretty_logging.dart';
2019-10-14 16:38:30 +00:00
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_static/shelf_static.dart';
2021-06-20 12:37:20 +00:00
void main() async {
2019-10-14 16:38:30 +00:00
Logger.root
..level = Level.ALL
..onRecord.listen(prettyLog);
// Create a basic Angel server, with some routes.
var app = Angel(
2021-09-14 01:23:03 +00:00
logger: Logger('angel3_shelf_demo'),
2019-10-14 16:38:30 +00:00
reflector: MirrorsReflector(),
);
app.get('/angel', (req, res) {
res.write('Angel embedded within shelf!');
return false;
});
app.get('/hello', ioc((@Query('name') String name) {
return {'hello': name};
}));
// Next, create an AngelShelf driver.
2019-10-14 16:45:37 +00:00
//
2019-10-14 16:38:30 +00:00
// If we have startup hooks we want to run, we need to call
// `startServer`. Otherwise, it can be omitted.
2019-10-14 16:45:37 +00:00
// Of course, if you call `startServer`, know that to run
// shutdown/cleanup logic, you need to call `close` eventually,
// too.
2019-10-14 16:38:30 +00:00
var angelShelf = AngelShelf(app);
await angelShelf.startServer();
// Create, and mount, a shelf pipeline...
// You can also embed Angel as a middleware...
var mwHandler = shelf.Pipeline()
.addMiddleware(angelShelf.middleware)
.addHandler(createStaticHandler('.',
defaultDocument: 'index.html', listDirectories: true));
// Run the servers.
await shelf_io.serve(mwHandler, InternetAddress.loopbackIPv4, 8080);
await shelf_io.serve(angelShelf.handler, InternetAddress.loopbackIPv4, 8081);
print('Angel as middleware: http://localhost:8080');
print('Angel as only handler: http://localhost:8081');
}