標籤:style class blog code http tar
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庫的安裝包,
包括 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_worldlocal 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 numberlocal 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 environmentfunction 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 functionsxmlrpc_exports = {}--- Get simple string.-- @return simple stringfunction xmlrpc_exports.hello_world() return "Hello World"end--- add two number function.-- @return sumfunction xmlrpc_exports.add(a, b) return a + bendlocal 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()