@@ -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 )
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 }
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:
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
352342Example
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
432429tnt_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+
714711Whether to wrap tnt response or not.
715712When this option is off:
716713```
717- {"id":0, "result": [[ 1 ] ]}
714+ {"id":0, "result": [ 1 ]}
718715```
719716When this option is on:
720717```
0 commit comments