코드 정리

This commit is contained in:
freestrings 2019-09-22 22:09:38 +09:00
parent c8ab8ad107
commit c3ac7e40e8
5 changed files with 4564 additions and 72 deletions

4501
benchmark/big_example.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,41 @@
lua_package_path '/etc/jsonpath/?.lua;;'; lua_package_path '/etc/jsonpath/?.lua;;';
access_log /var/log/nginx_access.log; access_log /var/log/access.log;
error_log /var/log/nginx_error.log info; error_log /var/log/error.log info;
lua_shared_dict jsonpaths 1m;
init_by_lua_block { init_by_lua_block {
jp = require("jsonpath") 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") jp.init("/etc/jsonpath/libjsonpath_lib.so")
local jsonpaths = ngx.shared.jsonpaths
BOOK = "$..book" for i, path in ipairs(pathStrings) do
jp.compile(BOOK) jsonpaths:set(i, path)
jp.compile(path)
end
COMMENT_COUNT = "$..commit[?(@.author.name == 'Thibault Charbonnier')]"
jp.compile(COMMENT_COUNT)
} }
server { server {
@ -23,7 +47,7 @@ server {
#gzip_comp_level 6; #gzip_comp_level 6;
#gzip_vary on; #gzip_vary on;
location ~ \.json { location / {
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off; expires off;
@ -31,21 +55,25 @@ server {
root /etc/jsonpath/example; root /etc/jsonpath/example;
} }
location /1 { location /filter {
default_type text/plain;
content_by_lua_file /etc/jsonpath/testa.lua;
}
location /2 {
# https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding
proxy_set_header Accept-Encoding "*"; proxy_set_header Accept-Encoding "*";
default_type 'text/plain'; default_type 'text/plain';
proxy_pass 'http://localhost/example.json'; rewrite /filter/(.*) /$1 break;
proxy_pass http://localhost;
header_filter_by_lua_block { header_filter_by_lua_block {
ngx.header.content_length = nil 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 { body_filter_by_lua_block {
@ -54,46 +82,11 @@ server {
if eof then if eof then
if buf 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 json = buf .. chunk
local template = jp.compile(BOOK)
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
}
}
location /3 {
# https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding
proxy_set_header Accept-Encoding "*";
default_type 'text/plain';
proxy_pass 'https://api.github.com/repos/openresty/lua-nginx-module/commits';
header_filter_by_lua_block {
ngx.header.content_length = nil
}
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 json = buf .. chunk
local template = jp.compile(COMMENT_COUNT)
local result = template(json) local result = template(json)
ngx.arg[1] = result ngx.arg[1] = result
return return

View File

@ -8,13 +8,18 @@ set -v
docker run -d --rm --name jsonpath \ docker run -d --rm --name jsonpath \
-v "${PWD}/../../benchmark/example.json":/etc/jsonpath/example/example.json:ro \ -v "${PWD}/../../benchmark/example.json":/etc/jsonpath/example/example.json:ro \
-v "${PWD}/../../benchmark/big_example.json":/etc/jsonpath/example/big_example.json:ro \
-v "${PWD}/../jsonpath.lua":/etc/jsonpath/jsonpath.lua:ro \ -v "${PWD}/../jsonpath.lua":/etc/jsonpath/jsonpath.lua:ro \
-v "${PWD}/testa.lua":/etc/jsonpath/testa.lua:ro \
-v "${PWD}/init.lua":/etc/jsonpath/init.lua:ro \ -v "${PWD}/init.lua":/etc/jsonpath/init.lua:ro \
-v "${PWD}/../target/release/deps/libjsonpath_lib.so":/etc/jsonpath/libjsonpath_lib.so:ro \ -v "${PWD}/../target/release/deps/libjsonpath_lib.so":/etc/jsonpath/libjsonpath_lib.so:ro \
-v "${PWD}/default.conf":/etc/nginx/conf.d/default.conf \ -v "${PWD}/default.conf":/etc/nginx/conf.d/default.conf \
-p 8080:80 \ -p 8080:80 \
openresty/openresty:bionic openresty/openresty:bionic
sleep 1 #for i in {1..16}; do
curl http://localhost:8080/3 # curl http://localhost:8080/filter/example.json?path=${i}
# echo
#done
#ab -n 1000 -c 10 http://localhost:8080/filter/big_example.json?path=17
#ab -n 1000 -c 10 http://localhost:8080/filter/big_example.json?path=18

View File

@ -1,14 +0,0 @@
require("init")
local jsonpath = require("jsonpath")
local args = ngx.req.get_uri_args()
local path = args['path']
if(path == nil or path == '') then
return ngx.exit(400)
end
local template = jsonpath.compile(path)
local data = ngx.location.capture("/example.json")
local result = template(data.body)
ngx.say(result)

View File

@ -35,8 +35,15 @@ function module.compile(path)
cache[path] = jsonpath.ffi_path_compile(path) cache[path] = jsonpath.ffi_path_compile(path)
_ngx.log(_ngx.INFO, 'compile : [' .. path .. ']') _ngx.log(_ngx.INFO, 'compile : [' .. path .. ']')
end end
end
function module.exec(path)
local compiledPath = cache[path] local compiledPath = cache[path]
if(cache[path] == nil) then
assert(jsonpath, path .. ": is not compiled")
end
return function(jsonStr) return function(jsonStr)
local result = jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr) local result = jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr)
return ffi.string(result); return ffi.string(result);