OpenResty 开发

ngx修改上传文件大小 http{ client_max_body_size 20m; }

ngx 跨域配置
server{
        ##-- start 设置跨域--
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,Sign,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        add_header 'Access-Control-Max-Age' 1728000;
        ##add_header 'Content-Type' 'application/x-www-form-urlencoded charset=UTF-8';
        ##add_header 'Content-Length' 0;
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        ##-- end 设置跨域--
}

lor 中到坑 post 请求

function Request:new()
    local body = {} -- body params
    local headers = ngx.req.get_headers()

    local header = headers['Content-Type']
    -- the post request have Content-Type header set
    if header then
        if sfind(header, "application/x-www-form-urlencoded", 1, true) then
            ngx.req.read_body()
            local post_args = ngx.req.get_post_args()
            if post_args and type(post_args) == "table" then
                for k,v in pairs(post_args) do
                    body[k] = v
                end
            end
        elseif sfind(header, "application/json", 1, true) then
            ngx.req.read_body()
            local json_str = ngx.req.get_body_data()
            body = utils.json_decode(json_str)
        -- form-data request
        elseif sfind(header, "multipart", 1, true) then
            -- upload request, should not invoke ngx.req.read_body()
        -- parsed as raw by default
        else
            ngx.req.read_body()
            body = ngx.req.get_body_data()
            --body 有可能为nil 上传大于1M的数据为nil 
            --TODO ZGT修改的
            local function getFile(file_name)
                local f = assert(io.open(file_name, 'r'))
                local string = f:read("*all")
                f:close()
                return string
            end
            if nil == body then
                local file_name = ngx.req.get_body_file()
                if file_name then
                    body = getFile(file_name)
                end
            end

        end
    -- the post request have no Content-Type header set will be parsed as x-www-form-urlencoded by default
    else
        ngx.req.read_body()
        local post_args = ngx.req.get_post_args()
        if post_args and type(post_args) == "table" then
            for k,v in pairs(post_args) do
                body[k] = v
            end
        end
    end

    local instance = {
        path = ngx.var.uri, -- uri
        method = ngx.req.get_method(),
        query = ngx.req.get_uri_args(),
        params = {},
        body = body,
        body_raw = ngx.req.get_body_data(),
        url = ngx.var.request_uri,
        origin_uri = ngx.var.request_uri,
        uri = ngx.var.request_uri,
        headers = headers, -- request headers

        req_args = ngx.var.args,
        found = false -- 404 or not
    }
    setmetatable(instance, { __index = self })
    return instance
end

正则表达式缓存大小
lua_regex_cache_max_entries 1024;

lua 配置路径问题添加 $prefix 对应的lua 为 ngx.config.prefix()

lua 进制转化
_convertTable = {
   [0] = "0",
   [1] = "1",
   [2] = "2",
   [3] = "3",
   [4] = "4",
   [5] = "5",
   [6] = "6",
   [7] = "7",
   [8] = "8",
   [9] = "9",
   [10] = "a",
   [11] = "b",
   [12] = "c",
   [13] = "d",
   [14] = "e",
   [15] = "f",
   [16] = "g",
   [17] = "h",
   [18] = "i",
   [19] = "j",
   [20] = "k",
   [21] = "l",
   [22] = "m",
   [23] = "n",
   [24] = "o",
   [25] = "p",
   [26] = "q",
   [27] = "r",
   [28] = "s",
   [29] = "t",
   [30] = "u",
   [31] = "v",
   [32] = "w",
   [33] = "x",
   [34] = "y",
   [35] = "z",
   [36] = "A",
   [37] = "B",
   [38] = "C",
   [39] = "D",
   [40] = "E",
   [41] = "F",
   [42] = "G",
   [43] = "H",
   [44] = "I",
   [45] = "J",
   [46] = "K",
   [47] = "L",
   [48] = "M",
   [49] = "M",
   [50] = "O",
   [51] = "P",
   [52] = "Q",
   [53] = "R",
   [54] = "S",
   [55] = "T",
   [56] = "U",
   [57] = "V",
   [58] = "W",
   [59] = "X",
   [60] = "Y",
   [61] = "Z",
}

local function Convert(dec,x)
     local function fn(num,t)
        if(num < x) then
            table.insert(t,num)
        else
          fn(math.floor(num/x),t)
          table.insert(t,num%x)
        end
     end

     local x_t = {}
     fn(dec,x_t,x)
     return x_t
end


function ConvertDec2x(dec,x)
      local x_t = Convert(dec,x)
      local text = ""
      for k,v in pairs(x_t) do
         text = text .. _convertTable[v]
      end
      return text
end
print(ConvertDec2x(3703473959,2))
print(ConvertDec2x(3703473959,10))
print(ConvertDec2x(3703473959,16))
print(ConvertDec2x(3703473959,36))
print(ConvertDec2x(3703473959,62))
java 的进制转化
public static final char chars[] = {'0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    };


    private  static  void fn(ArrayList<Long> x_t,long num,int x){
        if (num < x){
            x_t.add(num);
        }else {
            fn(x_t,(long) Math.floor(num/x),x);
            x_t.add(num%x);
        }
    }

    public static String convert(long dec,int x){
        //System.out.println(chars.length);
        ArrayList<Long> x_t = new ArrayList<>();
        fn(x_t,dec,x);
       // System.out.println(x_t);
        StringBuilder builder = new StringBuilder();

        for (Long l:x_t) {
            int i = Math.toIntExact(l);
            char c = chars[i];
            builder.append(c);
        }
        return builder.toString();
    }
    System.out.println(chars.length);
    String str = convert(3703473959L,2);
    System.out.println(str);
    System.out.println(convert(3703473959L,10));
    System.out.println(convert(3703473959L,36));
    System.out.println(convert(3703473959L,62));
    java 自带的36进制
    String ipHex =  new BigInteger(String.valueOf(3703473959L)).toString(36);

猜你喜欢

转载自blog.csdn.net/zhangguangtao1207/article/details/81109093