openresty router && template 试用
router 是一个比较方便的 openresty 路由组件,我们可以用来编写灵活强大的 web 应用,类似的
lua-resty-route 也是很不错的,但是如果是比较简单的直接可以使用 lua-resty-template
备注: 测试环境使用docker-compose
环境准备
- docker-compose 文件
version: "3"
services:
router:
build: ./
volumes:
- "./nginx_lua/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
ports:
- "8080:80"
- nginx.conf
包含了template 以及router 的使用
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
lua_need_request_body on;
gzip on;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
init_by_lua_block {
template = require "resty.template"
}
server {
listen 80;
server_name localhost;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
content_by_lua_block {
require("web/init")()
}
}
location /userlogin {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/jquery")();
}
}
location /usercom {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/com")();
}
}
location = /favicon.ico {
root /opt/app/static;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- dockerfile
安装lua 包
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install luacheck
RUN /usr/local/openresty/luajit/bin/luarocks install busted
RUN /usr/local/openresty/luajit/bin/luarocks install luacov
RUN /usr/local/openresty/luajit/bin/luarocks install luacov-coveralls
RUN /usr/local/openresty/luajit/bin/luarocks install router
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-template
EXPOSE 80
router && template 代码
- router
nginx_lua/web/init.lua router 的路由编写还是很清晰的,按照http verb 很清晰
local router = require 'router'
function init()
local r = router.new()
r:match({
GET = {
["/hello"] = function(params) ngx.print("someone said hello") end,
["/hello/:name"] = function(params) ngx.print("hello, " .. params.name) end,
["/"] = function(params) ngx.say([[demo app rong]]) end
},
POST = {
["/app/:id/comments"] = function(params)
ngx.print("comment " .. params.comment .. " created on app " .. params.id)
end
}
})
local ok, errmsg = r:execute(
ngx.var.request_method,
ngx.var.request_uri,
ngx.req.get_uri_args(), -- all these parameters
ngx.req.get_post_args(), -- will be merged in order
{other_arg = 1}) -- into a single "params" table
if ok then
ngx.status = 200
else
ngx.status = 404
ngx.print("Not found!")
ngx.log(ngx.ERROR, "some wrong")
end
end
return init
template 代码,这个主要是需要配置几变量,指定模板文件的位置,如下:
location /userlogin {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/jquery")();
}
}
template render 代码,template 使用了全局变量,在init 阶段初始化
init_by_lua_block {
template = require "resty.template"
}
render 处理
nginx_lua/web/juqey.lua
function init()
template.render(
"index.html",
{
message = "Hello, World!",
jquery = '<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>'
}
)
end
return init
view 模板
nginx_lua/static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index page demo</title>
</head>
<body>
{{message}}
{*jquery*}
<script >
$(document).ready(function(){
alert("is ok")
})
</script>
</body>
</html>
运行效果
- 启动
docker-compose up -d
- 路由测试效果
http://localhost:8080/
router:
curl -i http://localhost:8080
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Thu, 24 Jan 2019 00:29:35 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
demo app rong
view template:
curl -i http://localhost:8080/userlogin
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Thu, 24 Jan 2019 00:30:19 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index page demo</title>
</head>
<body>
Hello, World!
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script >
$(document).ready(function(){
alert("is ok")
})
</script>
</body>
说明
openresty 的开发还是很方便的,可以帮助我们解决好多实际中的问题。简单高效
参考资料
https://github.com/rongfengliang/openrety-router-docker-compose
https://github.com/bungle/lua-resty-route
https://github.com/APItools/router.lua
https://github.com/bungle/lua-resty-template