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;
  • 下载效果

说明

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

  1. openresty + lua 1、openresty 连接 mysql,实现 crud

    最近开发一个项目,公司使用的是 openresty + lua,所以就研究了 openresty + lua.介绍的话,我就不多说了,网上太多了. 写这个博客主要是记录一下,在学习的过程中遇到的一些坑 ...

  2. openresty+lua+kafka方案与Tomcat接口并发度对比分析

    1.openresty+lua+kafka 1.1 openresty+lua+kafka方案 之前的项目基于nginx反向代理后转发到Tomcat的API接口进行业务处理,然后将json数据打入ka ...

  3. Openresty+Lua+Kafka实现日志实时采集

    简介 在很多数据采集场景下,Flume作为一个高性能采集日志的工具,相信大家都知道它.许多人想起Flume这个组件能联想到的大多数都是Flume跟Kafka相结合进行日志的采集,这种方案有很多他的优点 ...

  4. OpenResty Lua钩子调用完整流程

    前面一篇文章介绍了Openresty Lua协程调度机制,主要关心的是核心调度函数run_thread()内部发生的事情,而对于外部的事情我们并没有涉及.本篇作为其姊妹篇,准备补上剩余的部分.本篇将通 ...

  5. openresty+lua在反向代理服务中的玩法

    openresty+lua在反向代理服务中的玩法 phith0n · 2015/06/02 10:35 0x01 起因 几天前学弟给我介绍他用nginx搭建的反代,代理了谷歌和维基百科. 由此我想到了 ...

  6. Openresty Lua协程调度机制

    写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...

  7. lua resty template && openresty 使用

    1. 安装 luarocks install lua-resty-template 2. 使用   配置模板页面位置     有多种方式:   a.  直接使用root 目录     代码如下:    ...

  8. LUA+resty 搭建验证码服务器

    使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...

  9. openresty lua 文件上传与删除

    [1]openresty 上传upload源码库 Github:https://github.com/openresty/lua-resty-upload 源码文件upload.lua文件 [2]上传 ...

随机推荐

  1. 关于js的对象原型继承(二)

    本章讨论使用new一个构造函数来创建一个对象. 前期知识点说明: 1.prototype是函数的一个属性,每个函数都有一个prototype属性.这个属性是一个指针,指向一个对象.它是显示修改对象的原 ...

  2. DevExpress WinForms v18.2新版亮点(四)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  3. 谷歌开源的TensorFlow Object Detection API视频物体识别系统实现(一)[超详细教程] ubuntu16.04版本

    谷歌宣布开源其内部使用的 TensorFlow Object Detection API 物体识别系统.本教程针对ubuntu16.04系统,快速搭建环境以及实现视频物体识别系统功能. 本节首先介绍安 ...

  4. tf 版本更新 记录

    tf 经常更新版本,网上教程又是各版本都有,且不标明版本,致使各种用法难以分清哪个新,哪个旧,这里做个记录,以前的博客我就不更新了,请大家见谅. tf.nn.rnn_cell 改为 tf.contri ...

  5. Unity最新版打包AssetBundle和加载的方法

    一.设置assetBundleName二.构建AssetBundle包三.上传AssetBundle到服务器四.把AssetBundle放到本地五.操作AssetBundle六.完整例子七.Asset ...

  6. pycharm 永久解封

    第一步   c:\windows\system32\drivers\etc    命令行输入这个 第二步     把host文件复制到桌面 第三步   记事本打开host 第四步    在最下面添加  ...

  7. python储存数据的方式

    python储存数据的方式2017年10月13日 23:38:10 Nick_Spider 阅读数:59286 标签: redis 数据库 爬虫 存储 结构 更多 个人分类: 数据库 爬虫 pytho ...

  8. Java学习笔记26(异常)

    异常的定义: Java代码在运行过程中发生的问题就是异常 异常类:出现问题就会常见异常类对象,并抛出异常的相关信息,异常的位置,原因 异常体系: Throwable类是java中所有错误或异常的父类 ...

  9. excel单元格内容连接

    1.连接符号: & 举例子:C1= A1&B1 2.生成sql: CONCATENATE("(seq_table.nextval,sysdate, 'test',sysdat ...

  10. 使用Redis做为MySQL的缓存

    OS: Ubuntu 16.04.4 x64 更新并安装必要的工具 apt update && apt upgrade -y && apt dist-upgrade - ...