2016-04-22 02:56:21 +00:00
|
|
|
/// This app's route configuration.
|
2017-10-19 21:53:33 +00:00
|
|
|
library angel.src.routes;
|
2016-04-22 02:56:21 +00:00
|
|
|
|
2018-07-11 13:44:21 +00:00
|
|
|
import 'package:angel_cors/angel_cors.dart';
|
2017-10-19 21:53:33 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_static/angel_static.dart';
|
|
|
|
import 'package:file/file.dart';
|
2017-02-13 00:19:00 +00:00
|
|
|
import 'controllers/controllers.dart' as controllers;
|
2016-04-22 02:56:21 +00:00
|
|
|
|
|
|
|
/// Put your app routes here!
|
2017-06-10 15:24:36 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2017-10-19 21:53:33 +00:00
|
|
|
AngelConfigurer configureServer(FileSystem fileSystem) {
|
|
|
|
return (Angel app) async {
|
2018-07-11 13:44:21 +00:00
|
|
|
// Enable CORS
|
|
|
|
app.use(cors());
|
|
|
|
|
2017-10-19 21:53:33 +00:00
|
|
|
// Typically, you want to mount controllers first, after any global middleware.
|
|
|
|
await app.configure(controllers.configureServer);
|
2016-04-22 02:56:21 +00:00
|
|
|
|
2017-10-19 21:53:33 +00:00
|
|
|
// Render `views/hello.jl` when a user visits the application root.
|
|
|
|
app.get(
|
|
|
|
'/', (RequestContext req, ResponseContext res) => res.render('hello'));
|
2017-06-10 15:24:36 +00:00
|
|
|
|
2017-10-19 21:53:33 +00:00
|
|
|
// Mount static server at web in development.
|
2018-07-11 13:44:56 +00:00
|
|
|
// The `CachingVirtualDirectory` variant of `VirtualDirectory` also sends `Cache-Control` headers.
|
2017-06-10 15:24:36 +00:00
|
|
|
//
|
2017-10-19 21:53:33 +00:00
|
|
|
// In production, however, prefer serving static files through NGINX or a
|
|
|
|
// similar reverse proxy.
|
|
|
|
//
|
|
|
|
// 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
|
2018-07-11 13:44:56 +00:00
|
|
|
var vDir = new VirtualDirectory(
|
2017-10-19 21:53:33 +00:00
|
|
|
app,
|
|
|
|
fileSystem,
|
|
|
|
source: fileSystem.directory('web'),
|
|
|
|
);
|
|
|
|
app.use(vDir.handleRequest);
|
|
|
|
|
|
|
|
// Throw a 404 if no route matched the request.
|
|
|
|
app.use(() => throw new AngelHttpException.notFound());
|
|
|
|
|
|
|
|
// Set our application up to handle different errors.
|
|
|
|
//
|
|
|
|
// Read the following for documentation:
|
|
|
|
// * https://github.com/angel-dart/angel/wiki/Error-Handling
|
2018-06-05 23:51:20 +00:00
|
|
|
|
|
|
|
var oldErrorHandler = app.errorHandler;
|
2017-10-19 21:53:33 +00:00
|
|
|
app.errorHandler = (e, req, res) async {
|
2018-06-05 23:51:20 +00:00
|
|
|
if (!req.accepts('text/html'))
|
|
|
|
return await oldErrorHandler(e, req, res);
|
|
|
|
else {
|
|
|
|
if (e.statusCode == 404) {
|
|
|
|
return await res
|
|
|
|
.render('error', {'message': 'No file exists at ${req.path}.'});
|
|
|
|
}
|
2017-10-19 21:53:33 +00:00
|
|
|
|
2018-06-05 23:51:20 +00:00
|
|
|
return await res.render('error', {'message': e.message});
|
|
|
|
}
|
2017-10-19 21:53:33 +00:00
|
|
|
};
|
2016-12-21 19:06:09 +00:00
|
|
|
};
|
2016-06-23 21:54:10 +00:00
|
|
|
}
|