LUAUsed inWeb DevelopmentThis article focuses on understanding and learning aboutLuaHow to ImplementWeb DevelopmentFor more information, see the detailed description in this article.
Kepler environment setup: I develop Web applications. Naturally, I like to associate New learning with the Web. When it comes to Web and Lua, everyone will naturally think of an open source project Kepler http://www.keplerproject.org /). It gives me the biggest feeling that it is short and concise, and it is a bit unbelievable, including the download and installation packages of Socket, IO, TCP/IP, HTTP, WebCGI and other multi-functional module software packages, it's about K! The installation process is also very simple. I will write down the process of installing Kepler in Windows2003/XP:
1, download and install LuaRocks http://www.luarocks.org/), the default latest version of the installation directory in C: LuaRocks.5.2, after installation, it is recommended to add this directory to the Windows system Path environment parameters.
2. In the CMD command window, type luarocks install kepler-xavante without adding the Path parameter. You need to specify the full luarocks Path.) note that your computer must be connected to the network properly, so that you can download the Kepler and Xavante program feature packages online. There will be about 3 ~ Download and install the SDK within 5 minutes.
3. After Kepler is installed, the C: LuaRocks directory creates two subdirectories bin and rocks. The following is the executable file of the Kepler core, the Lua software feature package and configuration information used by rocks are shown below. After the installation is complete, we recommend that you add the C: LuaRocksin directory to the Windows Path environment parameters.
4. Run the "setup-kepler" command without adding the Path parameter and run the "C: LuaRocksinsetup-kepler" command to configure the default Web site. I use the default configuration after Kepler installation. If you want to change the configuration, you can modify C: LuaRocks
The content of the setup-kepler file in the ockskepler1.1-1in directory.
5. After the Web site Initialization is complete, you will find that the directory name in the C: LuaRocks directory is related to the configuration of the Web site installed above ), this is what we will pay attention to in the future. Open C: LuaRockskeplerhtdocs est. in the lp file, do you feel that the content in the middle is the ASP/JSP of Lua syntax? :)
6. If the content of the Web site is ready, the Web server process may be started. Run xavante_start without the Path parameter. Run the C: LuaRocksinxavante_start (Batch Processing Command). The parameter configuration of the Web site is described in detail below.
The installation has been completed. If the default configuration of Kepler is used, open http: // localhost: 8080/in your browser to see the welcome page of Kepler?
Like genuine raiders of friends, the installation process can also refer to the http://www.keplerproject.org/en/Installation, but I feel it write not detailed enough, especially the last step, there are errors: directly running the xavante.exe file will cause errors that cannot be found in the reference database, and the Web service program cannot be started successfully. This is because the environment path is not configured. If you are a little careful, Open C: luaRocksinxavante_start.bat file.
Xavante parameter configuration
In the Kepler software package, the module that actually acts as a Web server is called Xavante. Its configuration file is a Lua file located in C: LuaRockskepleretcxavanteconfig. lua. before using the setup-kepler command to install the site, you can modify the default configuration file C: LuaRocks of Kepler.
Ockskepler1.1-1confxavanteconfig. lua allows your modifications to take effect for all new websites created in the future. Next let's take a look at the configuration content of this configuration file.
Default content of this file:
-- The copyright information at the beginning of the file is omitted
- 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}
- },
- }
-
- -- 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
- },
- }
The file is divided into three parts: simplerules, xavante. start_message, xavante. HTTP:
Simplerules: similar to ASP. net in IIS, the URL Rewrite function of the URL resend function, to put it bluntly, is to search in order, find the matching Request URL regular item, forward it to a script file in the Web site defined in this matching item for calculation, and finally return the HTTP Response content.
Xavante. start_message: used to record the Log record format configuration of the program after each Xavante process starts.
Xavante. HTTP: used to configure the main parameters of the Web server. Modify port = 8080 to modify the default port of the HTTP service. To bind a domain name to your Xavante server, add one:
- VirtualHosts = {
- ["Www.rex.com"] = simplerules --www.rex.com is the domain name of your website.
- },
Note: The H of virtualHosts must be capitalized here, otherwise the program will report an error! This is because of a written mistake on the official Kepler website. I tried this after being transferred for more than half an hour. I hope my friends will not take any detours. In addition, after the virtualHosts section is configured, the content of the original defaultHost Section cannot be removed. Otherwise, a program error may occur. After the domain name is bound, my complete xavante. HTTP configuration is as follows:
- xavante.HTTP{
- server = {host = "*", port = 80},
- defaultHost = {
- rules = simplerules
- },
-
- virtualHosts = {
- ["www.rex.com"] = simplerules
- },
- }
Note: If you have otherWebService programs such as Appach ISS) use port 80 at the same time, which may also cause Xavante startup errors. Therefore, pause other Web Services before starting Xavante. I have always been puzzled: Why does the Xavante configuration have to force the Port configuration to each site? I am a newbie. Thank you for your advice!
I tried to use Kepler and Xavante for only about two days. I wrote so many articles first, hoping to have a chance to discuss and learn with friends interested in it!
Embed LUA into an ASP. Net page
Considering the stability of Xavante, I decided to use IISWebServer. in ASP. Net, the Progress object is used to call the lua vm to call the LUA code. Create An ASPX page and write the following Code:
- 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)
- {
- if (!object.Equals(Request["lua"], null))
- {
- string luaFile = Request["lua"];
- if (!string.IsNullOrEmpty(luaFile)
- {
- Response.Cache.SetNoStore();
- Response.Cache.SetNoServerCaching();
- string output = _Default.EnvokeLua(this, luaFile);
- Response.Write(output);
- Response.End();
- }
- }
- }
- static string EnvokeLua(Page pg,string luaFile)
- {
- string rtval = string.Empty;
- HttpServerUtility hsu = pg.Server;
- string exeFile = hsu.MapPath("~/lib/lua.exe");
- string luaPath = hsu.MapPath(string.Format("~/lua/{0}.lua", luaFile));
- using (Process proc = new Process())
- {
- 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;
- }
This is simple. You can use An ASPX page as a proxy to explain how to execute the LUA file: Access http: // localhost/default. aspx? Lua = abc. lua, you can explain that the execution is located in the relative LUA directory of the website abc.LuaFile, and get the output. The output is obtained through HTTP Response. This pure Text output method is also suitable for AJAX and RESTWebProgram.
Summary:LUAThe content of the preliminary study on using Web development has been introduced. I hope this article will help you!