telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit 7ed0282fde2b2352ec81dec2b5b182f00532f236
parent 55c1a816453c914282d859679a4e45105ea91d27
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri, 12 Oct 2018 22:49:50 +0200

Add a getdata() function to get DB fields and append them to the JSON payload

Diffstat:
Msrc/location.c | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/src/location.c b/src/location.c @@ -24,11 +24,16 @@ #include "location.h" +#define ENTRY_TYPE_UINT32 0 +#define ENTRY_TYPE_STRING 1 +#define ENTRY_TYPE_DOUBLE 2 + MMDB_s asn; MMDB_s city; int init(int); int location(struct http_request *); +void getdata(struct kore_buf *, MMDB_lookup_result_s *, MMDB_entry_data_s *, char *, int, ...); int init(int state) @@ -50,6 +55,30 @@ init(int state) return (KORE_RESULT_OK); } +void +getdata(struct kore_buf *json, MMDB_lookup_result_s *lookup, MMDB_entry_data_s *entry_data, char *field, int type, ...) +{ + va_list keys; + va_start(keys, entry_data); + + MMDB_vget_value(&lookup->entry, entry_data, keys); + if (entry_data->has_data) { + switch(type) { + case ENTRY_TYPE_UINT32: + kore_buf_appendf(json, ",\"%s\":%d", field, entry_data->uint32); + break; + case ENTRY_TYPE_STRING: + kore_buf_appendf(json, ",\"%s\":\"%.*s\"", field, entry_data->data_size, entry_data->utf8_string); + break; + case ENTRY_TYPE_DOUBLE: + kore_buf_appendf(json, ",\"%s\":%.4f", field, entry_data->double_value); + break; + } + } + + va_end(keys); +} + int location(struct http_request *req) {