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 ...
随机推荐
- JavaScript 操作DOM对象
1)JavaScript 操作DOM對象 1.DOM:是Document Object Model 的缩写,及文档对象模型 2.DOM通常分为三类:DOM Core(核心).HTML-DOM 和 ...
- 顺便谈谈对于Java程序猿学习当中各个阶段的建议
引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...
- Linux如何产看系统信息
如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...
- delete和delete[] 区别
// DeleteAndDelete[].cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> ...
- JS日期工具类(转)
javascript Date format(js日期格式化) https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.htm ...
- 安装google 框架
使用 root exporer很方便 su cp /sdcard/google/busybox /data/local/tmp chmod 0755 /data/local/tmp/busybo ...
- jquery转换json对象为字符串
jquery转换json对象为字符串 JSON.stringify(jsonObject),可用于单个JSON对象,也可用于JSON数组 alert(JSON.stringify(jsonObject ...
- ubantu 安装mysql 5.7 解决安装不提示设置密码问题
1.安装Mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install l ...
- python之pandas简单介绍及使用(一)
python之pandas简单介绍及使用(一) 一. Pandas简介1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据 ...
- 【Python】练习题
练习1:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中 import os file1_path="e:\\test3\\2.t ...