forked from 362228416/openresty-web-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
git
committed
Jan 10, 2017
1 parent
4ce12ba
commit 45a7667
Showing
10 changed files
with
700 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.log | ||
*.pid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.