热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用OpenResty做一个postman小工具

文章目录用到的知识点参照mvc模型来做postman工具配置文件说明入口文件解析uri控制器模板代码使用方法lua_resty_template模板渲染方法模板语法遇到的坑项目地址


文章目录

    • 用到的知识点
    • 参照 mvc模型来做postman工具
      • 配置文件说明
      • 入口文件/解析uri
      • 控制器
      • 模板代码
    • 使用方法
      • lua_resty_template模板渲染方法
      • 模板语法
    • 遇到的坑
    • 项目地址



参考链接: https://www.shuzhiduo.com/A/QW5Y1LnK5m/

参考链接:https://www.shuzhiduo.com/A/QV5Z9YVnJy/


用到的知识点

mvc架构
模型渲染库:https://github.com/bungle/lua-resty-template
resty http请求库: https://github.com/bungle/lua-resty-template


参照 mvc模型来做postman工具


配置文件说明

worker_processes 1;
error_log logs/error.log;
events {worker_connections 1024;
}
http {lua_package_path "/open_resty/lualib/?.lua;/usr/local/openresty/lualib/?.lua;";server {listen 8080;default_type 'application/json;charset=utf8';lua_code_cache off;location / {content_by_lua_file mvc.lua;}location ~ ^/js/|^/css/|\.html {root static;}}
}

  • lua_code_cache off; 是为了修改lua脚本后不用重启nginx自动加载lua。
  • lua_package_path: lua脚本路径
  • root static: 静态目录是static

入口文件/解析uri

mvc.lua

#!/usr/bin/luangx.say("
")local uri = ngx.var.uri
ngx.header.content_type="application/json;charset=utf8"---- 默认首页
if uri == "" or uri == "/" thenlocal res = ngx.location.capture("/index.html",{})ngx.say(res.body)return
end--- uri解析
local m, err = ngx.re.match(uri, "([a-zA-Z0-9-]+)/*([a-zA-Z0-9-]+)*")
local moduleName = m[1] -- 模块名
local method = m[2] -- 方法名
if not method thenmethod = "index" -- 默认访问index方法
elsemethod = ngx.re.gsub(method, "-", "_")
end---动态Controller模块
-- 控制器默认在web包下面
local prefix = "web."
local path = prefix .. moduleName
-- 尝试引入模块,不存在则报错
local ret, ctrl, err = pcall(require, path)
local is_debug = true -- 调试阶段,会输出错误信息到页面上
if ret == false thenif is_debug thenngx.status = 404ngx.say("

Error: " .. ctrl .. " module not found !

")endngx.exit(404)
end-- 尝试获取模块方法,不存在则报错
local req_method = ctrl[method]
if req_method == nil thenif is_debug thenngx.status = 404ngx.say("

Error: " .. method .. "() method not found in " .. moduleName .. " lua module !

")endngx.exit(404)
end
-- 执行模块方法,报错则显示错误信息,所见即所得,可以追踪lua报错行数
ret, err = pcall(req_method)
if ret == false thenif is_debug thenngx.status = 404ngx.say("

Error: " .. err .. "

")elsengx.exit(500)end
end

控制器

控制器路径: lua/web/index.lua, lua/web/postman.lua
lua/web/index.lua 渲染模板 lua/tpl/index.html

local template = require "resty.template"
local _M = {}
function _M.index()local model = {reqUrl = "hello template", reqBody = "body"}-- 1、外部模板文件template.render("/index.html", model)
end
return _M

lua/web/postman.lua实现接收表单数据并处理Post逻辑

local template = require "resty.template"
local postman = require("cus.postman")local _M = {}
local function Parse()local model = {}local request_method = ngx.var.request_methodlocal args = nillocal Body = nillocal CurTime = nillocal CheckSum = nillocal MD5 = nillocal host = nilif "GET" == request_method thenargs = ngx.req.get_uri_args()elseif "POST" == request_method thenngx.req.read_body()--获取post请求的参数local post_args_tab = ngx.req.get_post_args()for k, v in pairs(post_args_tab) domodel[k]=vendBody = ngx.req.get_body_data()endmodel["resp"] = postman.httpPost(model["reqUrl"], model["reqBody"])return model
end
function _M.index()local model = Parse()-- 1、外部模板文件template.render("/index.html", model)end
return _M

模板代码

lua/tpl/index.html






PostMain

请求地址:
请求body:
返回值:


使用方法

访问地址: http://ip:8080/index/index
第一个index代表lua/web/index.lua
第二个index代表lua/web/index.lua文件中的index方法。


lua_resty_template模板渲染方法


  1. 使用template.render

local template = require "resty.template"
template.render("/index.html", model)

2.使用template.new

local template = require "resty.template" -- Using template.newlocal view = template.new "view.html" view.message = "Hello, World!" view:render()

  1. 使用template.compile

local template = require "resty.template"
local func = template.compile("view.html")
--执行函数,得到渲染之后的内容
local content = func(context) ngx.say(content)

模板语法


  • {{expression}}:输出传递的值,转义html相关标签
  • {*expression*}:输出传递的值
  • {% lua code %}:使用lua代码
  • {(template)}:引入html共用页面
  • {(base.html, { title = "Hello, World" } )}:引入html共用页面,并传递相关值
  • {-verbatim-}...{-verbatim-}/{-raw-}...{-raw-}:可原样输出模板语法
  • {# comments #}:在模板中使用注释,不会被执行和输出

遇到的坑

网页上直接输出了html字符串。而不是正常网页
解决办法
nginx.conf配置文件加上default_type text/html;

server {default_type text/html;location / {default_type text/html;}
}

入口文件mvc.lua开头加上下面的内容:

ngx.say("
")

项目地址

https://github.com/diycat1024/OpenRestyPostman


推荐阅读
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
author-avatar
wjr_l_be78e4
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有