handleRequest method Null safety
- Request request
Main method to handle the Requests. Requires a http Request (from the Shelf package) and based on the method requested calls the appropriate function.
Implementation
FutureOr<Response> handleRequest(Request request) {
try {
final mimeType = request.mimeType;
final method = request.method.toUpperCase();
if (method == 'GET') {
if (request.url.pathSegments.last == config.statics) {
return createStaticHandler(
config.statics,
defaultDocument: 'index.html',
)(request);
} else if (request.url.pathSegments.last == config.storage?.folder) {
return createStaticHandler(config.storage!.folder)(request);
} else if (request.url.pathSegments.last == 'auth') {
return handleAuth(request);
} else {
return handleGet(request);
}
} else if (method == 'DELETE') {
return handleDelete(request);
} else if (method == 'POST' && mimeType == 'application/json') {
return handlePost(request);
} else if (method == 'POST' &&
mimeType == 'multipart/form-data' &&
request.url.pathSegments.last == 'storage' &&
config.storage != null) {
return handleUpload(request);
} else if (method == 'PUT' && mimeType == 'application/json') {
return handlePut(request);
} else if (method == 'PATCH' && mimeType == 'application/json') {
return handlePatch(request);
} else {
final body = jsonEncode({
'error': 'Unsupported request: ${request.method}.',
});
return Response(HttpStatus.methodNotAllowed, body: body);
}
} catch (e) {
final body = jsonEncode({
'error': 'Exception: $e.',
});
return Response(HttpStatus.internalServerError, body: body);
}
}