mirror of
https://github.com/BertoldVdb/ZoneDetect.git
synced 2026-04-27 14:57:40 +00:00
Add warning message for endpoint switch
This commit is contained in:
parent
44fc1ab2ee
commit
5cdc5c9722
1 changed files with 72 additions and 51 deletions
|
|
@ -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,85 +9,105 @@ using namespace aws::lambda_runtime;
|
||||||
|
|
||||||
ZoneDetect* zd;
|
ZoneDetect* zd;
|
||||||
|
|
||||||
invocation_response zd_handler(invocation_request const& request){
|
std::string getTime(std::time_t param) {
|
||||||
try{
|
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);
|
auto body = json::parse(request.payload);
|
||||||
|
|
||||||
if(body.count("queryStringParameters")){
|
if(body.count("queryStringParameters")) {
|
||||||
auto param = body["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<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);
|
||||||
|
|
||||||
int compact = 0;
|
json result;
|
||||||
if(param.count("c")){
|
int blocked = 0;
|
||||||
compact = std::stoi(param["c"].get<std::string>());
|
|
||||||
}
|
if(param.count("obs")) {
|
||||||
|
auto obs = static_cast<std::time_t>(std::stol(param["obs"].get<std::string>(), nullptr));
|
||||||
int simple = 0;
|
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(param.count("s")){
|
if(time(NULL) >= obs) {
|
||||||
simple = std::stoi(param["s"].get<std::string>());
|
blocked = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
json body;
|
if(!blocked) {
|
||||||
|
int compact = 0;
|
||||||
if(!compact){
|
if(param.count("c")) {
|
||||||
body["Notice"] = ZDGetNotice(zd);
|
compact = std::stoi(param["c"].get<std::string>());
|
||||||
}
|
|
||||||
|
|
||||||
if(simple){
|
|
||||||
auto sr = ZDHelperSimpleLookupString(zd, lat, lon);
|
|
||||||
if(sr){
|
|
||||||
body["Result"] = sr;
|
|
||||||
free(sr);
|
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
float safezone = 0;
|
int simple = 0;
|
||||||
auto results = ZDLookup(zd, lat, lon, &safezone);
|
if(param.count("s")) {
|
||||||
|
simple = std::stoi(param["s"].get<std::string>());
|
||||||
if(results){
|
}
|
||||||
int index = 0;
|
|
||||||
while(results[index].lookupResult != ZD_LOOKUP_END) {
|
if(!compact) {
|
||||||
auto& zone = body["Zones"][index];
|
result["Notice"] = ZDGetNotice(zd);
|
||||||
zone["Result"] = ZDLookupResultToString(results[index].lookupResult);
|
}
|
||||||
body["Safezone"] = safezone;
|
|
||||||
|
if(simple) {
|
||||||
if(results[index].data) {
|
auto sr = ZDHelperSimpleLookupString(zd, lat, lon);
|
||||||
for(unsigned int i = 0; i < results[index].numFields; i++) {
|
if(sr) {
|
||||||
if(results[index].fieldNames[i] && results[index].data[i]) {
|
result["Result"] = sr;
|
||||||
zone[results[index].fieldNames[i]] = results[index].data[i];
|
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<std::string>() + zone["TimezoneId"].get<std::string>();
|
||||||
|
zone.erase("TimezoneIdPrefix");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(zone.count("TimezoneId") && zone.count("TimezoneIdPrefix")){
|
index++;
|
||||||
zone["TimezoneId"] = zone["TimezoneIdPrefix"].get<std::string>() + zone["TimezoneId"].get<std::string>();
|
|
||||||
zone.erase("TimezoneIdPrefix");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(...){}
|
} catch(...) {}
|
||||||
|
|
||||||
return invocation_response::failure("Error", "Error");
|
return invocation_response::failure("Error", "Error");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main() {
|
||||||
zd = ZDOpenDatabase("timezone21.bin");
|
zd = ZDOpenDatabase("timezone21.bin");
|
||||||
if(!zd) {
|
if(!zd) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue