Openresty - Scalable Web Platform NGINX with Lua

INTRO openresty๋Š” nginx์™€ lua ๊ธฐ๋ฐ˜์˜ ์›น๊ฐœ๋ฐœ ํ”Œ๋žซํผ์ด๋‹ค. lua๋Š” luajit ์„ ์‚ฌ์šฉํ•˜๊ณ  ์—”์ง„์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ˆ…์Šค ๋ฐฐํฌํŒ์€ ํŒจํ‚ค์ง€๋ฅผ ์ง€์›ํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” docker ์ด๋ฏธ์ง€๋กœ ์„ค์น˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ๋‹ค. System Info: RaspberryPi 3 Model B Rev 1.2 OS: Raspbian (Debian 11, Bullseye) Docker: version 24.0.5 Getting Started Hello World # Install Docker and Relogin after exit curl -sSL https://get.docker.com | sh sudo usermod -aG docker $usermod exit ... docker run hello-world # Docker Image get docker pull openresty/openresty:alpine # Setup mkdir ~/www cd ~/www mkdir conf html vi conf/nginx.conf ... worker_processes 1; error_log error.log; events { worker_connections 1024; } http { server { listen 8000; location / { default_type text/html; content_by_lua ' ngx.say("<p>hello world!</p>") '; } } } ... # Run openresty docker image docker run --name mresty --rm --volume `pwd`/conf/:/usr/local/openresty/nginx/conf/ \ -p 127.0.0.1:8000:8000 openresty/openresty:alpine # docker process check docker ps # openresty web server test curl localhost:8000 <p>hello world!</p> Openresty Comandline Utility # version check resty -v # Hello World resty -e 'print("Hello World")' echo 'print("Hello World")' > hello.lua resty hello.lua # resty ngx module test time resty -e 'gnx.sleep(1) ngx.say("done")' resty -e 'local sock = ngx.socket.tcp() print(socke:connect("openresty.com",443))' resty -e 'ngx.thread.wait(ngx.thread.spawn(function () print("in thread!") end))' mkdir lua/ vim lua/test.lua ... local _M = {} function _M.hello() print("Hello") end return _M ... resty -e 'require "test".hello()' # Error resty -I lua/ -e 'require "test".hello()' # Hello resty -e 'local ok, stdout = require "resty.shell".run([[echo ok]]) print(stdout)' # ok resty --shdict 'dogs 10m' -e 'print(ngx.shared.dogs:set("age",11))' # truenilfalse resty --shdict 'dogs 7m' --shdict 'cats 5m' -e 'print(ngx.shared.dogs, " ", ngx.shared.cats)' resty --http-conf 'lua_regex_match_limit 102400;' -e 'print "ok"' # Bench Test echo 'local a = 0 for 1, 1e8 do a = a+1 end print(a)' > bench.lua time resty -joff bench.lua # without Jit Compiler time resty bench.lua # with Jit Compiler (Fast) resty -jv bench.lua resty -jdump bench.lua | less # Help resty -h | less # See Help restydoc resty-cli # See Document Structure and Lua Module Create Project Folder and Modules mkdir test-module cd test-module mkdir logs conf lua vim lua/hello.lua ... local _M = {} function _M.greet(name) ngx.say("Greetings from ", name) end return _M ... Setup nginx.conf vim conf/nginx.conf ... worker_processes 1; events { worker_connections 1024; } http { # Lua module Preload for performance and Cache init_by_lua_block { require "hello" } # add module path to lua package path: $prefix means nginx -p option value lua_package_path "$prefix/lua/?.lua;;" # Server Setup: port, lua block... server { listen 8080 reuseport; location / { content_by_lua_block { local hello = require "hello" hello.greet("a Lua module") } } } } ... Server start and Test # server configuration test nginx -p $PWD/ -t # server start nginx -p $PWD/ # Chech Error Log tail logs/error.log # client request test : Browser also available curl 'http://127.0.0.1:8080' Greetins from a Lua module Server Admin # check server process ps aux | grep nginx | grep -v /tmp/ pgrep -l nginx # Edit nginx.conf vim conf/nginx.conf # Server Restart (After editting of nginx.conf) kill -HUP `cat logs/nginx.pid` # client request again curl 'http://127.0.0.1:8080' Build Develop Environment Makefile์„ ๋งŒ๋“ค๊ณ  ๋„์ปค์ด๋ฏธ์ง€๋ฅผ ์ œ์–ดํ•˜๊ณ  ๋กœ๊ทธ ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ๋งŒ๋“ ๋‹ค. Makefile conf/nginx.conf Test REFERENCE awesome-resty - https://github.com/bungle/awesome-resty ...

2023-09-16 ยท (updated 2023-09-27) ยท 566 words

gsl-shell - Luajit GNU Scientific Library Shell

Intro gsl-shell์€ GSL(GNU Scientific Library์— luajit์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค. ๋งคํŠธ๋ฆญ์Šค, ๋ฒกํ„ฐ ๋ฐ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜ ๋“ฑ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ์ถœ๋ ฅ์„ ์ง€์›ํ•œ๋‹ค. luajit + ์ถ”๊ฐ€ ํ•จ์ˆ˜ ๋ฐ ๋ช…๋ น์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ๋น„์Šทํ•œ ๋‹ค๋ฅธ ๋„๊ตฌ ์ค‘์— ๋งค์šฐ ์šฐ์ˆ˜ํ•œ ์—ฐ์‚ฐ ๋Šฅ๋ ฅ๊ณผ ์†๋„๋ฅผ ์ž๋ž‘ํ•œ๋‹ค. Install ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ์ง์ ‘ ๋นŒ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ๊นƒํ—ˆ๋ธŒ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ๋ฐ๋น„์•ˆ, ์šฐ๋ถ„ํˆฌ์—์„œ๋Š” GSL์™ธ์— gsl-shell์€ ํŒจํ‚ค์ง€๊ฐ€ ์—†๋‹ค. ์ฒ˜์Œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋นŒ๋“œ๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋ฆด๋ฆฌ์ฆˆ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋‹ˆ ์„ค๋ช…์„ ์ฐธ๊ณ ํ•ด์„œ ์ ํ•ฉํ•œ ๊ฒƒ์œผ๋กœ ๋‚ด๋ ค๋ฐ›๋Š”๋‹ค. $ sudo apt install libgsl27 libgslcblas0 $ wget https://github.com/franko/gsl-shell/releases/download/v2.3.4/gsl-shell-dynamic-linux-x86-64.tar.gz $ tar xvfz gsl-shell-dynamic-linux-x86-64.tar.gz $ ls gsl-shell $ export PATH=$PATH:$HOME/gsl-shell/bin # ~/.bashrc์— ์ถ”๊ฐ€ ๋“ฑ๋ก $ gsl-shell # ์‹คํ–‰ GSL Shell 2.3.4 -- Copyright (C) 2009-2022 Francesco Abbate GNU Scientific Library -- Copyright (C) The GSL Team LuaJIT 2.0.5 -- Copyright (C) 2005-2022 Mike Pall. https://luajit.org/ JIT: ON CMOV SSE2 SSE3 SSE4.1 AMD fold cse dce fwd dse narrow loop abc sink fuse > # ๋‚˜๊ฐ€๊ธฐ(Ctrl+d) $ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ๋Œ€ํ™”์‹์ด ์•„๋‹Œ ์†Œ์Šค๋ฅผ ์ž‘์„ฑํ•ด์„œ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ์•„๋ž˜ ๋ฐฉ๋ฒ•์™ธ์— #!/usr/bin/env gsl-shell์„ ์ฒซ ์ค„์— ๋„ฃ๊ณ  chmod๋กœ ์‹คํ–‰๊ถŒํ•œ์„ ์ฃผ์–ด ์ง์ ‘ ์‹คํ–‰ํ•ด๋„ ๋œ๋‹ค. ์Šคํฌ๋ฆฝํŠธ ํ™•์žฅ์ž๋ฅผ lua๋กœ ํ•˜๋Š” ๊ฒƒ์€ ํŽธ์ง‘๊ธฐ ๋“ฑ์—์„œ ๋ฌธ๋ฒ• ๊ฐ•์กฐ๋“ฑ์˜ ์ง€์›์„ ๋ฐ›๊ธฐ ์œ„ํ•จ๋„ ์žˆ๊ณ  lua์˜ ๋ฌธ๋ฒ•์„ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ lua๋‚˜ luajit์—์„œ๋Š” ์ •์ƒ ๋™์ž‘ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹๋ณ„์ด ๊ฐ€๋Šฅํ•œ ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ์ •๋ฆฌํ•ด ๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. $ gsl-shell --help $ gsl-shell # ๋Œ€ํ™”์‹ ์‚ฌ์šฉ $ gsl-shell my-program.lua # ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์ด๋ ‡๊ฒŒ ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด ์•„๋ž˜ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉด ๋œ๋‹ค. ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๊ฐ€๋Š” ์ž์‹ ์—๊ฒŒ ๋‹ฌ๋ ธ๋‹ค. GSL์— ์–ด๋А์ •๋„ ์•„๋Š” ์‚ฌ์šฉ์ž๋ผ๋ฉด ๋”์šฑ ์ˆ˜์›”ํ•  ๊ฒƒ์ด๋‹ค. ์ฐธ๊ณ ๋กœ ์„ค์น˜๋œ GSL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€๋„ ๋‚˜์™€ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•œ๋‹ค. ๊ธฐํƒ€ ๋„์›€๋ง ๋ฌธ์„œ๋Œ€๋กœ use "math" ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. gsl-shell/share/gsl-shell/import.lua ์„ ์ˆ˜์ •ํ•œ๋‹ค. ... -- ์•„๋ž˜ ๋ฌธ์žฅ์„ ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ์•„๋ž˜ ๋ฌธ์žฅ์œผ๋กœ ๊ณ ์นœ๋‹ค. -- local level = getinfo(2, "") and 2 or 0 local level = 2 ๋Œ€ํ™”ํ˜• ๋ชจ๋“œ ์‚ฌ์šฉ์‹œ์— rlwrap์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ๊ณผ ํ•จ์ˆ˜๋ฅผ ํŽธ์ง‘๊ธฐ์—์„œ ์ž๋™์™„์„ฑ, ๊ฒ€์ƒ‰ ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์€ ์•„์ง ๋ชจ๋ฅธ๋‹ค. ์ตœ์†Œํ•œ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ถ”์ถœํ•ด์„œ vim ๋“ฑ๊ณผ ์—ฐ๋™ํ•˜๊ฑฐ๋‚˜ grep์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. ๋ฌธ์„œ์ž‘์„ฑ์‹œ ์ˆ˜ํ•™์‹ ํ‘œํ˜„ ๊ด€๋ จ ํ”„๋กœ๊ทธ๋žจ(์Šคํฌ๋ฆฝํŠธ)๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ๋„์›€๋ง์ด๋‚˜ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ๋งˆํฌ๋‹ค์šด(markdown) ๋ฌธ์„œ์— latex ๋ฌธ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์ฐธ์กฐํ•œ๋‹ค. ์ฆ‰, ๋ฌธ์žฅ ์ค‘์—๋Š” $์ˆ˜์‹$ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๋…๋ฆฝ์ ์ธ ์ˆ˜์‹์€ $$์ˆ˜์‹$$์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊นƒํ—ˆ๋ธŒ๋‚˜ ๊ธฐํƒ€ ์›นํŽ˜์ด์ง€์—์„œ ์ด๋ฅผ ์ž˜ ์ง€์›ํ•œ๋‹ค. vscode์—์„œ๋Š” markdown all in one ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์น˜ํ•˜๋ฉด ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋กœ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ˆ˜ํ•™ํ‘œํ˜„์‹๋„ ์ง€์›ํ•œ๋‹ค. vim์—์„œ๋Š” hugo๋กœ ๋กœ์ปฌ์—์„œ ์„œ๋ฒ„๋ฅผ ๋„์–ด ์‹ค์‹œ๊ฐ„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ firefox์˜ markdown์ง€์› ๋ชจ๋“ˆ์ธ markdown viewer webtext๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์‹œ๊ฐ„ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด์™€ ๊ด€๋ จ๋œ ์‚ฌํ•ญ์€ ๋‹ค๋ฅธ ๋ฌธ์„œ๋กœ ์ •๋ฆฌํ•ด ๋ณผ ์ƒ๊ฐ์ด๋‹ค. Links GSL Shell project page https://github.com/franko/gsl-shell/ GSL Documentation https://franko.github.io/gsl-shell/ Download Release https://github.com/franko/gsl-shell/releases/tag/v2.3.4

2023-02-01 ยท 405 words

Alpine - Lua Language Server Install

25-07-27: Alpine Linux 3.2x ๊ธฐ์ค€์œผ๋กœ luajit-nvim-lspconfig ์ฐธ๊ณ  23-08-08: nvim ๋ฒ„์ „์ด 3.18๋Œ€๋กœ ์˜ฌ๋ผ์˜ค๊ณ , Community Repository๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ํŒจํ‚ค์ง€๋กœ ๊ฐ„๋‹จํžˆ ์„ค์น˜ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ตœ์ ํ™”๋‚˜ ์ปดํŒŒ์ผ ์ž์ฒด์— ์˜๋ฏธ๋ฅผ ๋‘์ง€ ์•Š๋Š”๋‹ค๋ฉด ํŒจํ‚ค์ง€๋กœ ์„ค์น˜ํ•˜์ž. PREINSTALL clang ๋ฐ luajit ๋ฐ ๊ฐœ๋ฐœ ํˆด ์„ค์น˜ ํ•„์ˆ˜: build-base, clang, clang-extra-tools(clangd), cmake, luajit, luajit-dev # apk add --no-cache build-base clang clang-extra-tools lld compiler-rt compiler-rt-static cmake git luajit luajit-dev bash bash-completion grep rlwrap bat fd curl wget rsync sqlite jq tree ninja ์„ค์น˜ - alpine 3.16.3 ํ˜„์žฌ ๊ธฐ๋ณธ ํŒจํ‚ค์ง€ ์—†์Œ # git clone https://github.com/ninja-build/ninja && cd ninja # cmake -Bbuild-cmake # cmake --build build-cmake # ./build-cmake/ninja_test # cp ./build-cmake/ninja /usr/local/bin/ # which ninja # PATH ํ™•์ธ lua-language-server ์„ค์น˜ # cd /usr/local/ # git clone --depth=1 https://github.com/sumneko/lua-language-server # cd lua-language-server # git submodule update --depth 1 --init --recursive # cd 3rd/luamake # ./compile/install.sh # cd ../.. # ./3rd/luamake/luamake rebuild # ./bin/lua-language-server --version # cat<<EOF > /usr/local/bin/lua-language-server #!/usr/bin/env bash exec "/usr/local/lua-language-server/bin/lua-language-server" "$@" EOF # chmod 755 /usr/local/bin/lua-language-server # which lua-language-server # PATH ํ™•์ธ clangd ์„ค์น˜ clangd๋Š” c,c++์„ ์ง€์›ํ•œ๋‹ค. clangd๋Š” clang-extra-tools ํŒจํ‚ค์ง€์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. Links ninja build howto https://github.com/ninja-build/ninja lua-language-server - Getting Started https://github.com/sumneko/lua-language-server/wiki/Getting-Started#command-line

2022-11-21 ยท (updated 2025-07-27) ยท 179 words

JSON - Lua Module

Intro ๊ฒฐ๋ก ์€ lua cjson์ด ๊ฐ€์žฅ ๋น ๋ฅด๋‹ค. ๊ทธ ๋‹ค์Œ์ด lunajson dkjson์€ ์ˆœ์ˆ˜ luaํŒŒ์ผ์ด๊ณ  ๋‹จ์ผ ํŒŒ์ผ์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” json.lua ์ด๋‹ค. ๋น ๋ฅด๋‹ค๊ณ  ํ•˜๋Š”๋ฐโ€ฆ ๋น„๊ต์  ์ตœ์‹ ์ด๋‹ค. cjson์€ ๋น ๋ฅด๋‚˜ ๋ชจ๋“ˆ ์ปดํŒŒ์ผ์ด ํ•„์š”ํ•˜๊ณ  lunajson์€ ๋‹ค์Œ์œผ๋กœ ๋น ๋ฅด๊ณ  ์ˆœ์ˆ˜ luaํŒŒ์ผ์ด์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋‹ค. dkjson์€ ์…‹ ์ค‘ ๊ฐ€์žฅ ํผํฌ๋จผ์Šค๊ฐ€ ๋‚ฎ์œผ๋‚˜ ๋‹จ์ผ ํŒŒ์ผ์ด๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜๋‹ค. Links https://github.com/grafi-tt/lunajson http://dkolf.de/src/dkjson-lua.fsl/home https://github.com/rxi/json.lua https://somedudesays.com/2019/12/using-json-with-lua

2022-11-07 ยท 55 words

CSV - Lua Module

ftcsv ์ถ”์ฒœ

2022-11-07 ยท 2 words