试用 openresty/lua-resty-shell
openresty/lua-resty-shell 是当前最新rc 版本内置的shell 功能,我们可以用来执行一个脚本,以及命令
还是比较方便的。
测试集成了一个oreilly电子书下载的功能
环境准备
- docker-compose 文件
version: "3"
services:
nginx:
build: ./
ports:
- "8888:8080"
env_file:
- .account.env
volumes:
- "./nginx_lua/:/opt/app/"
- "./ebooks/:/opt/ebooks/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
- dockerfile
FROM openresty/openresty:1.15.8.1rc1-bionic
LABEL author="1141591465@qq.com"
WORKDIR /app
RUN apt-get install -y wget \
&& wget -O safaribooks-downloader https://github.com/rongfengliang/My-SafariBooks-Downloader/raw/master/safaribooks-downloader-linux \
&& chmod +x safaribooks-downloader
ENV PATH=$PATH:/app
- 环境变量文件格式
环境变量配置主要是为了方便用户账户的管理,如果在请求体没有的话,可以使用环境变量内置的
.account.env
USERNAME=<yourid>
PASSWORD=<youpassword>
- nginx.conf
worker_processes 1;
user root;
events {
worker_connections 1024;
}
env USERNAME;
env PASSWORD;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
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;;';
server {
listen 8080;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location /ebooks {
root /opt;
autoindex on;
default_type application/octet-stream;
autoindex_exact_size off;
}
location /download {
content_by_lua_block {
require("api/download")()
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
lua-resty-shell 代码部分
主要是调用oreilly 电子书下载的cli 工具(原始使用nodejs 开发,为了方便我打包为了二进制文件)
nginx_lua/api/download.lua
-- this feature use lua-resty-shell call safaribooks-downloader to do
local json = require("cjson")
local shell = require "resty.shell"
local ngx = ngx;
local function exec_shell(ebookid,storagepath,username,password)
-- for simple use ebookid
local filename = ebookid..".epub"
local defaultstoragepath = (storagepath or "/app/")..filename
-- exec shell format -b ebookid -o output directory
-- safaribooks-downloader -b <ebookid> -u <id> -p <password> -o /Users/dalong/Desktop/testbook.epub
return "/app/safaribooks-downloader".." -b "..ebookid.." ".." -o "..defaultstoragepath.." -u "..username.." -p "..password.." && rm -rf books"
end
local function init()
ngx.req.read_body()
local method_name = ngx.req.get_method()
if method_name ~= "POST" then
ngx.say("must with post to pass datas")
end
local body = ngx.req.get_body_data()
if not body then
if ngx.req.get_body_file() then
return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'"
else
return ""
end
end
local downloadinfo = json.decode(body)
if downloadinfo ~=nil then
if downloadinfo.ebookid ==nil then
ngx.say("please pass ebook id ")
return nil
end
-- if not provide username && password use myself && set by os env
if downloadinfo.username == nil or downloadinfo.password == nil then
downloadinfo.username=os.getenv("USERNAME")
downloadinfo.password=os.getenv("PASSWORD")
end
end
local execommand = exec_shell(downloadinfo.ebookid,"/opt/ebooks/",downloadinfo.username,downloadinfo.password)
-- ngx.say(execommand)
local timeout = 300000 -- ms
local max_size = 409600 -- byte
-- shell 执行的核心
local ok, stdout, stderr, reason, status =
shell.run(execommand, nil, timeout, max_size)
if not ok then
return stderr
end
ngx.say(stdout..reason..status)
end
return init;
- 下载效果

- 查看下载的电子书
http://localhost:8888/ebooks/

说明
lua-resty-shell 使用起来还是很方便的,相对以前社区的基于配置socket 服务的模型,简化了好多,对于safaribooks-downloader-linux
这个文件的生成使用的是pkg nodejs 方便的打包工具,具体的可以参考项目源码。
参考资料
https://github.com/rongfengliang/safaribooks-downloader-docker-compose
https://github.com/rongfengliang/My-SafariBooks-Downloader
https://github.com/openresty/lua-resty-shell
试用 openresty/lua-resty-shell的更多相关文章
- openresty + lua 1、openresty 连接 mysql,实现 crud
最近开发一个项目,公司使用的是 openresty + lua,所以就研究了 openresty + lua.介绍的话,我就不多说了,网上太多了. 写这个博客主要是记录一下,在学习的过程中遇到的一些坑 ...
- openresty+lua+kafka方案与Tomcat接口并发度对比分析
1.openresty+lua+kafka 1.1 openresty+lua+kafka方案 之前的项目基于nginx反向代理后转发到Tomcat的API接口进行业务处理,然后将json数据打入ka ...
- Openresty+Lua+Kafka实现日志实时采集
简介 在很多数据采集场景下,Flume作为一个高性能采集日志的工具,相信大家都知道它.许多人想起Flume这个组件能联想到的大多数都是Flume跟Kafka相结合进行日志的采集,这种方案有很多他的优点 ...
- OpenResty Lua钩子调用完整流程
前面一篇文章介绍了Openresty Lua协程调度机制,主要关心的是核心调度函数run_thread()内部发生的事情,而对于外部的事情我们并没有涉及.本篇作为其姊妹篇,准备补上剩余的部分.本篇将通 ...
- openresty+lua在反向代理服务中的玩法
openresty+lua在反向代理服务中的玩法 phith0n · 2015/06/02 10:35 0x01 起因 几天前学弟给我介绍他用nginx搭建的反代,代理了谷歌和维基百科. 由此我想到了 ...
- Openresty Lua协程调度机制
写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...
- lua resty template && openresty 使用
1. 安装 luarocks install lua-resty-template 2. 使用 配置模板页面位置 有多种方式: a. 直接使用root 目录 代码如下: ...
- LUA+resty 搭建验证码服务器
使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...
- openresty lua 文件上传与删除
[1]openresty 上传upload源码库 Github:https://github.com/openresty/lua-resty-upload 源码文件upload.lua文件 [2]上传 ...
随机推荐
- 简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- VSTO:使用C#开发Excel、Word【6】
Office主互操作程序集(PIA)在了解如何构建Office解决方案之前,您需要更详细地了解在.NET中与Office对象模型通信的托管程序集.用于与Office通话的托管程序集称为Office主互 ...
- VBA续嘘嘘——宏技巧集绵
什么是VBA?它有什么作用? A.实现Excel中没有实现的功能. B.提高运行速度. C.编写自定义函数. D.实现自动化功能. E.通过插入窗体做小型管理软件. VBA在哪里存放的?怎么运行? A ...
- 4.8 C++ typeid操作符
参考:http://www.weixueyuan.net/view/6378.html 总结: typeid操作符用于判断表达式的类型,注意它和sizeof一样是一个操作符而不是函数. 如果需要使用t ...
- 5.Python爬虫入门五之URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...
- <Spark Streaming><Flume><Integration>
Overview Flume:一个分布式的,可靠的,可用的服务,用于有效地收集.聚合.移动大规模日志数据 我们搭建一个flume + Spark Streaming的平台来从Flume获取数据,并处理 ...
- linux的python版本升级
可利用Linux自带下载工具wget下载,如下所示: # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.13.tgz 下载完成后 ...
- snmp 简单网管协议
snmpget是取具体的OID的值.(适用于OID值是一个叶子节点的情况) snmpwalk snmpwalk — Fetch all the SNMP objects from an agent & ...
- MySQL笔记(2)
SQL 的约束 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性.. 1 约束分类 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性. ...
- meson 中调用shell script
meson 中有时需要调用其他脚本语言,加之对meson build system接口和原理不熟悉,无奈只有静心学习meson 官方文档,终于皇天不负有心人让我找到了: run_command() 只 ...