platform/lib/src/routes/routes.dart

139 lines
5 KiB
Dart
Raw Normal View History

2016-04-22 02:56:21 +00:00
/// This app's route configuration.
library angel.routes;
import 'dart:io';
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
/// Adds global middleware to the application.
///
/// Use these to apply functionality to requests before business logic is run.
///
/// More on the request lifecycle:
/// https://github.com/angel-dart/angel/wiki/Request-Lifecycle
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!
///
/// See the wiki for information about routing, requests, and responses:
/// * https://github.com/angel-dart/angel/wiki/Basic-Routing
/// * https://github.com/angel-dart/angel/wiki/Requests-&-Responses
2016-04-30 00:33:27 +00:00
configureRoutes(Angel app) async {
// Render `views/hello.mustache` when a user visits the application root.
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
/// Configures fallback middleware.
///
/// Use these to run generic actions on requests that were not terminated by
/// earlier request handlers.
///
/// Note that these middleware might not always run.
///
/// More on the request lifecycle: https://github.com/angel-dart/angel/wiki/Request-Lifecycle
2016-06-23 21:54:10 +00:00
configureAfter(Angel app) async {
// Uncomment this to proxy over `pub serve` while in development.
// This is a useful feature for full-stack applications, especially if you
// are using Angular2.
//
// For documentation, see `package:angel_proxy`:
// https://github.com/angel-dart/proxy
//
2017-02-12 20:20:56 +00:00
// await app.configure(new PubServeLayer());
// Mount static server at /web or /build/web, depending on if
// you are running in production mode. `Cache-Control` headers will also be enabled.
2017-02-27 00:21:52 +00:00
//
// Read the following two sources for documentation:
// * https://medium.com/the-angel-framework/serving-static-files-with-the-angel-framework-2ddc7a2b84ae
// * https://github.com/angel-dart/static
2017-02-27 00:21:52 +00:00
await app.configure(new CachingVirtualDirectory());
2017-01-20 22:46:12 +00:00
2016-12-21 19:06:09 +00:00
// Set our application up to handle different errors.
//
// Read the following two sources for documentation:
// * https://github.com/angel-dart/angel/wiki/Error-Handling
// * https://github.com/angel-dart/errors
2016-12-21 19:06:09 +00:00
var errors = new ErrorHandler(handlers: {
// Display a 404 page if no resource is found.
2016-12-21 19:06:09 +00:00
404: (req, res) async =>
2017-02-25 21:11:22 +00:00
res.render('error', {'message': 'No file exists at ${req.path}.'}),
// On generic errors, give information about why the application failed.
//
// An `AngelHttpException` instance will be present in `req.properties`
// as `error`.
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
// Use a fatal error handler to attempt to resolve any issues that
// result in Angel not being able to send the user a response.
2016-12-21 19:06:09 +00:00
errors.fatalErrorHandler = (AngelFatalError e) async {
try {
// Manually create a request and response context.
var req = await RequestContext.from(e.request, app);
var res = new ResponseContext(e.request.response, app);
// *Attempt* to render an error page.
res.render('error', {'message': 'Internal Server Error: ${e.error}'});
await app.sendResponse(e.request, req, res);
} catch (_) {
// If execution fails here, there is nothing we can do.
stderr..writeln('Fatal error: ${e.error}')..writeln(e.stack);
}
2016-12-21 19:06:09 +00:00
};
// Throw a 404 if no route matched the request.
app.after.add(() => throw new AngelHttpException.notFound());
2016-12-21 19:06:09 +00:00
// 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.
2017-01-14 23:52:25 +00:00
//
// Again, here is the error handling documentation:
// * https://github.com/angel-dart/angel/wiki/Error-Handling
// * https://github.com/angel-dart/errors
await app.configure(errors);
2016-06-23 21:54:10 +00:00
}
/// Adds response finalizers to the application.
///
/// These run after every request.
///
/// See more on the request lifecycle here:
/// https://github.com/angel-dart/angel/wiki/Request-Lifecycle
configureFinalizers(Angel app) async {}
/// Adds routes to our application.
///
/// See the wiki for information about routing, requests, and responses:
/// * https://github.com/angel-dart/angel/wiki/Basic-Routing
/// * https://github.com/angel-dart/angel/wiki/Requests-&-Responses
2016-06-23 21:54:10 +00:00
configureServer(Angel app) async {
// The order in which we run these plug-ins is relatively significant.
// Try not to change it.
// Add global middleware.
2016-06-23 21:54:10 +00:00
await configureBefore(app);
// Typically, you want to mount controllers first, after any global middleware.
2017-02-13 00:19:00 +00:00
await app.configure(controllers.configureServer);
// Next, you can add any supplemental routes.
2017-05-27 09:33:19 +00:00
await configureRoutes(app);
// Add handlers to run after requests are handled.
//
// See the request lifecycle docs to find out why these two
// are separate:
// https://github.com/angel-dart/angel/wiki/Request-Lifecycle
2016-06-23 21:54:10 +00:00
await configureAfter(app);
await configureFinalizers(app);
2016-06-23 21:54:10 +00:00
}