From 5cdc5c9722e40f38810e68d87658a41d9d74211b Mon Sep 17 00:00:00 2001 From: Bertold Van den Bergh Date: Wed, 15 Jan 2020 22:03:12 +0100 Subject: [PATCH] Add warning message for endpoint switch --- aws_lambda/main.cpp | 123 ++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 51 deletions(-) diff --git a/aws_lambda/main.cpp b/aws_lambda/main.cpp index c0ef405..491cdd1 100644 --- a/aws_lambda/main.cpp +++ b/aws_lambda/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "json.hpp" #include "../library/zonedetect.h" @@ -8,85 +9,105 @@ using namespace aws::lambda_runtime; ZoneDetect* zd; -invocation_response zd_handler(invocation_request const& request){ - try{ +std::string getTime(std::time_t param) { + char tstr[100]; + if (std::strftime(tstr, sizeof(tstr), "%F %T %Z", std::localtime(¶m))) { + return std::string(tstr); + } + return ""; +} + +invocation_response zd_handler(invocation_request const& request) { + try { auto body = json::parse(request.payload); - if(body.count("queryStringParameters")){ + if(body.count("queryStringParameters")) { auto param = body["queryStringParameters"]; - - if(param.count("lat") && param.count("lon")){ + + if(param.count("lat") && param.count("lon")) { float lat = std::stof(param["lat"].get(), nullptr); float lon = std::stof(param["lon"].get(), nullptr); - int compact = 0; - if(param.count("c")){ - compact = std::stoi(param["c"].get()); - } - - int simple = 0; - if(param.count("s")){ - simple = std::stoi(param["s"].get()); + json result; + int blocked = 0; + + if(param.count("obs")) { + auto obs = static_cast(std::stol(param["obs"].get(), nullptr)); + result["Warning"] = "You are accessing this API on an unsupported endpoint. Please use http[s]://timezone.bertold.org/timezone instead. This endpoint will stop responding on " + getTime(obs); + if(time(NULL) >= obs) { + blocked = 1; + } } - json body; - - if(!compact){ - body["Notice"] = ZDGetNotice(zd); - } - - if(simple){ - auto sr = ZDHelperSimpleLookupString(zd, lat, lon); - if(sr){ - body["Result"] = sr; - free(sr); + if(!blocked) { + int compact = 0; + if(param.count("c")) { + compact = std::stoi(param["c"].get()); } - }else{ - float safezone = 0; - auto results = ZDLookup(zd, lat, lon, &safezone); - - if(results){ - int index = 0; - while(results[index].lookupResult != ZD_LOOKUP_END) { - auto& zone = body["Zones"][index]; - zone["Result"] = ZDLookupResultToString(results[index].lookupResult); - body["Safezone"] = safezone; - - if(results[index].data) { - for(unsigned int i = 0; i < results[index].numFields; i++) { - if(results[index].fieldNames[i] && results[index].data[i]) { - zone[results[index].fieldNames[i]] = results[index].data[i]; + + int simple = 0; + if(param.count("s")) { + simple = std::stoi(param["s"].get()); + } + + if(!compact) { + result["Notice"] = ZDGetNotice(zd); + } + + if(simple) { + auto sr = ZDHelperSimpleLookupString(zd, lat, lon); + if(sr) { + result["Result"] = sr; + free(sr); + } + } else { + float safezone = 0; + auto results = ZDLookup(zd, lat, lon, &safezone); + + if(results) { + int index = 0; + while(results[index].lookupResult != ZD_LOOKUP_END) { + auto& zone = result["Zones"][index]; + zone["Result"] = ZDLookupResultToString(results[index].lookupResult); + result["Safezone"] = safezone; + + if(results[index].data) { + for(unsigned int i = 0; i < results[index].numFields; i++) { + if(results[index].fieldNames[i] && results[index].data[i]) { + zone[results[index].fieldNames[i]] = results[index].data[i]; + } + } + + if(zone.count("TimezoneId") && zone.count("TimezoneIdPrefix")) { + zone["TimezoneId"] = zone["TimezoneIdPrefix"].get() + zone["TimezoneId"].get(); + zone.erase("TimezoneIdPrefix"); } } - if(zone.count("TimezoneId") && zone.count("TimezoneIdPrefix")){ - zone["TimezoneId"] = zone["TimezoneIdPrefix"].get() + zone["TimezoneId"].get(); - zone.erase("TimezoneIdPrefix"); - } + index++; } - - index++; } + + ZDFreeResults(results); } - ZDFreeResults(results); } json response; response["statusCode"] = 200; response["headers"]["Cache-Control"] = "max-age=86400"; response["headers"]["Access-Control-Allow-Origin"] = "*"; - response["body"] = body.dump(compact?0:2); - + response["body"] = result.dump(compact?0:2); + return invocation_response::success(response.dump(), "application/json"); } } - }catch(...){} - + } catch(...) {} + return invocation_response::failure("Error", "Error"); } -int main(){ +int main() { zd = ZDOpenDatabase("timezone21.bin"); if(!zd) { return 1;