• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

试用openresty/lua-resty-shell

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 
 
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua时间戳和时间互相转换发布时间:2022-07-22
下一篇:
lua_call/lua_pcall/xpcall发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap