zoukankan      html  css  js  c++  java
  • lua lor mongol socket.http

    local lor = require("lor.index")
    local cjson = require("cjson")
    local template = require("resty.template")
    local mongo = require ("resty.mongol")
    
    local http = require("socket.http")
    local ltn12 = require("ltn12")
    
    conn = mongo:new()
    conn:set_timeout(1000)
    ok, err = conn:connect("127.0.0.1", 27017)
    if not ok then
        print("connect failed: "..err)
    end
    
    local db = conn:new_db_handle("fancy")
    
    local app = lor()
    
    local example = "/home/an/lua_project/app/views/example/"
    
    -- 模板配置
    app:conf("view enable", true)
    app:conf("view engine", "tmpl")
    app:conf("view ext", "php")
    app:conf("views", "/home/an/lua_project/app/views/example/")
     
    function Split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
        if not nFindLastIndex then
             nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
             break
        end
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
        nFindStartIndex = nFindLastIndex + string.len(szSeparator)
        nSplitIndex = nSplitIndex + 1
        end
    return nSplitArray
    end
    
    function tableToString(studentNum)
         local str =  "{ "
         str = str.."
    "
         for k, v in pairs(studentNum) do
            if type(v) ~= "table" then
                str = str.."[""..k..""]"
                str = str..":"
                str = str..v
                str = str..","
                str = str.."
    "
            else
                str = str.."[""..k..""]"
                str = str..":"
                str = str..tableToString(v)
                str = str..","
                str = str.."
    "
            end
         end
         str = string.sub(str, 1, -3)
         str = str.."
    "
         str = str .." }"
         return str
    end
    
    function get_req(url, headers)
        local response_body = {}
        response, status_code, headers = http.request {
                                method = "GET",
                                url = url,
                                headers = headers,
                                 sink = ltn12.sink.table(response_body),       
                               }
        if status_code == 200 then                      
            return response_body
        else
            return nil
        end
    end
    
    function post_req(url, headers, postdata_json)
        local response_body = {}
        headers["content-length"] = string.len(postdata_json)
        response, status_code, headers = http.request {
                                method = "POST",
                                url = url,
                                source = ltn12.source.string(postdata_json),
                                headers = headers,
                                sink = ltn12.sink.table(response_body),       
                               }
        if status_code == 200 then                      
            return response_body
        else
            return nil
        end
    end
    
    function get_demo(post_data)
        local url = "http://127.0.0.1:8000/v1/get"
    
        return nil
    end
    
    function post_demo(post_data, headers)
        local url = "http://127.0.0.1:8000/v1/post/ajax"
        local postdata_json = cjson.encode(post_data)
        local response_body = post_req(url, headers, postdata_json)
        if response_body then
            return response_body
        else    
            return nil
        end    
    end    
    
    function post_downloads_html(post_data, headers)
        local url = "http://127.0.0.1:8000/v1/downloads_html"
        local postdata_json = cjson.encode(post_data)
        local response_body = post_req(url, headers, postdata_json)
        if response_body then
            return response_body
        else    
            return nil
        end    
    end    
    
    app:get("/post/test", function(req, res, next)
        local he = req.headers
        local url = "http://127.0.0.1:8000/v1/post"
        local host = Split(url, "/")[3]
        local postdata = {}
        postdata["test"] = 1
        local postdata_json = cjson.encode(postdata)
        headers = {["Host"] = host,
                   ["Accept"] = "*/*",
                   ["Accept-Encoding"] = "gzip, deflate",
                   ["Referer"] = he["Referer"],
                   ["Content-Type"] = "application/json",
                   ["content-length"] = string.len(postdata_json),
                   ["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/73.0.3683.103 Chrome/73.0.3683.103 Safari/537.36",
                  }
        local response_body = post_req(url, headers, postdata_json)
        if response_body then                      
            res:send(response_body[1])
            --res:send("hello world! body: "..tableToString(response_body))
        else
            res:send("get url is nil")
        end    
    end)
    
    app:get("/", function(req, res, next)
        res:render("example/index", {
        })
    end)
    
    function file_exists(path)
          local file = io.open(path, "rb")
          if file then file:close() end
          return file ~= nil
    end
    
    local function read_files(fileName)
        local f = assert(io.open(fileName, 'r'))
        local content = f:read("*all")f:close()
        return content
    end
    
    app:get("/market/index.php", function(req, res, next)
        local query = req.query
        local month = query.month
        local uri = req.origin_uri
        local file_ = Split(uri, "?")[2]
        if not file_ then
            res:render("market/index", {})
        else
            local file_name = string.format("%stest/index.php_%s", example, file_)
            local status = file_exists(file_name)
            if status == false then
                local post_ = {}
                post_["url"] = uri
                local dowon = post_downloads_html(post_, req.headers)
            end
    
            local html_body = read_files(file_name)
            res:html(html_body)
        end
    end)
    app:post("/ajax/get_data_format.php", function(req, res, next)
        local post_data_ = ngx.req.get_body_data()
        local get_data_format = db:get_col("get_data_format")
        find_data_ = get_data_format:find_one({post_data=post_data_})
        if find_data_ then
            res:status(200):send(find_data_["data"])
        else 
            local post_ = {}
            local key = "get_data_format"
            post_["key"] = key
            post_["post_data"] = post_data_
            local data_ = post_demo(post_, req.headers)
            res:status(200):send(data_)
        end
    end)
  • 相关阅读:
    Linux MySQL的root无法登录数据库ERROR 1045 (28000)
    Linux 命令行初始化MySQL数据库
    Centos6、Centos7防火墙基本操作整理
    文本三剑客之awk
    加密类型、数据加密解密过程以及CA创建
    Centos7启动流程及systemd中Nginx启动配置
    Linux任务计划
    Linux进程管理
    IP地址简介及Linux网络管理工具
    Linux脚本
  • 原文地址:https://www.cnblogs.com/dockers/p/10857951.html
Copyright © 2011-2022 走看看