platform/angel_jael/example/main.dart

42 lines
1.2 KiB
Dart
Raw Normal View History

2017-09-30 23:01:30 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_jael/angel_jael.dart';
2018-06-27 23:57:53 +00:00
import 'package:dart2_constant/convert.dart';
2017-09-30 23:01:30 +00:00
import 'package:file/local.dart';
import 'package:logging/logging.dart';
main() async {
2017-10-01 05:00:14 +00:00
var app = new Angel()..lazyParseBodies = true;
2018-06-27 23:57:53 +00:00
var http = new AngelHttp(app);
2017-09-30 23:01:30 +00:00
var fileSystem = const LocalFileSystem();
await app.configure(
jael(fileSystem.directory('views')),
);
2017-10-01 05:00:14 +00:00
app.get('/',
(res) => res.render('index', {'title': 'Sample App', 'message': null}));
app.post('/', (RequestContext req, res) async {
var body = await req.lazyBody();
2017-10-02 17:29:02 +00:00
print('Body: $body');
2017-10-01 05:00:14 +00:00
var msg = body['message'] ?? '<unknown>';
2017-10-02 17:29:02 +00:00
return await res.render('index', {
'title': 'Form Submission',
'message': msg,
2018-06-27 23:57:53 +00:00
'json_message': json.encode(msg),
2017-10-02 17:29:02 +00:00
});
2017-10-01 05:00:14 +00:00
});
2017-09-30 23:01:30 +00:00
app.use(() => throw new AngelHttpException.notFound());
app.logger = new Logger('angel')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2018-06-27 23:57:53 +00:00
var server = await http.startServer(null, 3000);
2017-09-30 23:01:30 +00:00
print('Listening at http://${server.address.address}:${server.port}');
}