当前位置:网站首页>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 .
边栏推荐
- 机器人材料整理中的套-假-大-空话
- ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
- Where does this "judge the operation type according to the op value and assemble SQL by yourself" mean? It means simply using Flink tab
- 云原生(三十二) | Kubernetes篇之平台存储系统介绍
- On file uploading of network security
- The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
- (1) Chang'an chain learning notes - start Chang'an chain
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Use mitmproxy to cache 360 degree panoramic web pages offline
猜你喜欢
今日睡眠质量记录78分
机器人材料整理中的套-假-大-空话
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
[compilation principle] LR (0) analyzer half done
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...
企业不想换掉用了十年的老系统
C three ways to realize socket data reception
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
每日刷题记录 (十五)
传统企业要为 Web3 和去中心化做的 11 个准备
随机推荐
JDBC programming of MySQL database
Docker mysql5.7 how to set case insensitive
The application of machine learning in software testing
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
浅谈网络安全之文件上传
B 站弹幕 protobuf 协议还原分析
How to choose the server system
Cloud native technology container knowledge points
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
TDengine 社区问题双周精选 | 第二期
(1) Chang'an chain learning notes - start Chang'an chain
请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
Dockermysql modifies the root account password and grants permissions
(flutter2) as import old project error: inheritfromwidgetofexacttype
A few suggestions for making rust library more beautiful! Have you learned?
What does security capability mean? What are the protection capabilities of different levels of ISO?