jsonpath/lua/docker_example/default.conf

107 lines
2.8 KiB
Plaintext
Raw Normal View History

2019-08-23 22:09:09 +09:00
lua_package_path '/etc/jsonpath/?.lua;;';
2019-09-22 22:09:38 +09:00
access_log /var/log/access.log;
error_log /var/log/error.log info;
lua_shared_dict jsonpaths 1m;
2019-08-23 22:09:09 +09:00
2019-09-08 15:37:21 +09:00
init_by_lua_block {
2019-09-22 22:09:38 +09:00
local pathStrings = {
"$.store.book[*].author",
"$..author",
"$.store.*",
"$.store..price",
"$..book[2]",
"$..book[-2]",
"$..book[0,1]",
"$..book[:2]",
"$..book[1:2]",
"$..book[-2:]",
"$..book[2:]",
"$..book[?(@.isbn)]",
"$.store.book[?(@.price == 10)]",
"$..*",
"$..book[ ?( (@.price < 13 || $.store.bicycle.price < @.price) && @.price <=10 ) ]",
"$.store.book[?( (@.price < 10 || @.price > 10) && @.price > 10 )]",
"$..[?(@.originPrice > 1)]",
"$.pickBanner[?(@.originPrice > 1)]"
}
local jp = require("jsonpath")
2019-09-08 15:37:21 +09:00
jp.init("/etc/jsonpath/libjsonpath_lib.so")
2019-09-22 22:09:38 +09:00
local jsonpaths = ngx.shared.jsonpaths
2019-09-08 15:37:21 +09:00
2019-09-22 22:09:38 +09:00
for i, path in ipairs(pathStrings) do
jsonpaths:set(i, path)
jp.compile(path)
end
2019-09-08 15:37:21 +09:00
}
2019-08-23 22:09:09 +09:00
server {
2019-09-08 15:37:21 +09:00
listen 80;
server_name localhost;
gzip on;
gzip_types text/plain application/json;
#gzip_comp_level 6;
#gzip_vary on;
2019-09-22 22:09:38 +09:00
location / {
2019-09-08 15:37:21 +09:00
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
default_type 'text/plain';
root /etc/jsonpath/example;
2019-08-23 22:09:09 +09:00
}
2019-09-22 22:09:38 +09:00
location /filter {
2019-09-08 15:37:21 +09:00
# https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding
proxy_set_header Accept-Encoding "*";
default_type 'text/plain';
2019-09-22 22:09:38 +09:00
rewrite /filter/(.*) /$1 break;
proxy_pass http://localhost;
2019-09-08 15:37:21 +09:00
header_filter_by_lua_block {
2019-09-22 22:09:38 +09:00
ngx.header["content-length"] = nil
2019-09-08 15:37:21 +09:00
2019-09-22 22:09:38 +09:00
local args = ngx.req.get_uri_args()
local jsonpaths = ngx.shared.jsonpaths
local path = jsonpaths:get(args['path'])
2019-09-08 15:37:21 +09:00
2019-09-22 22:09:38 +09:00
if path == nil then
ngx.exit(ngx.HTTP_BAD_REQUEST)
2019-09-08 15:37:21 +09:00
end
}
body_filter_by_lua_block {
local chunk, eof = ngx.arg[1], ngx.arg[2]
local buf = ngx.ctx.buf
if eof then
if buf then
2019-09-22 22:09:38 +09:00
local args = ngx.req.get_uri_args()
local path = ngx.shared.jsonpaths:get(args['path'])
local jsonpath = require("jsonpath")
local template = jsonpath.exec(path)
2019-09-08 15:37:21 +09:00
local json = buf .. chunk
local result = template(json)
ngx.arg[1] = result
return
end
return
end
if buf then
ngx.ctx.buf = buf .. chunk
else
ngx.ctx.buf = chunk
end
ngx.arg[1] = nil
}
2019-08-23 22:09:09 +09:00
}
}