特性:
*) 支持Windows和Linux平台。
*) 支持高并发高性能。
*) HTML网页中内嵌LUA脚本代码,类似于PHP。
*) 支持非阻塞的数据库操作,目前只支持MYSQL。
*) 支持异步的文件IO操作。
*) 支持非阻塞的SOCKET IO操作。
API简介:
*) nginx 表
提供一些实用函数,例如URI转换、BASE64编解码、CRC校验、MURMURHASH2、MD5和SHA1。
具体用法请查看下面示例代码。
*) nginx.database 表
提供数据库操作的接口,这些接口的内部实现是基于非阻塞模式的,
因此不会阻塞Nginx的事件处理,可以支持比较高的并发。
具体用法请查看下面示例代码。
*)
nginx.file 表
提供文件操作的接口,这些接口的内部实现是基于异步IO模式的,
因此不会阻塞Nginx的事件处理,可以支持比较高的并发。
具体用法请查看下面示例代码。
*) nginx.logger 表
Nginx日志接口的封装,允许在LUA代码中写日志信息到Nginx的日志文件。
具体用法请查看下面示例代码。
*) nginx.request 表
提供与HTTP请求有关的接口,可以获取请求参数、请求头和Cookie值,等等。
具体用法请查看下面示例代码。
*) nginx.response 表
提供与HTTP响应有关的接口。
具体用法请查看下面示例代码。
*)
nginx.socket 表
提供SOCKET操作的接口,这些接口的内部实现是基于非阻塞IO模式的,
因此不会阻塞Nginx的事件处理,可以支持比较高的并发。
具体用法请查看下面示例代码。
*) nginx.variable 表
提供接口给LUA代码以获取Nginx的HTTP变量。
具体用法请查看下面示例代码。
TODO:
*) API说明文档。
*) 更多实用功能的LUA表和接口实现,
例如多台机器之间会话(Session)共享的透明处理。
下载:
https://github.com/hehaiqiang/ngwsx/downloads
示例代码:
index.lsp
<%
local req = nginx.request
--local name = req["name"]
--local name = req.name
if req.method == req.GET then
name = req.get["name"]
name = req.get.name
else
name = req.post["name"]
name = req.post.name
end
name = name or "world"
%>
<html>
<head><title>hello, <%=name%>!</title></head>
<body>
hello, <%=name%>!
<hr>
<form action="index.lsp" method="post">
<input type="text" name="name"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
test_core.lsp
<%
local nginx = nginx
%>
<html>
<head>
</head>
<body>
<%
local uri = "id=你好&title=世界"
local escaped_uri = nginx.escape_uri(uri)
local unescaped_uri = nginx.unescape_uri(escaped_uri)
local str = "dsdsdtdkslddfsdfdffdfsdfsdfweijqawlkrklekefwklksldksksd"
local encoded_base64 = nginx.encode_base64(str)
local decoded_base64 = nginx.decode_base64(encoded_base64)
local crc16 = nginx.crc16(str) or "crc16 error";
local crc32 = nginx.crc32(str) or "crc32 error";
local murmur_hash2 = nginx.murmur_hash2(str) or "murmur_hash2 error"
local md5 = nginx.md5(str) or "md5 error"
local sha1 = nginx.sha1(str) or "sha1 error"
%>
URI escape test<br/><%=uri%><br/><%=escaped_uri%>
<hr>
URI unescape test<br/><%=escaped_uri%><br/><%=unescaped_uri%>
<hr>
BASE64 encode test<br/><%=str%><br/><%=encoded_base64%>
<hr>
BASE64 decode test<br/><%=encoded_base64%><br/><%=decoded_base64%>
<hr>
CRC16 test<br/><%=str%><br/><%=crc16%>
<hr>
CRC32 test<br/><%=str%><br/><%=crc32%>
<hr>
MURMURHASH2 test<br/><%=str%><br/><%=murmur_hash2%>
<hr>
MD5 test<br/><%=str%><br/><%=md5%>
<hr>
SHA1 test<br/><%=str%><br/><%=sha1%>
</body>
</html>
test_database.lsp
<%
local print = print
local nginx = nginx
local req = nginx.request
local db = nginx.database
local res = db.execute({
driver = "libdrizzle",
host = "127.0.0.1",
port = 3306,
user = "root",
password = "123456",
database = "mysql",
sql = "show databases"
})
%>
<html>
<head>
</head>
<body>
err: <%=res.err%>
<br/>
errstr: <%=res.errstr%>
<br/>
col_count: <%=res.col_count or ""%>
<br/>
row_count: <%=res.row_count or ""%>
<br/>
affected_rows: <%=res.affected_rows or ""%>
<br/>
insert_id: <%=res.insert_id or ""%>
<br/>
<% if res.err ~= 0 then print("error") return end %>
<hr>
<table >
<tr>
<% for i=1,#res.columns do %>
<td><b><%=res.columns[i]%></b></td>
<% end %>
</tr>
<% for r=1,#res.rows do %>
<tr>
<% for i=1,#res.rows[r] do %>
<td><%=res.rows[r][i]%></td>
<% end %>
</tr>
<% end %>
</table>
<hr>
request_time: <%=req.request_time%>ms
</body>
</html>
test_file.lsp
<%
local nginx = nginx
local file = nginx.file
%>
<html>
<head>
</head>
<body>
<% local f = file.open("c:/test.txt") %>
<% local n,res = f:read() %>
n: <%=n%>
<br/>
res: <%=res%>
<hr>
<% local n,err = f:write("kdkdksldskdlsdkkkkkkkkkkkkkkkkkkkkkkkkkk") %>
n: <%=n%>
<br/>
err: <%=err or ""%>
<hr>
<% f:close() %>
</body>
</html>
test_logger.lsp
<%
local print = print
local nginx = nginx
local log = nginx.logger
%>
<html>
<head>
</head>
<body>
<%
-- writing some messages into the log file of the nginx
log.error(log.ALERT, "test alert" .. 1 .. 10)
log.debug(log.DEBUG_HTTP, "test debug http")
log.error(log.ERR, "test error")
log.error(log.EMERG, 1000)
%>
please opening the log file of the nginx to view messages.
</body>
</html>
test_request.lsp
<%
local print = print
local nginx = nginx
local req = nginx.request
local get_req_members = function()
return {
uri = req.uri,
args = req.args,
host = req.host,
exten = req.exten,
method = req.method,
referer = req.referer,
user_agent = req.user_agent,
method_name = req.method_name,
request_time = req.request_time .. "ms",
request_line = req.request_line,
unparsed_uri = req.unparsed_uri,
http_protocol = req.http_protocol
}
end
function get_headers_members()
local headers = req.headers
return {
host = headers.host,
user_agent = headers.user_agent
}
end
%>
<html>
<head>
</head>
<body>
<table >
<% for k,v in pairs(get_req_members()) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
<hr>
<table >
<% for k,v in pairs(get_headers_members()) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
<%
-- TODO: test the table "req.cookies"
%>
<hr>
<%
local one = req["one"] or 1
local two = req.two or 2
local three = req.get["three"] or 3
local four = req.get.four or 4
%>
hello, <%=one%><%=two%><%=three%><%=four%>!
</body>
</html>
test_response.lsp
<%
local print = print
local nginx = nginx
local req = nginx.request
local resp = nginx.response
resp.content_type = "text/html"
%>
<html>
<head><title></title></head>
<body>
<%
local one = req["one"] or 1
local two = req.two or 2
local three = req.post["three"] or 3
local four = req.post.four or 4
%>
hello, <%=one%><%=two%><%=three%><%=four%>!
<hr>
<form action="test_response.lsp" method="post">
<input type="text" name="one"/>
<input type="text" name="two"/>
<input type="text" name="three"/>
<input type="text" name="four"/>
<input type="submit" value="submit"/>
</form>
<hr>
</body>
</html>
test_socket.lsp
<%
local nginx = nginx
local resp = nginx.response
local socket = nginx.socket
resp.content_type = "text/html"
%>
<% local s = socket.open("www.nginx.org", 80) %>
<%=s:send("GET / HTTP/1.1rnHost: www.nginx.orgrnrn") or "send error"%>
<hr>
<% local n,res = s:recv() %>
<%=n or "recv error"%>
<hr>
<%=res or "recv error"%>
<hr>
<% n,res = s:recv() %>
<%=n or "recv error"%>
<hr>
<%=res or "recv error"%>
<hr>
<% n,res = s:recv() %>
<%=n or "recv error"%>
<hr>
<%=res or "recv error"%>
<% s:close() %>
test_variable.lsp
<%
local print = print
local nginx = nginx
local var = nginx.variable
local array = {
--var.arg_PARAMETER or "",
args = var.args or "",
binary_remote_addr = var.binary_remote_addr or "",
body_bytes_sent = var.body_bytes_sent or "",
content_length = var.content_length or "",
content_type = var.content_type or "",
--var.cookie_COOKIE or "",
document_root = var.document_root or "",
document_uri = var.document_uri or "",
host = var.host or "",
hostname = var.hostname or "",
--var.http_HEADER or "",
user_agent = var.http_user_agent or "",
is_args = var.is_args or "",
limit_rate = var.limit_rate or "",
nginx_version = var.nginx_version or "",
query_string = var.query_string or "",
remote_addr = var.remote_addr or "",
remote_port = var.remote_port or "",
remote_user = var.remote_user or "",
request_filename = var.request_filename or "",
request_body = var.request_body or "",
request_body_file = var.request_body_file or "",
request_completion = var.request_completion or "",
request_method = var.request_method or "",
request_uri = var.request_uri or "",
scheme = var.scheme or "",
server_addr = var.server_addr or "",
server_name = var.server_name or "",
server_port = var.server_port or "",
server_protocol = var.server_protocol or "",
uri = var.uri or ""
}
%>
<html>
<head>
</head>
<body>
<%=#array%>
<hr>
<table >
<% for k,v in pairs(array) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
</body>
</html>
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。