美文网首页
lua基础学习

lua基础学习

作者: 分裂四人组 | 来源:发表于2016-04-19 23:46 被阅读83次

    参考酷壳大叔的博客,http://coolshell.cn/articles/10739.html, 自己搭建了一套nginx+lua的环境,学习了一下的case:

    --nginx变量  
    local var = ngx.var  
    ngx.say("ngx.var.a : ", var.a)  
    ngx.say("ngx.var.b : ", var.b)  
    
    --[[
    ngx.say("ngx.var[2] : ", var[2])  
    ngx.var.b = 2;  
    
    --请求头  
    local headers = ngx.req.get_headers()  
    ngx.say("headers begin")  
    ngx.say("Host : ", headers["Host"])  
    ngx.say("user-agent : ", headers["user-agent"])  
    ngx.say("user-agent : ", headers.user_agent)  
    for k,v in pairs(headers) do  
        if type(v) == "table" then  
            ngx.say(k, " : ", table.concat(v, ","))  
        else  
            ngx.say(k, " : ", v)  
        end  
    end  
    ngx.say("headers end")  
      
    --get请求uri参数  
    ngx.say("uri args begin")  
    local uri_args = ngx.req.get_uri_args()  
    for k, v in pairs(uri_args) do  
        if type(v) == "table" then  
            ngx.say(k, " : ", table.concat(v, ", "))  
        else  
            ngx.say(k, ": ", v)  
        end  
    end  
    ngx.say("uri args end")  
      
    --post请求参数  
    ngx.req.read_body()  
    ngx.say("post args begin")  
    local post_args = ngx.req.get_post_args()  
    for k, v in pairs(post_args) do  
        if type(v) == "table" then  
            ngx.say(k, " : ", table.concat(v, ", "))  
        else  
            ngx.say(k, ": ", v)  
        end  
    end  
    ngx.say("post args end")  
    
    --[[
    ngx.say("ngx.req.http_version : ", ngx.req.http_version())   
    ngx.say("ngx.req.get_method : ", ngx.req.get_method())  
    --
    ngx.say("ngx.req.raw_header : ",  ngx.req.raw_header())  
    --
    ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data())  
    --]]
    
    
    --learn table
    table1 = {[1]="2", [3]="3", ["name"] = "lishuming01"}
    
    --ngx.say(table.concat(table1, " "))
    
    for  k, v in pairs(table1) do
      ngx.say(k,"->",v)
    end
    
    --learn array
    arr = {1,3,5,7,9}
    for i=1, #arr do
        ngx.say(arr[i])
    end
    
    --learn metatable
    
    f_op = {}
    function f_op.__add(f1,f2)
        ret = {}
        ret.n = f1.n * f2.d + f1.d * f2.n
        ret.d = f1.d * f2.d
        return ret
    end
    f_op1 = {n=2, d=3}
    f_op2 = {n=3, d=6}
    setmetatable(f_op1, f_op)
    setmetatable(f_op2, f_op)
    ret = f_op1 + f_op2
    for  k, v in pairs(ret) do
      ngx.say(k,"->",v)
    end
    
    
    
    --lean class
    Person = {}
    
    function Person:new( p )
        -- body
        local obj = p
        if (p == nil)then
            obj = {name="person", age=0}
        end
        self.__index = self
        return setmetatable(obj, self)
    end
    
    function Person:to_string()
        -- body
        return self.name .. ":" .. self.age
    end
    
    man1 = Person:new()
    ngx.say(man1:to_string())
    
    
    --todo: learn module
    
    
    

    相关文章

      网友评论

          本文标题:lua基础学习

          本文链接:https://www.haomeiwen.com/subject/stezlttx.html