코드 정리

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

View File

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