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
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 试用的更多相关文章

  1. skipper http router 简单试用

    说明: 使用源码编译,注意需要FQ,以及golang版本的问题,新版使用的是go mod 进行依赖管理 环境准备 clone 代码 git clone https://github.com/zalan ...

  2. vue router路由(三)

    当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) no ...

  3. Vue Router的配置

    1.beforeEnter function requireAuth (route, redirect, next) { if (!auth.loggedIn()) { redirect({ path ...

  4. Vue全家桶(Vue-cli、Vue-route、vuex)

    摘要 学习本篇之前要具备一定的vue基础知识,可以先看一下Vue基础(环境配置.内部指令.全局API.选项.内置组件) 1.Vue-cli Vue-cli是vue官方出品的快速构建单页应用的脚手架,这 ...

  5. 重开Vue2.0

    目录: 内容: 一.Vue内部指令: 1.v-if v-else&v-show v-if与v-show都是选择性显示内容的指令,但是二者之间有区别: 1.v-if:判断是否加载,在需要的时候加 ...

  6. vue-cli 脚手架分析

    Vue-cli 一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和 ...

  7. vue-cli 搭建

    一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和版本情况.出现版 ...

  8. Vue.js学习笔记(2)vue-router

    vue中vue-router的使用:

  9. CloudStack全局参数

    {     "listconfigurationsresponse": {         "count": 305,         "config ...

随机推荐

  1. 理解JavaScript的运行

    JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...

  2. 七. Python基础(7)--文件的读写

    七. Python基础(7)--文件的读写 1 ● 文件读取的知识补充 f = open('file', encoding = 'utf-8') content1 = f.read() content ...

  3. VSTO:使用C#开发Excel、Word【6】

    Office主互操作程序集(PIA)在了解如何构建Office解决方案之前,您需要更详细地了解在.NET中与Office对象模型通信的托管程序集.用于与Office通话的托管程序集称为Office主互 ...

  4. html 经验之谈

  5. Linux alias别名命令

    首先介绍一下命令的别名,怎么查看的呢? 咱们使用which命令就可以查看的到它完整的命令是怎样的 [root@master ~]# which ls alias ls='ls --color=auto ...

  6. angular2的依赖注入

    更好阅读体验,请看原文 在读这篇文章之前,你要先了解一下什么是依赖注入,网上关于这个的解释很多,大家可以自行Google. 我们这一篇文章还是以QuickStart项目为基础,从头开始讲解怎么在Ang ...

  7. 牛客第三场多校 E Sort String

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...

  8. day 51

    一 window对象 window 对象表示一个浏览器窗口. 在客户端 JavaScript 中,Window 对象是全局对象,所有的表达式都在当前的环境中计算.也就是说,要引用当前窗口根本不需要特殊 ...

  9. chromium ⑤

    我们都知道chromium是用webkit完成页面显示的,   那么chromium是怎样集成和封装webkit的呢?   是怎样将webkit整合到自己的框架中,并将一个页面渲染出来的?   这篇我 ...

  10. day 03 变量与基本数据类型

    变量的命名规范 一:变量命名的大前提,应该能够反映出变量值所记录内容 1:变量名只能由数字,字母,下划线组成 2:变量名不能以数字开头 3:变量名不能使用系统的关键字,不然可能会报错 二:变量名的命名 ...