openresty 前端開發輕量級MVC框架封裝一(控制器篇)

經過前面幾章,咱們已經掌握了一些基本的開發知識,可是代碼結構比較簡單,缺少統一的標準,模塊化,也缺少統一的異常處理,這一章咱們主要來學習如何封裝一個輕量級的MVC框架,規範以及簡化開發,而且提供相似php所見即所得的能力

一、統一入口

一般來講一個mvc框架會有一個統一的入口點,相似於spring mvc的DispatcherServlet,會攔截全部的請求,也就是/,因而咱們能夠得出咱們的入口點php

conf/nginx.confcss

worker_processes  1;

error_log logs/error.log notice;

events {
    worker_connections 1024;
}

http {
    lua_package_path "/Users/john/opensource/openresty-web-dev/demo8/lua/?.lua;/Users/john/opensource/openresty-web-dev/demo8/lualib/?.lua;/usr/local/openresty/lualib/?.lua";
    server {
        listen 80;
        server_name localhost;
        lua_code_cache off;

        location / {
            content_by_lua_file lua/mvc.lua;
        }

        location ~ ^/js/|^/css/|\.html {
            root html;
        }
    }
}

除了靜態文件js/css/html文件,其餘的請求都會被咱們的mvc.lua處理。html

默認頁面

當請求uri爲空時,默認返回index.html頁面,固然也能夠本身定義,實現這個效果很簡單前端

local uri = ngx.var.uri
-- 默認首頁
if uri == "" or uri == "/" then
    local res = ngx.location.capture("/index.html", {})
    ngx.say(res.body)
    return
end

二、url解析

這裏簡單的把url解析成模塊名模塊方法,根據/分割,若是隻有模塊名,沒有方法名,則默認爲index方法nginx

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模塊

獲得模塊名以後,須要動態引入模塊,經過pcall,而後再調用模塊的方法git

-- 控制器默認在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

四、異常處理

能夠看到,從引入模塊,到獲取模塊方法,已經執行方法,都有可能報錯,這裏經過pcall來進行調用,這種方式能夠安全的調用lua代碼,不會致使異常中斷,而後經過定義一個變量,來區分是否爲開發調試階段,若是是則把錯誤信息輸出到瀏覽器端,不然直接報404或者500,避免把錯誤信息輸出到客戶端,致使代碼泄漏。github

至此,一個簡單的mvc框架已經能夠使用了,可是如今還只能作前端渲染,下一章,我講介紹若是進行服務端渲染。web

示例代碼 參見demo8部分spring

相關文章
相關標籤/搜索