Skip to content

Commit

Permalink
demo9
Browse files Browse the repository at this point in the history
  • Loading branch information
git committed Jan 10, 2017
1 parent 4ce12ba commit 45a7667
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 0 deletions.
27 changes: 27 additions & 0 deletions demo9/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

worker_processes 1;

error_log logs/error.log notice;

events {
worker_connections 1024;
}

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

location / {
root lua;
default_type "text/html; charset=utf-8";
content_by_lua_file lualib/lite/mvc.lua;
}

location ~ ^/js/|^/css/|\.html {
root html;
}
}
}
2 changes: 2 additions & 0 deletions demo9/logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
*.pid
61 changes: 61 additions & 0 deletions demo9/lua/mvc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

local uri = ngx.var.uri
-- 如果是首页
if uri == "" or uri == "/" then
local res = ngx.location.capture("/index.html", {})
ngx.say(res.body)
return
end

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

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


9 changes: 9 additions & 0 deletions demo9/lua/tpl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
{* content *}
</body>
</html>
21 changes: 21 additions & 0 deletions demo9/lua/web/index.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local template = require "resty.template"

local _M = {}

function _M.index()
local model = {title = "hello template", content = "<h1>content</h1>"}
template.render('tpl/index.html', model)
-- template.render([[
-- <html>
-- <head>
-- <meta charset="UTF-8">
-- <title>{{title}}</title>
-- </head>
-- <body>
-- {* content *}
-- </body>
-- </html>
-- ]], model)
end

return _M
21 changes: 21 additions & 0 deletions demo9/lua/web/user.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local cjson = require "cjson"
local req = require "lite.req"

local _M = {}

local users = {"张三", "李四", "王五"}

function _M.index()
ngx.say(cjson.encode(users))
end

function _M.get()
local args = req.getArgs()
local index = tonumber(args['index'])
if not index then
index = 1
end
ngx.say(users[index])
end

return _M
61 changes: 61 additions & 0 deletions demo9/lualib/lite/mvc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

local uri = ngx.var.uri
-- 如果是首页
if uri == "" or uri == "/" then
local res = ngx.location.capture("/index.html", {})
ngx.say(res.body)
return
end

local m, err = ngx.re.match(uri, "([a-zA-Z0-9-]+)/*([a-zA-Z0-9-]+)*")

local is_debug = true -- 调试阶段,会输出错误信息到页面上

local moduleName = m[1] -- 模块名
local method = m[2] -- 方法名

if not method then
method = "index" -- 默认访问index方法
else
method = ngx.re.gsub(method, "-", "_")
end

-- 控制器默认在web包下面
local prefix = "web."
local path = prefix .. moduleName

-- 尝试引入模块,不存在则报错
local ret, ctrl, err = pcall(require, path)

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


20 changes: 20 additions & 0 deletions demo9/lualib/lite/req.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local _M = {}

-- 获取http get/post 请求参数
function _M.getArgs()
local request_method = ngx.var.request_method
local args = ngx.req.get_uri_args()
-- 参数获取
if "POST" == request_method then
ngx.req.read_body()
local postArgs = ngx.req.get_post_args()
if postArgs then
for k, v in pairs(postArgs) do
args[k] = v
end
end
end
return args
end

return _M
Binary file added demo9/lualib/resty/.DS_Store
Binary file not shown.
Loading

0 comments on commit 45a7667

Please sign in to comment.