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

相关文章
相关标签/搜索