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 endlocal 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) endM.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 endM.startsWith = function(str,start_str)
return string.sub(str,1,string.len(start_str))==start_str endM.endsWith = function(str,end_str)
return string.sub(str,-string.len(end_str))==end_str endM.main = function()
local b = args["b"] local f = args["f"] local d = args["d"]if b == nil then
b = "" endif 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) endend
M.main()