functionisFile(name) iftype(name)~="string"thenreturnfalseend local f = io.open(name) if f then f:close() returntrue end returnfalse end
functionfile_exists_v2(file) -- some error codes: -- 13 : EACCES - Permission denied -- 17 : EEXIST - File exists -- 20 : ENOTDIR - Not a directory -- 21 : EISDIR - Is a directory -- local isok, errstr, errcode = os.rename(file, file) if isok == nilthen if errcode == 13then -- Permission denied, but it exists returntrue end returnfalse end returntrue end
local root_path = ngx.var.root_path print("Root Path: ", root_path)
functionreadFiles(projectid, version) print("Three segments: ", projectid, ", ", version) print("", root_path , appid, file_exists_v2(root_path .. projectid .. '/') ) if file_exists_v2(root_path .. projectid) == falsethen ngx.say('{"code":404}') else print("存在目录" .. root_path .. projectid) local idx_path = root_path .. projectid .. '/index' -- --目录存在,判断版本是否存在 if file_exists_v2(root_path .. projectid .. '/' .. version) then -- -- 读取 index idx_path = root_path .. projectid .. '/' .. version .. '/index' end print(idx_path) print(isFile(idx_path)) local file = io.open(idx_path, "r") -- -- -- 读取文件内容 local content = file:read("*all") ngx.say(content) file:close() end end -- -- 获取请求的 URI 路径 local uri = ngx.var.uri print(uri) --分割 URI 路径 local segments = {} for segment instring.gmatch(uri, "[^/]+") do table.insert(segments, segment) end --根据路径长度赋值变量 if #segments == 1then local var1 = segments[1] readFiles(var1, nil) elseif #segments >= 2then local var1 = segments[1] local var2 = segments[2] readFiles(var1, var2) end