Lua中使用Web開發(fā)
Lua 中使用Web 開發(fā)目錄Kepler 環(huán)境搭建 . .......................................................................
Lua 中使用Web 開發(fā)
目錄
Kepler 環(huán)境搭建 . ................................................................................................. 2
Xavante 參數(shù)配置 ............................................................................................... 3
將LUA 嵌入ASP.Net 頁面 .................................................................................... 5
標(biāo)簽:lua,web 服務(wù)器,web 開發(fā),web 服務(wù)
1
,LUA 中使用Web 開發(fā)初探是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)在lua 中如何實現(xiàn)web 開發(fā)的,具體內(nèi)容來看本文詳細(xì)內(nèi)容講解。
Kepler 環(huán)境搭建
我本身是做Web 開發(fā)的,自然喜歡把新學(xué)習(xí)的東西往Web 方面聯(lián)系。提到Web 和Lua ,所有人都會自然而然的想起一個開源的項目Kepler (http://www.keplerproject.org/)。它給我最大的感覺就是短小精悍,實在有點難以置信,包括Socket, IO, TCP/IP, HTTP, WebCGI 等眾多功能模塊的軟件包在內(nèi)的下載和安裝包,居然只有900K 左右!安裝的過程也很簡單,我寫一下我在Windows2003/XP下安裝Kepler 的過程:
1、下載并安裝LuaRocks (http://www.luarocks.org/),默認(rèn)的最新版本安裝目錄在 C:LuaRocks.5.2,安裝之后,推薦將這個目錄加入Windows 系統(tǒng)Path 環(huán)境參數(shù)中。
2、在CMD 命令窗口中鍵入 luarocks install kepler-xavante (沒加Path 參數(shù)的,需要打出 luarocks 全路徑),注意,此時你的電腦一定要可以正常連結(jié)網(wǎng)絡(luò),以便可以在網(wǎng)上下載Kepler, Xavante程序功能包,會有約3~5分鐘的下載安裝過程。
3、Kepler 安裝完成后,C:LuaRocks 目錄下會新建2個子目錄 bin 和 rocks,bin 下面是Kepler 核心的可執(zhí)行文件,rocks 下面是它使用到的Lua 軟件功能包和配置信息。安裝完成之后,建議將 C:LuaRocksin 目錄也加入Windows 系統(tǒng)Path 環(huán)境參數(shù)中。
4、運行 setup-kepler (沒加Path 參數(shù)的,需要打出 C:LuaRocksinsetup-kepler )批處理命令進(jìn)行默認(rèn)Web 站點的配置。我采用Kepler 安裝后默認(rèn)的配置,如果希望改變配置,可以修改 C:LuaRocks
ockskepler1.1-1in 目錄下 setup-kepler 文件內(nèi)容。
5、Web 站點初始化完畢,會發(fā)現(xiàn) C:LuaRocks 目錄下,又多出一個 kepler 子目錄(此目錄名和以上安裝Web 站點的配置有關(guān)),這就是以后我們經(jīng)常關(guān)注的站點內(nèi)容了,打開 C:LuaRockskeplerhtdocs est.lp 文件看看,是不是感覺中間的內(nèi)容就是Lua 語法的ASP/JSP? :)
6、Web 網(wǎng)站的內(nèi)容都具備了,就差啟動Web 服務(wù)器進(jìn)程了。運行 xavante_start (沒加Path 參數(shù)的,需要打出 C:LuaRocksinxavante_start )批處理命令就可以,關(guān)于Web 網(wǎng)站的參數(shù)配置,下文會詳細(xì)介紹。
安裝的工作已經(jīng)完成了,如果采用的是Kepler 的默認(rèn)配置,那么用你的瀏覽器打開 http://localhost:8080/ 是不是看到Kepler 的歡迎頁面了?
2
,喜歡正版攻略的朋友,安裝的過程也可以參照
Xavante 參數(shù)配置
在Kepler 軟件包中,真正起Web 服務(wù)器作用的模塊叫做Xavante 。它的配置文件是一個Lua 文件,它位于 C:LuaRockskepleretcxavanteconfig.lua ,在使用 setup-kepler 命令安裝站點之前,可以修改Kepler 默認(rèn)的配置文件 C:LuaRocks
ockskepler1.1-1confxavanteconfig.lua 讓你的修改生效于以后所有創(chuàng)建的新網(wǎng)站。下面我們看看這個配置文件的配置內(nèi)容。
這個文件的默認(rèn)內(nèi)容:
-- 文件開頭關(guān)于版權(quán)信息略去
require "xavante.filehandler"
require "xavante.cgiluahandler"
require "xavante.redirecthandler"
require "orbit.ophandler"
-- Define here where Xavante HTTP documents scripts are located
local webDir = XAVANTE_WEB
local simplerules = {
{ -- URI remapping example
match = "^/$",
with = xavante.redirecthandler,
params = {"index.lp"}
},
{ -- cgiluahandler example
match = {".lp$", ".lp/.*$", ".lua$", ".lua/.*$" },
with = xavante.cgiluahandler.makeHandler (webDir)
},
{ -- ophandler example
match = {".op$", ".op/.*$" },
with = orbit.ophandler.makeHandler (webDir)
},
{ -- wsapihandler example
match = {".ws$", ".ws/" },
with = wsapi.xavante.makeGenericHandler (webDir)
},
{ -- filehandler example
match = ".",
with = xavante.filehandler,
params = {baseDir = webDir}
},
3
,}
-- 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.HTTP{
server = {host = "*", port = 8080},
defaultHost = {
rules = simplerules
},
}
文件共分3個部分:simplerules, xavante.start_message, xavante.HTTP:
simplerules :類似于ASP.Net 在IIS 里面的URL Rewrite功能的URL 重發(fā)功能,說白了就是依次檢索,找到匹配的Request URL正則項,轉(zhuǎn)發(fā)給這個匹配項中所定義的Web 站點中某個腳本文件進(jìn)行運算,最后返回HTTP Response的內(nèi)容。
xavante.start_message :用于記錄每次Xavante 進(jìn)程啟動后,程序發(fā)生的Log 記錄格式的配置。
xavante.HTTP : 用于Web 服務(wù)器主要參數(shù)配置。修改port = 8080, 可以修改HTTP 服務(wù)的默認(rèn)端口。如果想在你的Xavante 服務(wù)器上綁定域名,請?zhí)砑右豁棧?/p>
virtualHosts = {
["www.rex.com"] = simplerules --www.rex.com 就是你自己網(wǎng)站的域名
},
注意:這里 virtualHosts 的 H 一定要大寫,否則程序?qū)箦e!這一點,是Kepler 官方網(wǎng)站資料上存在筆誤,我調(diào)了半個多小時才嘗試出來這一點,希望后來的朋友少走點彎路。還有就是,配置了 virtualHosts 節(jié)之后,原來的 defaultHost 節(jié)內(nèi)容不能去掉,不然也會造成程序出錯,加入了域名綁定后,我完整的 xavante.HTTP 配置如下: xavante.HTTP{
server = {host = "*", port = 80},
defaultHost = {
rules = simplerules
},
virtualHosts = {
["www.rex.com"] = simplerules
},
}
4
,注意:如果這事你有其它Web 服務(wù)程序(如Appach ISS等)同時使用80端口,也將造成Xavante 啟動出錯。因此,請在啟動Xavante 之前,暫停其它Web 服務(wù)程序。這一點,我一直很費解:為什么Xavante 的配置非要將Port 的配置強制加到每個站點上去呢?我是新手,請熟悉的朋友指教一二,非常感謝!
我嘗試使用Kepler 和Xavante 只有2天左右,先寫這么多,希望有機會和對此感興趣的朋友討論學(xué)習(xí)!
將LUA 嵌入ASP.Net 頁面
考慮到Xavante 的穩(wěn)定性問題,我決定嘗試一下使用IIS 作為Web 服務(wù)器,并且在
ASP.Net 里面使用Progress 對象調(diào)用LUA 的VM 來調(diào)用LUA 代碼。新建一個ASPX 頁面,寫入以下Code behind代碼:
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
,proc.StartInfo.FileName = exeFile;
proc.StartInfo.Arguments = string.Format(@" {0}", luaPath);
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
rtval = proc.StandardOutput.ReadToEnd();
}
return rtval;
}
這樣很簡單,通過一個ASPX 頁面作為代理,就可以解釋執(zhí)行LUA 文件了:通過訪問 http://localhost/default.aspx?lua=abc.lua ,你就可以解釋執(zhí)行位于該網(wǎng)站相對LUA 目錄下 abc.lua 文件,并獲得輸出,在通過HTTP Response的方式獲得輸出了。這種純粹的Text 輸出方式,也比較適合使用AJAX 和REST 的Web 程序中
6