Rest command line tool demo

Time:2021-4-21

https://www.bilibili.com/vide…

In this tutorial, we’ll show you how to use therestyCommand line tools.

cd ~
export PATH=/usr/local/openresty/bin:$PATH
which resty

Rest command line tool demo

It’s usually this path.

We can go through it-VOption to check its version number.

resty -V

Rest command line tool demo

If you use usPre built Linux binary packageInstall openresty, then you should install itopenresty-restyBag.

dnf list installed openresty-resty

Rest command line tool demo

Because it’s not hereopenrestyIn the main package.

For examplerestyIt’s much easier to do “Hello world” with the command.

resty -e 'print("Hello World")'

Rest command line tool demo

be careful-eOptions.

Or run a Lua script on the terminal.

echo 'print("Hello World")' > hello.lua
cat hello.lua
resty hello.lua

Rest command line tool demo

So it’s also a good way to write new command-line applications using openresty.

Non blocking I can also be implemented here / O。

time resty -e 'ngx.sleep(1) ngx.say("done")'

Rest command line tool demo

Let’s use the cosocket API to connect to the openresty.com Port 443.

resty -e 'local sock = ngx.socket.tcp() print(sock:connect("openresty.com", 443))'

Rest command line tool demo

Or use light threading.

resty -e 'ngx.thread.wait(ngx.thread.spawn(function () print("in thread!") end))'

Rest command line tool demo

You can also easily use Lua modules. Let’s create onetestmodular.

mkdir lua/
vim lua/test.lua

lua/test.luaThe file looks like this.

local _M = {}

function _M.hello() print("Hello") end

return _M

Rest command line tool demo

Then we use-IOptions willlua/The directory is added to the Lua module search path.

resty -I lua/ -e 'require "test".hello()'

Rest command line tool demo

without-IOption, it cannot be found.

resty -e 'require "test".hello()'

Rest command line tool demo

that is becauselua/The directory is not in the search path of lua module by default.

You can load standard Lua modules directly, such asresty.shell

resty -e 'local ok, stdout = require "resty.shell".run([[echo ok]]) print(stdout)'

Rest command line tool demo

This module is used to run a small number of shell commands nonblocking.

We can also get through--shdictOption to define the Lua shared memory dictionary.

resty --shdict 'dogs 10m' -e 'print(ngx.shared.dogs:set("age", 11))'

Rest command line tool demo

Multiple shared dictionaries can be defined in this way.

resty --shdict 'dogs 7m' --shdict 'cats 5m' -e 'print(ngx.shared.dogs, " ", ngx.shared.cats)'

Rest command line tool demo

It can also easily inject custom nginx configuration code.

resty --http-conf 'lua_regex_match_limit 102400;' -e 'print "ok"'

Rest command line tool demo

We can also play with luajit’s JIT compiler.

Let’s create a hot Lua script.

echo 'local a = 0 for i = 1, 1e8 do a = a + 1 end print(a)' > bench.lua
cat bench.lua

Rest command line tool demo

Then disable the JIT compiler completely.

time resty -joff bench.lua

Rest command line tool demo

For comparison, we can check how fast the JIT compiler is enabled.

time resty bench.lua

Rest command line tool demo

Or we can use it-jvOption to check the compiled Lua code path, or “traces.”.

resty -jv bench.lua

Rest command line tool demo

Or there are more details, such as compiled bytecode dump, IR code dump, and machine code dump.

resty -jdump bench.lua

You can go through it at any time-hOption to find all supported features.

resty -h

Or refer to its documentation through the “restydoc” tool.

restydoc resty-cli

If you install through our pre built binary packageopenrestyThen you should installopenresty-docoropenresty-restydocBag.

dnf list installed openresty-doc

Rest command line tool demo

We’ll learn more about the “restydoc” tool in another dedicated video tutorial.
If you like this tutorial, please subscribe to this blog and ourYouTube channelorStation B channel. thank you!

<! — Xray “improve the performance of your application through openresty Xray products” — >

About this article and Related videos

This article and the associated videos are all created by our openresty demo system from a very simpleScript file Automatically generated.

About the author

Zhang Yichun is an open source projectOpenResty®The founder of theOpenResty Inc.The founder and CEO of the company. He contributed a lot of third-party modules of nginx, quite a lot of nginx andLuaJITCore patch, and designedOpenResty XRayAnd other products.

Focus on us

If you like this article, please follow usOpenResty Inc.Company’sBlog website. Also welcome to scan code to pay attention to our WeChat official account:

Rest command line tool demo

translate

We provideEnglish versionThe original text and Chinese Version (this article). We also welcome readers to provide translation versions in other languages. As long as the full text translation does not include ellipsis, we will consider it. Thank you very much!

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]