<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] 參考鏈接: https://www.shuzhiduo.com/A/QW5Y1LnK5m/ 參考鏈接:https://www.shuzhiduo.com/A/QV5Z9YVnJy/ ## 用到的知識點 mvc架構 模型渲染庫:https://github.com/bungle/lua-resty-template resty http請求庫: https://github.com/bungle/lua-resty-template ## 參照 mvc模型來做postman工具 ### 配置文件說明 ``` worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { lua_package_path "/open_resty/lualib/?.lua;/usr/local/openresty/lualib/?.lua;"; server { listen 8080; default_type 'application/json;charset=utf8'; lua_code_cache off; location / { content_by_lua_file mvc.lua; } location ~ ^/js/|^/css/|\.html { root static; } } } ``` * lua_code_cache off; 是為了修改lua腳本后不用重啟nginx自動加載lua。 * lua_package_path: lua腳本路徑 * root static: 靜態目錄是static ### 入口文件/解析uri mvc.lua ``` #!/usr/bin/lua ngx.say("<br/>") local uri = ngx.var.uri ngx.header.content_type="application/json;charset=utf8" ---- 默認首頁 if uri == "" or uri == "/" then local res = ngx.location.capture("/index.html",{}) ngx.say(res.body) return end --- uri解析 local m, err = ngx.re.match(uri, "([a-zA-Z0-9-]+)/*([a-zA-Z0-9-]+)*") local moduleName = m[1] -- 模塊名 local method = m[2] -- 方法名 if not method then method = "index" -- 默認訪問index方法 else method = ngx.re.gsub(method, "-", "_") end ---動態Controller模塊 -- 控制器默認在web包下面 local prefix = "web." local path = prefix .. moduleName -- 嘗試引入模塊,不存在則報錯 local ret, ctrl, err = pcall(require, path) local is_debug = true -- 調試階段,會輸出錯誤信息到頁面上 if ret == false then if is_debug then ngx.status = 404 ngx.say("<p style='font-size: 50px'>Error: <span style='color:red'>" .. ctrl .. "</span> module not found !</p>") end ngx.exit(404) end -- 嘗試獲取模塊方法,不存在則報錯 local req_method = ctrl[method] if req_method == nil then if is_debug then ngx.status = 404 ngx.say("<p style='font-size: 50px'>Error: <span style='color:red'>" .. method .. "()</span> method not found in <span style='color:red'>" .. moduleName .. "</span> lua module !</p>") end ngx.exit(404) end -- 執行模塊方法,報錯則顯示錯誤信息,所見即所得,可以追蹤lua報錯行數 ret, err = pcall(req_method) if ret == false then if is_debug then ngx.status = 404 ngx.say("<p style='font-size: 50px'>Error: <span style='color:red'>" .. err .. "</span></p>") else ngx.exit(500) end end ``` ### 控制器 控制器路徑: lua/web/index.lua, lua/web/postman.lua lua/web/index.lua 渲染模板 lua/tpl/index.html ``` local template = require "resty.template" local _M = {} function _M.index() local model = {reqUrl = "hello template", reqBody = "body"} -- 1、外部模板文件 template.render("/index.html", model) end return _M ``` lua/web/postman.lua實現接收表單數據并處理Post邏輯 ``` local template = require "resty.template" local postman = require("cus.postman") local _M = {} local function Parse() local model = {} local request_method = ngx.var.request_method local args = nil local Body = nil local CurTime = nil local CheckSum = nil local MD5 = nil local host = nil if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() --獲取post請求的參數 local post_args_tab = ngx.req.get_post_args() for k, v in pairs(post_args_tab) do model[k]=v end Body = ngx.req.get_body_data() end model["resp"] = postman.httpPost(model["reqUrl"], model["reqBody"]) return model end function _M.index() local model = Parse() -- 1、外部模板文件 template.render("/index.html", model) end return _M ``` ### 模板代碼 lua/tpl/index.html ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PostMan</title> </head> <body> <div style="text-align: center; float: left"> <div><h1>PostMain</h1></div> <div> <form action="/postman/index" method="post" name="postman"> <table style="width: 500px;height: 200px"> <tr> <td>請求地址:</td> <td><input id="reqUrl" name="reqUrl" type="text" style="width: 300px;height: 35px;" value="{*reqUrl*}"/></td> </tr> <tr> <td>請求body:</td> <td><textarea id="reqBody" name="reqBody" type="text" style="width: 300px;height: 55px">{*reqBody*}</textarea></td> </tr> <tr> <td>返回值:</td> <td><textarea id="resp" name="resp" disabled style="width: 300px;height: 105px">{*resp*}</textarea></td> </tr> </table> <input id="btn" type="submit" value="提交"/> </form> </div> </div> </body> </html> ``` ## 使用方法 訪問地址: http://ip:8080/index/index 第一個index代表lua/web/index.lua 第二個index代表lua/web/index.lua文件中的index方法。 ### lua_resty_template模板渲染方法 1. 使用template.render ``` local template = require "resty.template" template.render("/index.html", model) ``` 2.使用template.new ``` local template = require "resty.template" -- Using template.new local view = template.new "view.html" view.message = "Hello, World!" view:render() ``` 3. 使用template.compile ``` local template = require "resty.template" local func = template.compile("view.html") --執行函數,得到渲染之后的內容 local content = func(context) ngx.say(content) ``` ### 模板語法 * `{{expression}}`:輸出傳遞的值,轉義`html`相關標簽 * `{*expression*}`:輸出傳遞的值 * `{% lua code %}`:使用`lua`代碼 * `{(template)}`:引入`html`共用頁面 * `{(base.html, { title = "Hello, World" } )}`:引入`html`共用頁面,并傳遞相關值 * `{-verbatim-}...{-verbatim-}`/`{-raw-}...{-raw-}`:可原樣輸出模板語法 * `{# comments #}`:在模板中使用注釋,不會被執行和輸出 ## 遇到的坑 網頁上直接輸出了html字符串。而不是正常網頁 解決辦法 nginx.conf配置文件加上default_type text/html; ``` server { default_type text/html; location / { default_type text/html; } } ``` 入口文件mvc.lua開頭加上下面的內容: ``` ngx.say("<br/>") ``` ## 項目地址 https://github.com/diycat1024/OpenRestyPostman
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看