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
openresty router && template 试用的更多相关文章
- skipper http router 简单试用
说明: 使用源码编译,注意需要FQ,以及golang版本的问题,新版使用的是go mod 进行依赖管理 环境准备 clone 代码 git clone https://github.com/zalan ...
- vue router路由(三)
当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) no ...
- Vue Router的配置
1.beforeEnter function requireAuth (route, redirect, next) { if (!auth.loggedIn()) { redirect({ path ...
- Vue全家桶(Vue-cli、Vue-route、vuex)
摘要 学习本篇之前要具备一定的vue基础知识,可以先看一下Vue基础(环境配置.内部指令.全局API.选项.内置组件) 1.Vue-cli Vue-cli是vue官方出品的快速构建单页应用的脚手架,这 ...
- 重开Vue2.0
目录: 内容: 一.Vue内部指令: 1.v-if v-else&v-show v-if与v-show都是选择性显示内容的指令,但是二者之间有区别: 1.v-if:判断是否加载,在需要的时候加 ...
- vue-cli 脚手架分析
Vue-cli 一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v 检测你是否安装了npm和 ...
- vue-cli 搭建
一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v 检测你是否安装了npm和版本情况.出现版 ...
- Vue.js学习笔记(2)vue-router
vue中vue-router的使用:
- CloudStack全局参数
{ "listconfigurationsresponse": { "count": 305, "config ...
随机推荐
- nginx负载均衡实验
Nginx负载均衡概述 Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现 ...
- IOCP IO完成端口
一. IO完成端口概念 IO完成端口的出现是为了解决并发模型中可运行线程上下文切换开销过大而出现的. 在<Windows核心编程>的描述中,IO完成端口是Wnidows系统提供的最复杂的内 ...
- DevExpress ASP.NET Core Controls v18.2新功能详解
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Core ...
- 漫步Java------接口
接口 一.定义 具有相同行为(方法),但是不相关的类 二.特点 只是提供方法,不定义方法的具体实现. 一个类只能继承一个父类,但是接口却可以继承多个接口. 接口是一个引用类型的变量 接口没有构造方法, ...
- git 实现提交远程分支步骤
git clone git branch [分支名] 创建分支 git branch 查看本地所有分支 git checkout [分支名称] 切换分支 ---写代码--- git status (查 ...
- C++ SUBLIME TEXT3 环境配置
一.第一种方法 1.New Build System,设置C++.sublime-build编译文件 { "path": "E:\\MinGW\\bin", & ...
- python day07作业
- reset.css 文件
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...
- 【Python】多线程-2
1. 进程和线程的区别: (1) 一个进程可以有多个线程,一个进程中的多个线程共享该进程的所有资源,多线程切换比多进程切换快,因为不用上下文切换,Python中并发建议用多进程 (2) 进程是资 ...
- sublime text3 key
Sublime Text 3 3126 注册码 第一个测试通过 —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8 ...