httpRequestHandler function
this is a class which get matched routes and
pass http request to route middleware and controllers
and response from controllers is passed to httpResponseHandler
Implementation
Future<void> httpRequestHandler(HttpRequest req) async {
try {
// preflight
if (req.method == 'OPTIONS') {
req.response.statusCode = HttpStatus.ok;
await req.response.close();
return;
}
String? domain = req.headers.value('host');
/// getting matched route
RouteData? route = _getMatchRoute(req.uri.path, req.method, domain);
if (route == null) {
await _routeNotFound(req);
return;
}
/// convert http request into DoxRequest
/// we did not use constructor here because we require
/// async await to get body string from HttpRequest.
DoxRequest doxReq = await DoxRequest.httpRequestToDoxRequest(req, route);
/// route.controllers will be always list
/// see Route()._addRoute() for explanation
await _handleMiddlewareController(route, doxReq, req);
return;
} catch (error, stackTrace) {
if (error is Exception || error is Error) {
DoxLogger.warn(error);
DoxLogger.danger(stackTrace.toString());
}
httpResponseHandler(error, req);
return;
}
}