platform/lib/src/routes/routes.dart

62 lines
2 KiB
Dart
Raw Normal View History

2016-04-22 02:56:21 +00:00
/// This app's route configuration.
library angel.routes;
2016-12-21 22:05:11 +00:00
import 'package:angel_common/angel_common.dart';
2017-02-13 00:19:00 +00:00
import 'controllers/controllers.dart' as controllers;
2016-04-22 02:56:21 +00:00
2016-12-05 02:52:45 +00:00
configureBefore(Angel app) async {
app.before.add(cors());
}
2016-06-23 21:54:10 +00:00
2016-04-22 02:56:21 +00:00
/// Put your app routes here!
2016-04-30 00:33:27 +00:00
configureRoutes(Angel app) async {
2016-04-22 02:56:21 +00:00
app.get('/', (req, ResponseContext res) => res.render('hello'));
2016-06-23 21:54:10 +00:00
}
2016-04-22 02:56:21 +00:00
2016-06-23 21:54:10 +00:00
configureAfter(Angel app) async {
2017-02-12 20:20:56 +00:00
// Uncomment this to proxy over pub serve while in development:
// await app.configure(new PubServeLayer());
// Static server at /web or /build/web, depending on if in production
2016-12-21 23:33:31 +00:00
await app.configure(new VirtualDirectory());
2017-01-20 22:46:12 +00:00
2016-12-21 19:06:09 +00:00
// Set our application up to handle different errors.
var errors = new ErrorHandler(handlers: {
404: (req, res) async =>
2017-02-13 00:03:02 +00:00
res.render('error', {'message': 'No file exists at /${req.path}.'}),
2016-12-21 19:06:09 +00:00
500: (req, res) async => res.render('error', {'message': req.error.message})
2016-09-21 06:50:44 +00:00
});
2016-09-21 05:44:56 +00:00
2016-12-21 19:06:09 +00:00
errors.fatalErrorHandler = (AngelFatalError e) async {
2017-01-20 22:46:12 +00:00
var req = await RequestContext.from(e.request, app);
var res = new ResponseContext(e.request.response, app);
res.render('error', {'message': 'Internal Server Error: ${e.error}'});
2017-02-19 12:57:47 +00:00
await app.sendResponse(e.request, req, res);
2016-12-21 19:06:09 +00:00
};
// Throw a 404 if no route matched the request
app.after.add(errors.throwError());
// Handle errors when they occur, based on outgoing status code.
// By default, requests will go through the 500 handler, unless
// they have an outgoing 200, or their status code has a handler
// registered.
app.after.add(errors.middleware());
// Pass AngelHttpExceptions through handler as well
await app.configure(errors);
2016-12-21 20:35:53 +00:00
// Compress via GZIP
2017-01-14 23:52:25 +00:00
// Ideally you'll run this on a `multiserver` instance, but if not,
// feel free to knock yourself out!
//
// app.responseFinalizers.add(gzip());
2016-06-23 21:54:10 +00:00
}
configureServer(Angel app) async {
await configureBefore(app);
await configureRoutes(app);
2017-02-13 00:19:00 +00:00
await app.configure(controllers.configureServer);
2016-06-23 21:54:10 +00:00
await configureAfter(app);
}