Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 210 lines (176 sloc) 6.58 KB
<?npl
--[[
Title: routing ajax/html requests
Author: LiXizhi
Date: 2016/6/29
Desc: redirect AJAX URL to model functions
Reference: http://guides.rubyonrails.org/routing.html
Mapping of CRUD AJAX operations:
- PUT "/api/wiki/models/user/new" --> wiki/models/user:create()
- GET "/api/wiki/models/user" --> wiki/models/user:get()
- PUT "/api/wiki/models/user" --> wiki/models/user:update()
- DELETE "/api/wiki/models/user" --> wiki/models/user:delete()
- XXX "/api/wiki/models/user/[anyfunc]" --> wiki/models/user:api_[anyfunc]()
Everything else is mapped to `wiki.page` as standard html file.
]]
WIKI_ROOT = dirname(__FILE__);
WIKI_WEBROOT = WIKI_WEBROOT or "/wiki/";
--log("----------------------")
--log(request:GetHost())
-- 加载helper相关文件
include_once(WIKI_ROOT .. 'helpers/index.page')
local path = pagename() or "";
local ajax_type, ajax_path = path:match("^/?api/(%w+)/(.+)");
if ajax_type then
refuseLockedUser()
end
if(ajax_type == "mod") then
include_once("./mod/routes.page");
return;
elseif(ajax_type == "wiki") then
-- Allow for cross-domain requests (from the frontend).
-- send_origin_headers();
-- cross-domain request allow
addheader('Access-Control-Allow-Origin','*'); -- debug use
--addheader('Access-Control-Allow-Credentials', 'true'); -- debug use
addheader('Access-Control-Allow-Methods',"OPTIONS, GET, POST, PUT, DELETE"); -- debug use
addheader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, Authorization"); -- debug use
addheader('Access-Control-Expose-Headers', 'Date')
addheader('Content-Type', 'text/html');
addheader('X-Robots-Tag', 'noindex');
addheader('X-KeepWork-Defined-Message', request:header('X-KeepWork-Defined-Message')); -- 淘米专用
send_nosniff_header();
nocache_headers();
-- debug use start
if request:GetMethod() == "OPTIONS" then
return response:send("OPTIONS", true);
end
-- end
local modelname, params = ajax_path:match("^models/([%w_]+)/?(.*)")
if(modelname) then
local modelfilename = "./models/"..modelname..".page";
if(not file_exists(modelfilename)) then
return response:status(404):send({message="invalid model"});
end
include_once(modelfilename);
local model = models and models[modelname];
if(not model) then
return response:status(404):send({message="model not found"});
else
model = model:new();
end
local func_name = params and params:match("^(%w+)");
if(func_name) then
if(func_name == "new") then
if(model.create) then
local result = model:create(request:getparams());
return response:send(result, true);
end
else
local name = "api_"..func_name;
if(model[name]) then
local result = model[name](model, request:getparams())
return response:send(result, true);
end
end
end
-- redirect CRUD URL to method in model.
local req_method = request:GetMethod();
if(req_method == "GET" or req_method == "POST") then
if(model.get) then
local result = model:get(request:getparams());
return response:send(result, true);
end
elseif(req_method == "PUT") then
if(model.update) then
local result = model:update(request:getparams());
return response:send(result, true);
end
elseif(req_method == "DELETE") then
if(model.delete) then
local result = model:delete(request:getparams());
return response:send(result, true);
end
end
else
-- only allow ajax in wiki folder
include( "./"..ajax_path..".page" );
end
return;
else
-- dynamic page has no cache headers
response:nocache_headers();
end
?>
<?npl
include_once(WIKI_ROOT .. "auth/api.page")
-- 解析URL
function parseURL()
local host = request:GetHost() or ""
local sitename = (request:GetHost() or ""):match("([%w]+)%.[%w]+%.[%w]+")
-- 排除IP地址访问
if (host:match("^%d+%.%d+%.%d+%.%d+$")) or (host:match("^%d+%.%d+%.%d+%.%d+:%d+$")) or (sitename == "www") then
sitename = nil
end
if sitename and sitename ~= "www" then
local _pagename, subpage = request:url():match("^/?([^/]*)(.*)")
else
local url = commonlib.Encoding.url_decode(request:url())
sitename, _pagename, subpage = url:match("^/?([^/]+)/?([^/]*)(.*)");
end
return sitename, _pagename, subpage
end
function RenderServerWikiContent()
local sitename, _pagename = parseURL()
if (not sitename) or (not _pagename) then
return
end
--log(sitename)
--log(_pagename)
--log(subpage)
if sitename == nil or sitename == "wiki" or sitename == "api" then
if _pagename == "mod" then
include_once(WIKI_ROOT .. "mod/routes.page");
else
local filename = WIKI_ROOT .. 'pages/' .. _pagename .. subpage .. ".page";
if file_exists(filename) then
include(filename);
end
end
if _pagename and _pagename:match("^_") then
local filename = WIKI_ROOT .. 'pages/special/' .. _pagename:gsub("^_", "") .. ".page";
if(file_exists(filename)) then
include(filename);
end
end
end
end
function RenderServerWikiCss()
local bustVersion = getBustVersion()
echo(format('<link href="/' .. WIKI_CODE_ENV .. '/assets/css/main.css?bust=%s" rel="stylesheet">\n', bustVersion))
end
function RenderServerWikiScript()
local config = {}
config.hostname = request:GetHost() or ""
config.serverUpdateTime = getServerUpdateTime()
config.webroot = '/' .. WIKI_CODE_ENV .. '/'
config.wikiEnv = WIKI_ENV
config.locale = os.getenv("KEEPWORK_LOCALE")
config.gitServerHost = util:getGitServerConfig()["host"]
--config.token = getTokenStr()
local devIndex = string.find(config.hostname, ".dev.keepwork.com")
if config.hostname == "dev.keepwork.com" or config.hostname == "stage.keepwork.com" or devIndex then
config.bustVersion = os.time()
else
config.bustVersion = getBustVersion()
end
-- log(config)
echo(format('<script>window.wiki_config=%s</script>\n', json_encode(config)))
echo(format('<script src="/' .. WIKI_CODE_ENV .. '/js/lib/jquery/jquery.min.js?bust=%s"></script>\n', config.bustVersion))
echo(format('<script src="/' .. WIKI_CODE_ENV .. '/js/lib/jquery-cookie/jquery.cookie.js?bust=%s"></script>\n', config.bustVersion))
echo(format('<script src="/' .. WIKI_CODE_ENV .. '/js/app/config.js?bust=%s"></script>\n', config.bustVersion))
echo(format('<script data-main="/' .. WIKI_CODE_ENV .. '/js/main.js?bust=%s" src="/wiki/js/lib/requirejs/require.js?bust=%s"></script>\n', config.bustVersion, config.bustVersion))
end
-- 加载通用页
include(WIKI_ROOT .. "index.page")
?>