当前位置:网站首页>How to implement Lua entry of API gateway
How to implement Lua entry of API gateway
2022-07-06 23:25:00 【Fried chicken and spicy chicken 123】
structure Lua Project environment
LuaJIT Is relative to Lua More mainstream existence
stay IDEA Add EmmyLua Plug in is ready to use ,
idea function lua Script
Handwritten code on
Basic grammar
You can refer to the following information Lua Introduction reference
Print function
- io.write
print("123")
io.write('1234')
loop
There are two kinds of circulation
- for loop
for i = 1, 10 do
if i % 2 == 0 then
io.write(" even numbers ", i, '\n')
else
io.write(' Odd number ', i, "\n")
end
end
- while loop
a = 1
while true do
if a % 2 == 0 then
io.write(" even numbers ", a, '\n')
else
io.write(' Odd number ', a, "\n")
end
a = a + 1
end
This grammar and Bash shell The script is a little similar ,end and then Key words .
Conditions
- if - else - end
if a % 2 == 0 then
io.write(" even numbers ", a, '\n')
else
io.write(' Odd number ', a, "\n")
end
- if - elseif - end
if a > 10 and a % 8 == 0 and not (a % 16 == 0) then
break
elseif a > 10 and a % 8 == 0 and (a % 16 == 0) then
io.write(a, "+", '\n')
end
Default data structure
table, Is a generic like structure , That is, you can put anything in it , Represents a normal array , The symbol table , aggregate , Record , graphics , Trees, etc , And realize associative array .
c = table
table.insert(c,'a')
table.insert(c,'b')
table.insert(c,'c')
print(c[0],c[1],c[2],c[3])
According to the experimental results , Find out table It seems that the default from offset is 1 Start filling . let me put it another way ,table Itself is from the index 1 At the beginning .
except table Beyond this complex structure ,lua There are also some basic data types , as follows :
Special operators
lua There are two unusual operators , They are string concatenation characters … and Take string length characters #
c = 'dsadsadsadsadqewdxadssadsasdsasdsadsad'
d = 'dsarewfdsdsasdad'
print(c..d)
print(#c,#d,#(c..d))
function
Support to define template functions
function demo(func, value)
value = value * value
func(value)
end
function demo1(value)
print('-',value,'-')
end
function demo2(value)
print('=',value,'=')
end
demo(demo1,2)
demo(demo2,2)
there demo Template function ,demo1 and demo2 Is a subfunction , A subfunction can be a parameter of a function .
String manipulation
string The method inside
iterator
Iterators are required to traverse an array or collection , and lua The iterator function provided in is ipairs, In terms of name , It's a binary structure .
OpenResty The method in
If in OpenResty and Lua The same method provided in , Then be sure to use OpenResty Of , because OpenResty It's synchronous and non blocking , however Lua The library itself may be blocked , It will greatly reduce the performance .
obtain http Request related methods
-- obtain uri
real = ngx.var.uri
-- To obtain parameters
local query = ngx.req.get_uri_args()
-- obtain headers
local headers = ngx.req.get_headers()
-- obtain cookies
local cookies = ngx.ctx.__cookies__
-- Get sources IP
real = ngx.var.remote_addr
-- obtain UA
real = ngx.var.http_user_agent
-- Access method
local method = ngx.req.get_method()
-- obtain post Parameters
ngx.req.read_body()
local post_params, err = ngx.req.get_post_args()
-- obtain referer
real = ngx.var.http_referer
-- obtain host
real = ngx.var.host
Some useful libraries
- resty.dns.client
- resty.dns.balancer
- resty.http
- resty.jwt
matters needing attention
- Local variables use local Keyword to define . Here, too Python It's kind of like , however python Generally, you specify function variables as global variables (global), And here we use local To define local variables .
- Lua The function parameter of can be method , This and JS A bit similar .
Okay , With Lua Knowledge and open source API Gateway knowledge , You can start writing your own API Gateway , My ultimate goal is to write an anti crawl system , Interested friends can pay attention to , I will keep updating .
边栏推荐
- Implementation steps of mysql start log in docker
- 让 Rust 库更优美的几个建议!你学会了吗?
- 传统企业要为 Web3 和去中心化做的 11 个准备
- Cover fake big empty talk in robot material sorting
- docker启动mysql及-eMYSQL_ROOT_PASSWORD=my-secret-pw问题解决
- The problem of ASP reading Oracle Database
- 自动更新Selenium驱动chromedriver
- B站大佬用我的世界搞出卷積神經網絡,LeCun轉發!爆肝6個月,播放破百萬
- (1)长安链学习笔记-启动长安链
- Why are some people still poor and living at the bottom of society even though they have been working hard?
猜你喜欢
吴恩达2022机器学习课程评测来了!
若依请求url中带有jsessionid的解决办法
让我们,从头到尾,通透网络I/O模型
云原生(三十二) | Kubernetes篇之平台存储系统介绍
每日刷题记录 (十五)
On file uploading of network security
Les entreprises ne veulent pas remplacer un système vieux de dix ans
docker中mysql开启日志的实现步骤
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
Docker starts MySQL and -emysql_ ROOT_ Password = my secret PW problem solving
随机推荐
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
JS import excel & Export Excel
B 站弹幕 protobuf 协议还原分析
案例推荐丨安擎携手伙伴,保障“智慧法院”更加高效
Koa2 addition, deletion, modification and query of JSON array
借助这个宝藏神器,我成为全栈了
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
How to choose indoor LED display? These five considerations must be taken into account
A novice asks a question. I am now deployed on a single machine. I submitted an SQL job and it runs normally. If I restart the service job, it will disappear and I will have to
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
MySQL数据库之JDBC编程
Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
How to achieve text animation effect
Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
What can be done for traffic safety?
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
传统企业要为 Web3 和去中心化做的 11 个准备
docker mysql5.7如何设置不区分大小写
None of the strongest kings in the monitoring industry!
Enterprises do not want to replace the old system that has been used for ten years