Skip to content

Commit 3af386f

Browse files
committed
1) tnt_eval has been removed, since it does not work well. It was replaced 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.
1 parent f634533 commit 3af386f

20 files changed

+263
-1372
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ many contributions from the community.
44
Below follows a list of people, who contributed their code.
55

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

99
NOTE: If you can commit a change to this list, please do not hesitate
1010
to add your name to it.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ configure-debug:
6464
--prefix=$(PREFIX_PATH) \
6565
--with-http_addition_module \
6666
--add-module=$(MODULE_PATH) \
67+
--add-module=$(MODULE_PATH)/../ngx_devel_kit \
68+
--add-module=$(MODULE_PATH)/../lua-nginx-module \
6769
--with-debug
6870

6971
configure-for-testing: configure-debug

README.md

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ Tarantool - https://hub.docker.com/r/tarantool/tarantool
4242
* [JSON](#json)
4343
* [Directives](#directives)
4444
* [tnt_pass](#tnt_pass)
45-
* [tnt_eval](#tnt_eval)
46-
* [tnt_eval_buffer_size](#tnt_eval_buffer_size)
45+
* [HTTP headers and status](#HTTP headers and status)
4746
* [tnt_http_methods](#tnt_http_methods)
4847
* [tnt_http_rest_methods](#tnt_http_rest_methods)
4948
* [tnt_pass_http_request](#tnt_pass_http_request)
@@ -211,7 +210,7 @@ end
211210
[ { "result": JSON_RESULT_OBJECT, "id":UINT, "error": { "message": STR, "code": INT } }, ...N ]
212211

213212

214-
"result" - DEPRECATED in 2.4.0+
213+
"result"
215214

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

@@ -253,11 +252,11 @@ end
253252

254253
rpc call 1:
255254
--> { "method": "echo", "params": [42, 23], "id": 1 }
256-
<-- [42, 23]
255+
<-- { "id": 1, "result": [42, 23]
257256

258257
rpc call 2:
259258
--> { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
260-
<-- [ {"hello": "world"} ], "!" ]
259+
<-- { "id": 2, "result": [ {"hello": "world"} ], "!" ]}
261260

262261
rpc call of a non-existent method:
263262
--> { "method": "echo_2", "id": 1 }
@@ -273,8 +272,8 @@ end
273272
{ "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
274273
]
275274
<-- [
276-
[42, 23],
277-
[{"hello": "world"} ], "!" ],
275+
{ "id": 1, "result": [42, 23]},
276+
{ "id": 2, "result" : [{"hello": "world"} ], "!" ]},
278277
]
279278

280279
rpc call Batch of a non-existent method:
@@ -284,7 +283,7 @@ end
284283
]
285284
<-- [
286285
{ "error": {"code": -32601, "message": "Method not found"}, "id": 1 },
287-
[ {"hello": "world"} ], "!" ]
286+
{"id": 2, "result": [ {"hello": "world"} ], "!" ]}
288287
]
289288

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

328327
[Back to content](#content)
329328

330-
tnt_eval
331-
--------
332-
**syntax:** *tnt_eval $HTTP_STATUS_VAR_NAME $HTTP_BODY_VAR_NAME*
329+
HTTP headers and status
330+
-----------------------
333331

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

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

347-
NOTICE!
339+
Here is an example with `ngx_lua`:
348340

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

352342
Example
353343

@@ -358,7 +348,7 @@ Example
358348
-- First arg. if __ngx exists and tnt_eval is used, then it will be
359349
-- readed by nginx
360350
{
361-
__ngx = {
351+
ngx = {
362352
200, -- set status HTTP 200
363353
{ ["X-Tarantool"] = "FROM_TNT" } -- set headers
364354
}
@@ -373,60 +363,67 @@ Example
373363
374364
upstream tnt_upstream {
375365
127.0.0.1:9999;
366+
keepalive 10000;
376367
}
377368
378-
location = /tnt {
379-
380-
tnt_eval_buffer_size 1m;
381-
382-
tnt_eval $tnt_http_status $tnt_body {
383-
tnt_method foo;
384-
tnt_pass 127.0.0.1:9999;
385-
}
386-
387-
if ($tnt_http_status = 404) {
388-
return 404 $tnt_body;
389-
}
390-
391-
if ($tnt_body ~= 'Tarantool') {
392-
return 200 '<html><h1>Found Tarantool!</h1></html>';
393-
}
394-
395-
return 200 $tnt_body;
369+
location /tnt_proxy {
370+
tnt_method tnt_proxy;
371+
tnt_buffer_size 100k;
372+
tnt_pass_http_request on parse_args;
373+
tnt_pass tnt_upstream;
396374
}
397375
398-
location = /tnt/with_echo_module {
399-
400-
tnt_eval_buffer_size 1m;
401-
402-
tnt_eval $tnt_http_status $tnt_body {
403-
tnt_method foo;
404-
tnt_pass 127.0.0.1:9999;
376+
location /api {
377+
378+
lua_need_request_body on;
379+
380+
rewrite_by_lua '
381+
382+
local cjson = require("cjson")
383+
384+
local map = {
385+
GET = ngx.HTTP_GET,
386+
POST = ngx.HTTP_POST,
387+
PUT = ngx.HTTP_PUT,
388+
-- ...
389+
}
390+
391+
local res = ngx.location.capture("/tnt_proxy", {
392+
args = ngx.var.args,
393+
method = map[ngx.var.request_method],
394+
body = ngx.body
395+
})
396+
397+
if res.status == ngx.HTTP_OK then
398+
local answ = cjson.decode(res.body)
399+
400+
-- Read reply
401+
local result = answ["result"]
402+
403+
if result ~= nil then
404+
ngx.status = result[1]["ngx"][1]
405+
for k, v in pairs(result[1]["ngx"][2]) do
406+
ngx.header[k] = v
407+
end
408+
409+
table.remove(result, 1)
410+
ngx.say(cjson.encode(result))
411+
else
412+
ngx.status = 502
413+
ngx.say(res.body)
414+
end
415+
416+
-- Finalize execution
417+
ngx.exit(ngx.OK)
418+
else
419+
ngx.status = 502
420+
ngx.say("Tarantool does not work")
421+
end
422+
';
405423
}
406424
407-
echo $tnt_body;
408-
}
409-
410-
# ...
411-
# Also those variables are available in any nginx's languages;
412425
```
413426

414-
2) '$'-prefix is required, means that tnt_eval http_code body { ... } will rise an error,
415-
it should be tnt_eval $http_status $body { ... }.
416-
417-
[Back to content](#content)
418-
419-
tnt_eval_buffer_size
420-
--------------------
421-
422-
**syntax:** *tnt_eval_buffer_size size*
423-
424-
**default:** *PAGE_SIZE x 16*
425-
426-
**context:** *main, server, location*
427-
428-
Specify the size of the buffer used for `tnt_eval`.
429-
430427
[Back to content](#content)
431428

432429
tnt_http_methods
@@ -703,7 +700,7 @@ The 0 value turns off this limitation.
703700

704701
[Back to content](#content)
705702

706-
tnt_pure_result - DEPRECATED in 2.4.0+
703+
tnt_pure_result
707704
--------------------------------------
708705
**syntax:** *tnt_pure_result [on|off]*
709706

@@ -714,7 +711,7 @@ tnt_pure_result - DEPRECATED in 2.4.0+
714711
Whether to wrap tnt response or not.
715712
When this option is off:
716713
```
717-
{"id":0, "result": [[ 1 ]]}
714+
{"id":0, "result": [ 1 ]}
718715
```
719716
When this option is on:
720717
```

config

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
ngx_addon_name="ngx_http_tnt_module"
22

3-
__libs=" \
4-
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a \
5-
$ngx_addon_dir/third_party/msgpuck/libmsgpuck.a \
6-
"
3+
libs="-lyajl -lmsgpuck"
74

8-
__module_src_dir="$ngx_addon_dir/src"
5+
test -f $ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a &&
6+
test -f $ngx_addon_dir/third_party/msgpuck/libmsgpuck.a && {
7+
libs=" \
8+
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/lib/libyajl_s.a \
9+
$ngx_addon_dir/third_party/msgpuck/libmsgpuck.a \
10+
"
11+
}
912

10-
__include_paths=" \
13+
module_src_dir="$ngx_addon_dir/src"
14+
15+
include_paths=" \
1116
$ngx_addon_dir/src \
1217
$ngx_addon_dir/third_party \
1318
$ngx_addon_dir/third_party/msgpuck \
1419
$ngx_addon_dir/third_party/yajl/build/yajl-2.1.0/include \
1520
"
1621

17-
__sources=" \
18-
$__module_src_dir/json_encoders.c \
19-
$__module_src_dir/tp_transcode.c \
20-
$__module_src_dir/ngx_http_tnt_module.c \
21-
$__module_src_dir/ngx_http_tnt_handlers.c \
22+
sources=" \
23+
$module_src_dir/json_encoders.c \
24+
$module_src_dir/tp_transcode.c \
25+
$module_src_dir/ngx_http_tnt_module.c \
26+
$module_src_dir/ngx_http_tnt_handlers.c \
2227
"
2328

24-
__headers=" \
25-
$__module_src_dir/debug.h \
26-
$__module_src_dir/tp_ext.h \
27-
$__module_src_dir/json_encoders.h \
28-
$__module_src_dir/tp_transcode.h \
29-
$__module_src_dir/ngx_http_tnt_handlers.h \
29+
headers=" \
30+
$module_src_dir/debug.h \
31+
$module_src_dir/tp_ext.h \
32+
$module_src_dir/json_encoders.h \
33+
$module_src_dir/tp_transcode.h \
34+
$module_src_dir/ngx_http_tnt_handlers.h \
3035
"
3136

32-
__old_style_build=yes
37+
old_style_build=yes
3338
if test -n "$ngx_module_link"; then
34-
__old_style_build=no
39+
old_style_build=no
3540
fi
3641

3742
#
3843
# Old-style build [[
39-
if test "$__old_style_build" = "yes"; then
44+
if test "$old_style_build" = "yes"; then
4045

4146
CORE_INCS=" \
4247
$CORE_INCS \
@@ -46,7 +51,7 @@ if test "$__old_style_build" = "yes"; then
4651
CORE_LIBS=" \
4752
$CORE_LIBS \
4853
$ngx_feature_libs \
49-
$__libs \
54+
$libs \
5055
"
5156

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

5762
NGX_ADDON_SRCS=" \
5863
$NGX_ADDON_SRCS \
59-
$__sources \
64+
$sources \
6065
"
6166

62-
for path in $__include_paths; do
67+
for path in $include_paths; do
6368
CFLAGS="$CFLAGS -I$path"
6469
done
6570
# ]]
@@ -69,18 +74,10 @@ else
6974

7075
ngx_module_type=HTTP
7176
ngx_module_name=$ngx_addon_name
72-
ngx_module_incs=$__include_paths
73-
ngx_module_deps=$__headers
74-
ngx_module_srcs=$__sources
75-
ngx_module_libs=$__libs
76-
. auto/module
77-
78-
ngx_module_type=HTTP_AUX_FILTER
79-
ngx_module_name=ngx_http_tnt_eval_module
80-
ngx_module_incs=$__include_paths
81-
ngx_module_deps=$__headers
82-
ngx_module_srcs=$__module_src_dir/ngx_http_tnt_eval_module.c
83-
ngx_module_libs=
77+
ngx_module_incs=$include_paths
78+
ngx_module_deps=$headers
79+
ngx_module_srcs=$sources
80+
ngx_module_libs=$libs
8481
. auto/module
8582

8683
# ]]

0 commit comments

Comments
 (0)