Lua xavante WEB server实现xmlrpc服务器端

xavante

xavante是一个使用lua实现的遵守http1.1的web server,支持wsapi。

依赖库:

xavante核心 -- lua, copas(纯lua编写,网络连接coroutine处理), luasocket处理网络连接。

xavante file handler -- luaFileSystem

此项目属于kepler项目的一个子项目,见官网地址:

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

github上维护开源代码:

https://github.com/keplerproject/xavante

xavante提供三类处理接口:

URL映射, 文件上传, 和 CGIlua调用,配置运行步骤 见官网介绍:

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

xavante是以一个库的形式存在,如果想运行必须下载wsapi。

WSAPI

wsapi是从web应用中抽象出来的web server接口, 按照wsapi接口编程的web应用程序具有可移植性,

可以再不同的服务器上运行, 包括 CGI FASTCGI XAVANTE

主要负责,请求处理 和 输出缓存, 详情见官网:

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

安装运行:

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

接口说明文档:

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

实验 -- lua xml rpc 访问xavante提供的xmlrpc service

  • 下载安装 lua for windows

lua for windows 是一个包含了很多lua库的安装包,

包括 luaExpat luaSocket luaFileSystem Copas Rings 等库,

其中Copas为xavante依赖的库;

luaExpat 和 luaSocket为lua-xmlrpc依赖库。

http://code.google.com/p/luaforwindows/downloads/list

  • xavante安装
  • 将解压后的xavante文件夹中src的xavante目录拖到lua.exe同级,
  • 并文件夹中的xavante.lua拖出来与lua.exe同级。
  • wsapi安装
  • 将加压后的wsapi文件夹中的src的 wsapi 目录和 wsapi.lua 文件拖到lua.exe同级。
  • 将lua-xmlrpc加压后的 src文件放到 xmlrpc目录中(与lua.exe同级)
  • 将 lua-xmlrpc 的 client.lua 和 server-xavante.lua 添加add加法rpc调用,放到lua.exe同级。

代码:

client.lua

  • require("xmlrpc.http")
    
    -- hello_world
    local ok, res = xmlrpc.http.call("http://localhost:12345", "hello_world")
    assert(ok, string.format("XML-RPC call failed on client: %s", tostring(res))) print("Result: " .. tostring(res)) -- add number
    local ok, res = xmlrpc.http.call("http://localhost:12345", "add", 1, 2)
    assert(ok, string.format("XML-RPC call failed on client: %s", tostring(res))) print("Result: " .. tostring(res))

server_xavante.lua

xavante = require("xavante") wsapi = require("wsapi") wsapi.xavante = require("wsapi.xavante") wsapi.request = require("wsapi.request")

require("xmlrpc")

--- XML-RPC WSAPI handler
-- @param wsapi_env WSAPI environment
function wsapi_handler(wsapi_env)
   local headers = { ["Content-type"] = "text/xml" }
   local req = wsapi.request.new(wsapi_env) local method, arg_table = xmlrpc.srvDecode(req.POST.post_data)
   local func = xmlrpc.dispatch(method)
   local result = { pcall(func, unpack(arg_table or {})) }
   local ok = result[1]
   if not ok then
      result = { code = 3, message = result[2] }
   else
      table.remove(result, 1)
      if table.getn(result) == 1 then
         result = result[1]
      end
   end

   local r = xmlrpc.srvEncode(result, not ok)
   headers["Content-length"] = tostring(#r)

   local function xmlrpc_reply(wsapienv) coroutine.yield(r)
   end

   return 200, headers, coroutine.wrap(xmlrpc_reply)
end

-- XML-RPC exported functions
xmlrpc_exports = {}

--- Get simple string.
-- @return simple string
function xmlrpc_exports.hello_world()
   return "Hello World"
end

--- add two number function.
-- @return sum
function xmlrpc_exports.add(a, b)
   return a + b
end

local rules = {{ match = ".", with = wsapi.xavante.makeHandler(wsapi_handler) }} local config = { server = {host = "*", port = 12345}, defaultHost = { rules = rules} } xmlrpc.srvMethods(xmlrpc_exports) xavante.HTTP(config) xavante.start()