Skip to content

Instantly share code, notes, and snippets.

View background.lua
-- ngx.timer.every(sync_interval, sync, self)
ngx.timer.every(sync_interval, sync, self)
View webp.sh
ffmpeg -i ${source} \
-vcodec libwebp -lossless 0 -q:v 75 -preset picture -loop 0 -an -vsync 0 -filter_complex "setpts=0.5*PTS,fps=12,scale=w=320:h=-2" -t 10 animated.webp
View test.lua
-- location /test {
-- content_by_lua_block {
local redis_client = redis_cluster:new(config)
--- simulating a dynamic code being stored at Redis ---
redis_client:set('authentication', "access_by_lua_block||local token = ngx.var.arg_token or ngx.var.cookie_superstition \n if token ~= 'token' then \n return ngx.exit(ngx.HTTP_FORBIDDEN) \n else \n ngx.header['Set-Cookie'] = {'superstition=token'} \n end")
redis_client:sadd('coding_units', 'authentication')
--- simulating a dynamic code being stored at Redis ---
--- fetch ---
local resp, err = redis_client:smembers("coding_units")
View loop.lua
--- fetch ---
local resp, err = redis_client:smembers("coding_units")
local raw_coding_units = {}
for _, coding_unit_key in ipairs(resp) do
local resp, err = redis_client:get(coding_unit_key) -- it'd be nice to keep the id for logging reasons
table.insert(raw_coding_units, resp)
end
--- fetch ---
--- parse ---
View lua.lua
-- Error during evaluation - syntactically invalid
code, err = loadstring(" a = 1 \n a a \n pring(a) \n")
print(code)
-- nil
print(err)
-- [string " a = 1 ..."]:2: '=' expected near 'a'
-- Error during runtime - syntactically valid
stringcode = " a = taketime.menu "
code, err = loadstring(stringcode)
View lua.lua
-- running luajit in repl will be useful for exploration
-- docker run -it --rm akorn/luajit:2.1-alpine
-- just inspired at
-- https://stackoverflow.com/questions/1426954/split-string-in-lua
function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
View redis.sh
# run this in one tab
docker run --rm -it --name some-redis redis
# run this in another tab
docker exec -it some-redis redis-cli
127.0.0.1:6379> set authentication "access_by_lua_block||if token ~= 'token' then \n return ngx.exit(ngx.HTTP_FORBIDDEN) \n else \n ngx.header['Set-Cookie'] = {'superstition=token'} \n end"
OK
127.0.0.1:6379> get authentication
"access_by_lua_block||if token ~= 'token' then \n return ngx.exit(ngx.HTTP_FORBIDDEN) \n else \n ngx.header['Set-Cookie'] = {'superstition=token'} \n end"
127.0.0.1:6379> sadd coding_units authentication
View steps1.sh
# make sure you have docker
git clone https://github.com/leandromoreira/nott.git
cd nott
git checkout 1.0.2
# in a tab
make run
# wait until the platform is up and running
# and in another tab run
View request_counter.lua
-- counting how many requests a given ip did
local redis_client = redis_cluster:new(config)
local resp, err = redis_client:incr(ngx.var.remote_addr)
ngx.header['X-Request-Counter'] = resp
View steps.sh
# make sure you have docker
git clone https://github.com/leandromoreira/nott.git
cd nott
git checkout 0.0.3
# in a tab
make run
# wait until the platform is up and running
# and in another tab run