码迷,mamicode.com
首页 > 其他好文 > 详细

xavante运行cgilua流程

时间:2016-06-23 00:59:03      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:

安装软件

需要安装三个lua 库:  xavante wsapi cgilua

 

luarocks install xavante

http://keplerproject.github.io/xavante/manual.html#install

Xavante is a Lua HTTP 1.1 Web server that uses a modular architecture based on URI mapped handlers. Xavante currently offers a file handler, a redirect handler and a WSAPI handler. Those are used for general files, URI remapping and WSAPI applications respectively.

 

 

luarocks install wsapi-xavante

http://keplerproject.github.io/wsapi/index.html

WSAPI is an API that abstracts the web server from Lua web applications. By coding against WSAPI your application can run on any of the supported servers and interfaces (currently CGI, FastCGI and Xavante, on Windows and UNIX-based systems).

https://github.com/keplerproject/wsapi

 

cgilua

luarocks install cgilua

https://github.com/keplerproject/cgilua

CGILua is a tool for creating dynamic Web pages and manipulating input data from Web forms. CGILua allows the separation of logic and data handling from the generation of pages, making it easy to develop web applications with Lua.

 

运行脚本

 

切换到cgilua样例目录

root@fqs:/home/share/cgilua-master/cgilua-master/examples# lua xavante_cgilua.lua
[2016-06-22 22:16:50] Xavante started on port(s) 8080

 

脚本内容:

xavante = require "xavante"
xavante.filehandler = require "xavante.filehandler"
xavante.cgiluahandler = require "xavante.cgiluahandler"
xavante.redirecthandler = require "xavante.redirecthandler"

-- Define here where Xavante HTTP documents scripts are located
local webDir = "."

local simplerules = {

   { -- URI remapping example
      match = "^[^%./]*/$",
      with = xavante.redirecthandler,
      params = {"index.lp"}
   },

   { -- cgiluahandler example
      match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },
      with = xavante.cgiluahandler.makeHandler (webDir)
   },
   
   { -- filehandler example
      match = ".",
      with = xavante.filehandler,
      params = {baseDir = webDir}
   },
}

xavante.HTTP{
   server = {host = "*", port = 8080},
   
   defaultHost = {
      rules = simplerules
   },
}


-- Displays a message in the console with the used ports
xavante.start_message(function (ports)
    local date = os.date("[%Y-%m-%d %H:%M:%S]")
    print(string.format("%s Xavante started on port(s) %s",
                date, table.concat(ports, ", ")))
end)

xavante.start()

 

 

 

运行流程分析

1、 xavante 负责服务器的启动, 并制定 cgilua 请求处理的handler注册

   { -- cgiluahandler example
      match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },
      with = xavante.cgiluahandler.makeHandler (webDir)
   },

 

2、xavante.cgiluahandler.makeHandler (webDir) , handler的构造

 

-------------------------------------------------------------------------------
-- Returns the CGILua handler
-------------------------------------------------------------------------------
function _M.makeHandler (diskpath, params)
   params = setmetatable(params or {}, { __index = { modname = "wsapi.sapi",
      bootstrap = bootstrap } })
   local sapi_loader = common.make_isolated_launcher(params)
   return xavante.makeHandler(sapi_loader, nil, diskpath)
end

 

其中,handler构造依赖了 xavante的wsapi, with的值可以是 function(req, res)

local xavante = require "wsapi.xavante"  --- 专门针对  xavante写的wsapi的适配

-- Makes a WSAPI handler for a single WSAPI application
function _M.makeHandler (app_func, app_prefix, docroot, app_path, extra_vars)
  return function (req, res)
    return wsapihandler(req, res, app_func, app_prefix, docroot, app_path, extra_vars)
  end
end

wsapi.xavante中存在 构造 cgivars 表。

 

make_handler 入参 wsapi_loader需要 wsapi.sapi文件支持

   params = setmetatable(params or {}, { __index = { modname = "wsapi.sapi",
      bootstrap = bootstrap } })
   local sapi_loader = common.make_isolated_launcher(params)

此loader即加载 wsapi.sapi文件, 文件被rings包裹运行, 即 wsapi.sapi –> cgilua 都是在独立环境下运行

前面 “wsapi.xavante中存在 构造 cgivars 表” 也会传入 独立的运行环境。

 

wsapi.sapi 直接 调用 cgilua

  local cgilua = require "cgilua"
  cgilua.main()
  return res:finish()
end

xavante运行cgilua流程

标签:

原文地址:http://www.cnblogs.com/lightsong/p/5608957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!