I want to make a logger that will log certain information from every request to the database. I know how to make a controller and I could link it to every route like this:
@override
Controller get entryPoint {
final router = Router();
router
.route('/register')
.link(() => LogController(context))
.link(() => RegisterController(context, authServer));
router
.route('/auth/token')
.link(() => LogController(context))
.link(() => AuthController(authServer));
router
.route('/logout')
.link(() => LogController(context))
.link(() => Authorizer.bearer(authServer))
.link(() => LogoutController(context));
router
.route('/words/[:id]')
.link(() => LogController(context))
.link(() => Authorizer.bearer(authServer))
.link(() => WordsController(context));
return router;
}
As you can see, the LogController
is repeated for every route.
I would rather just match any route from a single location, log the data, and then let the other controllers handle things as normal. Is that possible?
Sure, return a LogController linked to a Router as your entry point:
@override
Controller get entryPoint {
final router = Router();
/* configure Router as you are today */
// Notice the cascade operator... LogController instance is returned
return LogController(context)..link(() => router);
}