试用 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]上传 ... 
随机推荐
- 《Python》常用模块之collections模块
			内置的数据类型: int float complex str list tuple dict set 基础数据类型: int float complex str list tuple ... 
- Linux3.10.0块IO子系统流程(3)-- SCSI策略例程
			很长时间以来,Linux块设备使用了一种称为“蓄流/泄流”(plugging/unplugging)的技术来改进吞吐率.简单而言,这种工作方式类似浴盆排水系统的塞子.当IO被提交时,它被储存在一个队列 ... 
- Bluedroid: 蓝牙协议栈源码剖析
			一. 基础知识介绍 1.缩略语 BTIF: Bluetooth Interface BTU : Bluetooth Upper Layer BTM: Bluetooth Manager BTE: Bl ... 
- web 架构 /http协议,状态码,django中常用命令
			什么是web应用? web应用 架构 :B/S架构 | C/S架构 网站:BS架构其实就是应用程序: B是浏览器 S是sever(实现了wsgi协议,实现了socket的服务端) + applicat ... 
- this是什么!
			this 1.js的关键字指定一个对象,然后去替代他 函数内的this 函数外的this 函数内的this指向行为发生的主体 函数外的this都指向window 2.函数内的this和函数在什么 ... 
- HDU 6055 17多校 Regular polygon(计算几何)
			Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure ou ... 
- MySQL数据库优化的方式
			1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ... 
- Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache
			文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ... 
- [转] 带你彻底理解RSA算法原理
			http://blog.csdn.net/dbs1215/article/details/48953589 1. 什么是RSA RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法 ... 
- php操作mysql几个常用操作
			1.链接数据库 mysql_connet('数据库地址','数据库账号','数据库密码'); 2.选择数据库 mysql_select_db("数据库名"); 3.设置编码 mys ... 
