lua_package_path '/etc/jsonpath/?.lua;;'; access_log /var/log/access.log; error_log /var/log/error.log info; lua_shared_dict jsonpaths 1m; init_by_lua_block { 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") jp.init("/etc/jsonpath/libjsonpath_lib.so") local jsonpaths = ngx.shared.jsonpaths for i, path in ipairs(pathStrings) do jsonpaths:set(i, path) jp.compile(path) end } server { listen 80; server_name localhost; gzip on; gzip_types text/plain application/json; #gzip_comp_level 6; #gzip_vary on; location / { 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; } location /filter { # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding proxy_set_header Accept-Encoding "*"; default_type 'text/plain'; rewrite /filter/(.*) /$1 break; proxy_pass http://localhost; header_filter_by_lua_block { ngx.header["content-length"] = nil local args = ngx.req.get_uri_args() local jsonpaths = ngx.shared.jsonpaths local path = jsonpaths:get(args['path']) if path == nil then ngx.exit(ngx.HTTP_BAD_REQUEST) 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 local args = ngx.req.get_uri_args() local path = ngx.shared.jsonpaths:get(args['path']) local jsonpath = require("jsonpath") local template = jsonpath.exec(path) 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 } } }