telize

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

commit ee628165cf8fcd766747e32dd8971260f96ea33f
parent e7e1b39ccc5b66aeda5c70a8c7e51e79618cc36e
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 24 Oct 2016 16:41:01 +0200

Use 'content_by_lua_block' directive instead of 'content_by_lua'

Diffstat:
MTODO | 2+-
Mtelize | 162++++++++++++++++++++++++++++++++++++++++---------------------------------------
2 files changed, 84 insertions(+), 80 deletions(-)

diff --git a/TODO b/TODO @@ -1,4 +1,4 @@ - Document time zone offsets : http://en.wikipedia.org/wiki/List_of_UTC_time_offsets - Force UTF-8 encoding of GeoIP variables directly in Nginx to drop dependency on Lua iconv module -- Use 'content_by_lua_block' directive instead of 'content_by_lua' + diff --git a/telize b/telize @@ -34,27 +34,30 @@ server { charset off; default_type text/plain; - content_by_lua 'ngx.say(ngx.var.remote_addr)'; + content_by_lua_block { + ngx.say(ngx.var.remote_addr) + } } location ~ /jsonip$ { charset utf-8; default_type application/json; - content_by_lua ' - local cjson = require("cjson") + content_by_lua_block { + local cjson = require("cjson") - local json = cjson.encode({ - ip = ngx.var.remote_addr - }) + local json = cjson.encode({ + ip = ngx.var.remote_addr + }) - local callback = ngx.var.arg_callback + local callback = ngx.var.arg_callback - if callback then - ngx.say(callback, "(", json, ");") - else - ngx.say(json) - end'; + if callback then + ngx.say(callback, "(", json, ");") + else + ngx.say(json) + end + } } location ~ /geoip/?(?<ip>.*) { @@ -82,75 +85,76 @@ server { add_header Access-Control-Allow-Origin $cors_origin; } - content_by_lua ' - local cjson = require("cjson") - local iconv = require("iconv") - local cd = iconv.new("utf-8","iso-8859-15") - - ngx.header["Cache-Control"] = "no-cache"; - - -- Check for invalid IP addresses - if ngx.var.remote_addr == "127.0.0.1" then - ngx.status = ngx.HTTP_BAD_REQUEST - ngx.say(cjson.encode({code = 401, message = "Input string is not a valid IP address"})) - ngx.exit(ngx.HTTP_OK) - end - - local payload = { - ip = ngx.var.remote_addr, - country_code = ngx.var.geoip_city_country_code, - country_code3 = ngx.var.geoip_city_country_code3, - country = ngx.var.geoip_city_country_name, - region = ngx.var.geoip_region_name, - region_code = ngx.var.geoip_region, - city = ngx.var.geoip_city, - postal_code = ngx.var.geoip_postal_code, - continent_code = ngx.var.geoip_city_continent_code, - latitude = ngx.var.geoip_latitude, - longitude = ngx.var.geoip_longitude, - dma_code = ngx.var.geoip_dma_code, - area_code = ngx.var.geoip_area_code, - organization = ngx.var.geoip_org, - timezone = ngx.var.geoip_timezone, - offset = ngx.var.geoip_timezone_offset, - } + content_by_lua_block { + local cjson = require("cjson") + local iconv = require("iconv") + local cd = iconv.new("utf-8","iso-8859-15") + + ngx.header["Cache-Control"] = "no-cache"; + + -- Check for invalid IP addresses + if ngx.var.remote_addr == "127.0.0.1" then + ngx.status = ngx.HTTP_BAD_REQUEST + ngx.say(cjson.encode({code = 401, message = "Input string is not a valid IP address"})) + ngx.exit(ngx.HTTP_OK) + end + + local payload = { + ip = ngx.var.remote_addr, + country_code = ngx.var.geoip_city_country_code, + country_code3 = ngx.var.geoip_city_country_code3, + country = ngx.var.geoip_city_country_name, + region = ngx.var.geoip_region_name, + region_code = ngx.var.geoip_region, + city = ngx.var.geoip_city, + postal_code = ngx.var.geoip_postal_code, + continent_code = ngx.var.geoip_city_continent_code, + latitude = ngx.var.geoip_latitude, + longitude = ngx.var.geoip_longitude, + dma_code = ngx.var.geoip_dma_code, + area_code = ngx.var.geoip_area_code, + organization = ngx.var.geoip_org, + timezone = ngx.var.geoip_timezone, + offset = ngx.var.geoip_timezone_offset, + } + + local callback = ngx.var.arg_callback + + -- Validate payload + for item, value in pairs(payload) do + if payload[item] == "" then + payload[item] = nil + end + end - local callback = ngx.var.arg_callback + -- Convert latitude and longitude to numeric values + if payload.latitude ~= nil and payload.longitude ~= nil then + payload.latitude = tonumber(payload.latitude) + payload.longitude = tonumber(payload.longitude) + end + + -- Convert city name to UTF-8 if it exists + if payload.city ~= nil then + payload.city = cd:iconv(payload.city) + end + + -- Convert region name to UTF-8 if it exists + if payload.region ~= nil then + payload.region = cd:iconv(payload.region) + end - -- Validate payload - for item, value in pairs(payload) do - if payload[item] == "" then - payload[item] = nil + -- Convert organization name to UTF-8 if it exists + if payload.organization ~= nil then + payload.organization = cd:iconv(payload.organization) end - end - - -- Convert latitude and longitude to numeric values - if payload.latitude ~= nil and payload.longitude ~= nil then - payload.latitude = tonumber(payload.latitude) - payload.longitude = tonumber(payload.longitude) - end - - -- Convert city name to UTF-8 if it exists - if payload.city ~= nil then - payload.city = cd:iconv(payload.city) - end - - -- Convert region name to UTF-8 if it exists - if payload.region ~= nil then - payload.region = cd:iconv(payload.region) - end - - -- Convert organization name to UTF-8 if it exists - if payload.organization ~= nil then - payload.organization = cd:iconv(payload.organization) - end - - local json = cjson.encode(payload) - - if callback ~= "" then - ngx.say(callback, "(", json, ");") - else - ngx.say(json) - end'; + + local json = cjson.encode(payload) + + if callback ~= "" then + ngx.say(callback, "(", json, ");") + else + ngx.say(json) + end + } } }