Add warning message for endpoint switch

This commit is contained in:
Bertold Van den Bergh 2020-01-15 22:03:12 +01:00
parent 44fc1ab2ee
commit 5cdc5c9722

View file

@ -1,5 +1,6 @@
#include <aws/lambda-runtime/runtime.h> #include <aws/lambda-runtime/runtime.h>
#include <string> #include <string>
#include <ctime>
#include "json.hpp" #include "json.hpp"
#include "../library/zonedetect.h" #include "../library/zonedetect.h"
@ -8,6 +9,14 @@ using namespace aws::lambda_runtime;
ZoneDetect* zd; ZoneDetect* zd;
std::string getTime(std::time_t param) {
char tstr[100];
if (std::strftime(tstr, sizeof(tstr), "%F %T %Z", std::localtime(&param))) {
return std::string(tstr);
}
return "";
}
invocation_response zd_handler(invocation_request const& request) { invocation_response zd_handler(invocation_request const& request) {
try { try {
auto body = json::parse(request.payload); auto body = json::parse(request.payload);
@ -19,6 +28,18 @@ invocation_response zd_handler(invocation_request const& request){
float lat = std::stof(param["lat"].get<std::string>(), nullptr); float lat = std::stof(param["lat"].get<std::string>(), nullptr);
float lon = std::stof(param["lon"].get<std::string>(), nullptr); float lon = std::stof(param["lon"].get<std::string>(), nullptr);
json result;
int blocked = 0;
if(param.count("obs")) {
auto obs = static_cast<std::time_t>(std::stol(param["obs"].get<std::string>(), 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;
}
}
if(!blocked) {
int compact = 0; int compact = 0;
if(param.count("c")) { if(param.count("c")) {
compact = std::stoi(param["c"].get<std::string>()); compact = std::stoi(param["c"].get<std::string>());
@ -29,16 +50,14 @@ invocation_response zd_handler(invocation_request const& request){
simple = std::stoi(param["s"].get<std::string>()); simple = std::stoi(param["s"].get<std::string>());
} }
json body;
if(!compact) { if(!compact) {
body["Notice"] = ZDGetNotice(zd); result["Notice"] = ZDGetNotice(zd);
} }
if(simple) { if(simple) {
auto sr = ZDHelperSimpleLookupString(zd, lat, lon); auto sr = ZDHelperSimpleLookupString(zd, lat, lon);
if(sr) { if(sr) {
body["Result"] = sr; result["Result"] = sr;
free(sr); free(sr);
} }
} else { } else {
@ -48,9 +67,9 @@ invocation_response zd_handler(invocation_request const& request){
if(results) { if(results) {
int index = 0; int index = 0;
while(results[index].lookupResult != ZD_LOOKUP_END) { while(results[index].lookupResult != ZD_LOOKUP_END) {
auto& zone = body["Zones"][index]; auto& zone = result["Zones"][index];
zone["Result"] = ZDLookupResultToString(results[index].lookupResult); zone["Result"] = ZDLookupResultToString(results[index].lookupResult);
body["Safezone"] = safezone; result["Safezone"] = safezone;
if(results[index].data) { if(results[index].data) {
for(unsigned int i = 0; i < results[index].numFields; i++) { for(unsigned int i = 0; i < results[index].numFields; i++) {
@ -72,11 +91,13 @@ invocation_response zd_handler(invocation_request const& request){
ZDFreeResults(results); ZDFreeResults(results);
} }
}
json response; json response;
response["statusCode"] = 200; response["statusCode"] = 200;
response["headers"]["Cache-Control"] = "max-age=86400"; response["headers"]["Cache-Control"] = "max-age=86400";
response["headers"]["Access-Control-Allow-Origin"] = "*"; 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"); return invocation_response::success(response.dump(), "application/json");
} }