I am adding CORS support to my OpenWhisk/IBM Cloud function. But after making the modification ( -a web-custom-options true ) to the function I noticed a decline in performance. To isolate the issue I created a simple function, see below:
public static JsonObject main(JsonObject args) throws IOException {
String method = args.get("__ow_method").getAsString();
System.out.println(method+" handle");
if (method.equalsIgnoreCase("OPTIONS")) {
JsonObject responseJSON = new JsonObject();
//add CORS headers
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
responseJSON.add("headers", headers);
responseJSON.addProperty("statusCode", 200);
return responseJSON;
} else {
JsonObject responseJSON = new JsonObject();
JsonObject headers = new JsonObject();
headers.addProperty("Access-Control-Allow-Headers", "*");
headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
headers.addProperty("Access-Control-Allow-Credentials", "true");
headers.addProperty("Content-Type", "application/json");
responseJSON.add("headers", headers);
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
JsonObject answerJSON=new JsonObject();
JsonArray timeArray = new JsonArray();
timeArray.add( "It is "+sdf.format(now));
answerJSON.add("text",timeArray);
responseJSON.add("body",answerJSON );
responseJSON.addProperty("statusCode", 200);
return responseJSON;
}
}
There a two scenario's 1. .http endpoint and web-custom-options true 2. .json endpoint and web-custom-options false
As you can see from the screenshots the difference in OPTIONS is large, but also the difference in the POST replies is notable. Is there something I can do to get CORS support with some performance ? Am I doing something wrong in the JAVA code ?