Skip to content

Commit

Permalink
1) tnt_eval has been removed, since it does not work well. It was rep…
Browse files Browse the repository at this point in the history
…laced by ngx_lua and so on, that means this module is optimized for an other nginx's modules like ngx_lua, ngx_perl and so on. 2) tnt_pure_result is back. Reply can be filtered by ngx_lua or ngx_perl. 3) Updated tests. 4) Updated README.
  • Loading branch information
dedok committed Jul 2, 2017
1 parent f634533 commit 3af386f
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 1,372 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ many contributions from the community.
Below follows a list of people, who contributed their code.

Vasily Soshnikov, Andrew Drozdow, E. Blih, Konstantin Osipov, Valery Kholodkov,
Yichun Zhang (eval module, valgrind suppess, debug.h), Alex Turenko
Yichun Zhang (valgrind suppess, debug.h), Alex Turenko

NOTE: If you can commit a change to this list, please do not hesitate
to add your name to it.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ configure-debug:
--prefix=$(PREFIX_PATH) \
--with-http_addition_module \
--add-module=$(MODULE_PATH) \
--add-module=$(MODULE_PATH)/../ngx_devel_kit \
--add-module=$(MODULE_PATH)/../lua-nginx-module \
--with-debug

configure-for-testing: configure-debug
Expand Down
145 changes: 71 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ Tarantool - https://hub.docker.com/r/tarantool/tarantool
* [JSON](#json)
* [Directives](#directives)
* [tnt_pass](#tnt_pass)
* [tnt_eval](#tnt_eval)
* [tnt_eval_buffer_size](#tnt_eval_buffer_size)
* [HTTP headers and status](#HTTP headers and status)
* [tnt_http_methods](#tnt_http_methods)
* [tnt_http_rest_methods](#tnt_http_rest_methods)
* [tnt_pass_http_request](#tnt_pass_http_request)
Expand Down Expand Up @@ -211,7 +210,7 @@ end
[ { "result": JSON_RESULT_OBJECT, "id":UINT, "error": { "message": STR, "code": INT } }, ...N ]


"result" - DEPRECATED in 2.4.0+
"result"

Version 2.4.0+ output a raw result, i.e. "JSON_RESULT_OBJECT".

Expand Down Expand Up @@ -253,11 +252,11 @@ end

rpc call 1:
--> { "method": "echo", "params": [42, 23], "id": 1 }
<-- [42, 23]
<-- { "id": 1, "result": [42, 23]

rpc call 2:
--> { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
<-- [ {"hello": "world"} ], "!" ]
<-- { "id": 2, "result": [ {"hello": "world"} ], "!" ]}

rpc call of a non-existent method:
--> { "method": "echo_2", "id": 1 }
Expand All @@ -273,8 +272,8 @@ end
{ "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
]
<-- [
[42, 23],
[{"hello": "world"} ], "!" ],
{ "id": 1, "result": [42, 23]},
{ "id": 2, "result" : [{"hello": "world"} ], "!" ]},
]

rpc call Batch of a non-existent method:
Expand All @@ -284,7 +283,7 @@ end
]
<-- [
{ "error": {"code": -32601, "message": "Method not found"}, "id": 1 },
[ {"hello": "world"} ], "!" ]
{"id": 2, "result": [ {"hello": "world"} ], "!" ]}
]

rpc call Batch with invalid JSON:
Expand Down Expand Up @@ -327,27 +326,18 @@ Specify the Tarantool server backend.

[Back to content](#content)

tnt_eval
--------
**syntax:** *tnt_eval $HTTP_STATUS_VAR_NAME $HTTP_BODY_VAR_NAME*
HTTP headers and status
-----------------------

**default:** *no*

**context:** *location*

This directive put execution of tnt_pass into the nginx REWRITE PHASE.
That exactly this mean? That means that you can have a access to the body (in for of
JSON), http codes and http headers which have been passed from the Tarantool
to the nginx inside nginx config. This very useful for setting custom HTTP
statuses, headers and for post-processing of the original body.
Sometimes you have to set status or headers which came from the Tarantool.
For this you have to use something like [ngx_lua](https://github.com/openresty/lua-nginx-module)
or [ngx_perl](http://nginx.org/en/docs/http/ngx_http_perl_module.html) and so on.

Even more, you can use this for using this module with OpenResty, Nginx Script,
Nginx Perl and so on.
Also using the methods you can transform result from the `Tarantool` into
something else

NOTICE!
Here is an example with `ngx_lua`:

1) This directive expects that tarantool returns special object with meta
information about an HTTP status and an HTTP headers.

Example

Expand All @@ -358,7 +348,7 @@ Example
-- First arg. if __ngx exists and tnt_eval is used, then it will be
-- readed by nginx
{
__ngx = {
ngx = {
200, -- set status HTTP 200
{ ["X-Tarantool"] = "FROM_TNT" } -- set headers
}
Expand All @@ -373,60 +363,67 @@ Example
upstream tnt_upstream {
127.0.0.1:9999;
keepalive 10000;
}
location = /tnt {
tnt_eval_buffer_size 1m;
tnt_eval $tnt_http_status $tnt_body {
tnt_method foo;
tnt_pass 127.0.0.1:9999;
}
if ($tnt_http_status = 404) {
return 404 $tnt_body;
}
if ($tnt_body ~= 'Tarantool') {
return 200 '<html><h1>Found Tarantool!</h1></html>';
}
return 200 $tnt_body;
location /tnt_proxy {
tnt_method tnt_proxy;
tnt_buffer_size 100k;
tnt_pass_http_request on parse_args;
tnt_pass tnt_upstream;
}
location = /tnt/with_echo_module {
tnt_eval_buffer_size 1m;
tnt_eval $tnt_http_status $tnt_body {
tnt_method foo;
tnt_pass 127.0.0.1:9999;
location /api {
lua_need_request_body on;
rewrite_by_lua '
local cjson = require("cjson")
local map = {
GET = ngx.HTTP_GET,
POST = ngx.HTTP_POST,
PUT = ngx.HTTP_PUT,
-- ...
}
local res = ngx.location.capture("/tnt_proxy", {
args = ngx.var.args,
method = map[ngx.var.request_method],
body = ngx.body
})
if res.status == ngx.HTTP_OK then
local answ = cjson.decode(res.body)
-- Read reply
local result = answ["result"]
if result ~= nil then
ngx.status = result[1]["ngx"][1]
for k, v in pairs(result[1]["ngx"][2]) do
ngx.header[k] = v
end
table.remove(result, 1)
ngx.say(cjson.encode(result))
else
ngx.status = 502
ngx.say(res.body)
end
-- Finalize execution
ngx.exit(ngx.OK)
else
ngx.status = 502
ngx.say("Tarantool does not work")
end
';
}
echo $tnt_body;
}
# ...
# Also those variables are available in any nginx's languages;
```

2) '$'-prefix is required, means that tnt_eval http_code body { ... } will rise an error,
it should be tnt_eval $http_status $body { ... }.

[Back to content](#content)

tnt_eval_buffer_size
--------------------

**syntax:** *tnt_eval_buffer_size size*

**default:** *PAGE_SIZE x 16*

**context:** *main, server, location*

Specify the size of the buffer used for `tnt_eval`.

[Back to content](#content)

tnt_http_methods
Expand Down Expand Up @@ -703,7 +700,7 @@ The 0 value turns off this limitation.

[Back to content](#content)

tnt_pure_result - DEPRECATED in 2.4.0+
tnt_pure_result
--------------------------------------
**syntax:** *tnt_pure_result [on|off]*

Expand All @@ -714,7 +711,7 @@ tnt_pure_result - DEPRECATED in 2.4.0+
Whether to wrap tnt response or not.
When this option is off:
```
{"id":0, "result": [[ 1 ]]}
{"id":0, "result": [ 1 ]}
```
When this option is on:
```
Expand Down
67 changes: 32 additions & 35 deletions config
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
ngx_addon_name="ngx_http_tnt_module"

__libs=" \
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a \
$ngx_addon_dir/third_party/msgpuck/libmsgpuck.a \
"
libs="-lyajl -lmsgpuck"

__module_src_dir="$ngx_addon_dir/src"
test -f $ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a &&
test -f $ngx_addon_dir/third_party/msgpuck/libmsgpuck.a && {
libs=" \
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a \
$ngx_addon_dir/third_party/msgpuck/libmsgpuck.a \
"
}

__include_paths=" \
module_src_dir="$ngx_addon_dir/src"

include_paths=" \
$ngx_addon_dir/src \
$ngx_addon_dir/third_party \
$ngx_addon_dir/third_party/msgpuck \
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/include \
"

__sources=" \
$__module_src_dir/json_encoders.c \
$__module_src_dir/tp_transcode.c \
$__module_src_dir/ngx_http_tnt_module.c \
$__module_src_dir/ngx_http_tnt_handlers.c \
sources=" \
$module_src_dir/json_encoders.c \
$module_src_dir/tp_transcode.c \
$module_src_dir/ngx_http_tnt_module.c \
$module_src_dir/ngx_http_tnt_handlers.c \
"

__headers=" \
$__module_src_dir/debug.h \
$__module_src_dir/tp_ext.h \
$__module_src_dir/json_encoders.h \
$__module_src_dir/tp_transcode.h \
$__module_src_dir/ngx_http_tnt_handlers.h \
headers=" \
$module_src_dir/debug.h \
$module_src_dir/tp_ext.h \
$module_src_dir/json_encoders.h \
$module_src_dir/tp_transcode.h \
$module_src_dir/ngx_http_tnt_handlers.h \
"

__old_style_build=yes
old_style_build=yes
if test -n "$ngx_module_link"; then
__old_style_build=no
old_style_build=no
fi

#
# Old-style build [[
if test "$__old_style_build" = "yes"; then
if test "$old_style_build" = "yes"; then

CORE_INCS=" \
$CORE_INCS \
Expand All @@ -46,7 +51,7 @@ if test "$__old_style_build" = "yes"; then
CORE_LIBS=" \
$CORE_LIBS \
$ngx_feature_libs \
$__libs \
$libs \
"

HTTP_MODULES=" \
Expand All @@ -56,10 +61,10 @@ if test "$__old_style_build" = "yes"; then

NGX_ADDON_SRCS=" \
$NGX_ADDON_SRCS \
$__sources \
$sources \
"

for path in $__include_paths; do
for path in $include_paths; do
CFLAGS="$CFLAGS -I$path"
done
# ]]
Expand All @@ -69,18 +74,10 @@ else

ngx_module_type=HTTP
ngx_module_name=$ngx_addon_name
ngx_module_incs=$__include_paths
ngx_module_deps=$__headers
ngx_module_srcs=$__sources
ngx_module_libs=$__libs
. auto/module

ngx_module_type=HTTP_AUX_FILTER
ngx_module_name=ngx_http_tnt_eval_module
ngx_module_incs=$__include_paths
ngx_module_deps=$__headers
ngx_module_srcs=$__module_src_dir/ngx_http_tnt_eval_module.c
ngx_module_libs=
ngx_module_incs=$include_paths
ngx_module_deps=$headers
ngx_module_srcs=$sources
ngx_module_libs=$libs
. auto/module

# ]]
Expand Down
Loading

0 comments on commit 3af386f

Please sign in to comment.