博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Openresty Lua 合并 js 和 css
阅读量:7222 次
发布时间:2019-06-29

本文共 2453 字,大约阅读时间需要 8 分钟。

hot3.png

local str = require "resty.string"

ngx.header.content_type = "text/plain"

local static_root = ngx.var.static_root
local split_str = ","
local uri = ngx.var.uri
local args = ngx.req.get_uri_args()

local M = {

    log_level = ngx.NOTICE,
    _VERSION = "2017.07.01",
}

--[[

    string msg 
    string level => ngx.STDERR , ngx.EMERG , ngx.ALERT , ngx.CRIT , ngx.ERR , ngx.WARN , ngx.NOTICE , ngx.INFO , ngx.DEBUG
    nginx => log file
]]
M.log = function (msg, level)
    level = level or M.log_level
    ngx.log(level, msg)
end

M.uri_split = function (str, split_char)
    local sub_str_tab = {}
    local i = 0
    local j = 0
    while true do
        j = string.find(str, split_char, i+1)
        if j == nil then
            if(table.getn(sub_str_tab)==0) then
                table.insert(sub_str_tab, str)
            end
            break
        end
        table.insert(sub_str_tab, string.sub(str, i+1, j-1))
        i = j
    end
    return sub_str_tab
end

M.write_file = function(bname,filenames,debug)
    local data = ""
    local parts = M.uri_split(filenames, split_str)
    -- read table and get static data
    local content_type = nil
    for key, value in ipairs(parts) do
        content_type = M.getContentType(value)
        if content_type == nil then
            ngx.say("//"..value.." not found,please check 'f' ")
            return
        end

        local fp1, err = io.open(static_root.."/"..bname.."/"..value, "r")

        if fp1~=nil then
            data = data..fp1:read("*all")
            fp1:close()
        else
            local http_referer = ngx.var.http_referer
            if http_referer == nil then
                http_referer = ""
            end
            M.log(value.." not found referer="..http_referer, ngx.ERR)
            if debug ~= nil then
                ngx.say("//"..value.." not found")
                return
            end
        end
    end
    ngx.header.content_type = content_type
    ngx.say(data)
end

M.getContentType = function(filename) 

    if M.endsWith(filename,'.js') == true then
        return "application/javascript"
    elseif M.endsWith(filename,'.css') == true then
        return "text/css"
    else
        return nil
    end 
end

M.startsWith = function(str,start_str)

    return string.sub(str,1,string.len(start_str))==start_str
end

M.endsWith = function(str,end_str)

   return string.sub(str,-string.len(end_str))==end_str
end

M.main = function()

    local b = args["b"]
    local f = args["f"]
    local d = args["d"]

    if b == nil then

        b = ""
    end

    if f == nil then

        M.log("args error uri = "..uri..",b="..b..",f=nil", ngx.ERR)
        ngx.say('// min uri error useage: //host/index.js?b=js&f=common/index.js,common/list.js')
    else
        if M.endsWith(f,split_str) == false then
            f = f..split_str
        end
      --  M.log(" uri = "..uri..",b = "..b..",f="..f, ngx.ERR)
        M.write_file(b,f,d)
    end

end

M.main()

转载于:https://my.oschina.net/u/3231866/blog/1186517

你可能感兴趣的文章
在客户端显示服务器端任务处理进度条
查看>>
查找最相近的字符串
查看>>
map的运用
查看>>
mybatis--MapperScannerConfigurer
查看>>
【笔记】mysql两条数据的某个属性值互换
查看>>
leetcode—Populating Next Right Pointers in Each Node
查看>>
python发起请求提示UnicodeEncodeError
查看>>
文件夹搜索不能用【弹出意外错误,操作无法实现】如何解决呢
查看>>
C程序的存储空间布局
查看>>
OpenStack 的防火墙规则流程
查看>>
环境变量 安装SU
查看>>
请注意,再次记住, centos7,fedora 24中 没有iptables服务, 而使用的firewalld, 也可以安装 iptables-services程序来实现...
查看>>
Overloading Django Form Fields
查看>>
JVM内幕:Java虚拟机详解
查看>>
An incompatible version 1.1.14 of APR based Apache Tomcat Native library is installed, while Tomcat
查看>>
高并发与负载均衡-nginx-反向代理概念
查看>>
Easyui DataGrid DateRange Filter 漂亮实用的日期区间段筛选功能
查看>>
03.MyBatis的核心配置文件SqlMapConfig.xml
查看>>
phpcms 整合 discuz!
查看>>
转:说说JSON和JSONP
查看>>